这个问题在这里已有答案:
有许多选项可用于实施您的任务。其中之一是绝对定位中的伪元素。我们在父单元格的整个区域上拉伸每个伪元素,并使用background
属性绘制对角线。从左上角到右下角的一个伪元素,第二个 - 从左下角到右上角。无论细胞的宽度或高度如何 - 线条都会从一个角落到另一个角落。如果您需要将单元格内容放在行顶部,则需要使用z-index
属性将其放置在伪元素上方。
HTML:
<table>
<tr>
<td class="cross">
<span>Text</span>
</td>
<td>
<span>Text</span>
</td>
<td class="cross">
<span>Text</span>
</td>
</tr>
</table>
CSS:
td {
border: 1px solid #222;
}
.cross span {
position: relative;
z-index: 2;
}
.cross {
position: relative;
width: 200px;
height: 100px;
text-align: center;
}
.cross:before,
.cross:after {
content: '';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: 1;
}
.cross:before {
background: linear-gradient(to top right, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px) );
}
.cross:after {
background: linear-gradient(to left top, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px) );
}
一个例子: