css 实现表格对角线

HTML 表格表头的对角线,在 HTML 里面没有可用的标签可以方便简单的实现,必须要用 css 撸。

应用场景

示例:

example.html

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表格对角线</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}

caption {
font-size: 14px;
font-weight: bold;
}

table {
border-collapse: collapse;
border: 1px #525152 solid;
width: 600px;
margin: 0 auto;
margin-top: 100px;
}

th,
td {
border: 1px #525152 solid;
text-align: center;
font-size: 12px;
line-height: 30px;
background: #C6C7C6;
}

th {
background: #D6D3D6;
}

/*模拟对角线*/
.out {
border-top: 40px #D6D3D6 solid;
/*上边框宽度等于表格第一行行高*/
width: 0px;
/*让容器宽度为0*/
height: 0px;
/*让容器高度为0*/
border-left: 80px #BDBABD solid;
/*左边框宽度等于表格第一行第一格宽度*/
position: relative;
}

b {
font-style: normal;
display: block;
position: absolute;
top: -40px;
left: -40px;
width: 35px;
}

em {
font-style: normal;
display: block;
position: absolute;
top: -25px;
left: -70px;
width: 55x;
}

.t1 {
background: #BDBABD;
}
</style>
</head>

<body>
<table>
<caption>
用div+css模拟表格对角线
</caption>
<tr>
<th style="width:80px;">
<div class="out"> <b>类别</b> <em>姓名</em> </div>
</th>
<th>年级</th>
<th>班级</th>
<th>成绩</th>
</tr>
<tr>
<td class="t1">张三</td>
<td></td>
<td>2</td>
<td>99</td>
</tr>
<tr>
<td class="t1">李四</td>
<td></td>
<td>1</td>
<td>95</td>
</tr>
</table>
</body>

</html>

本文源于很早之前看到的一个技巧,来源不知出处。

本文由 linx(544819896@qq.com) 创作,采用 CC BY 4.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。本文链接为: https://blog.jijian.link/2020-03-13/css-table-diagonal/

如果您觉得文章不错,可以点击文章中的广告支持一下!