我想创建一个HTML表格,可以像下面的图示一样划掉单元格:
我最初想到在单元格中使用CSS右三角形,但是我无法弄清如何仅涂色斜边而不涂色其余两个侧面或三角形本身。
换句话说,我想做的事情是可能的吗? 制作一个带有对角线的图像,然后使该图像拉伸到单元格的100%宽度和高度是最明智的选择吗? 谢谢。
![Table with crossed out cells](https://istack.dev59.com/zEQCY.webp)
换句话说,我想做的事情是可能的吗? 制作一个带有对角线的图像,然后使该图像拉伸到单元格的100%宽度和高度是最明智的选择吗? 谢谢。
虽然有点取巧,但它有效。
利用linear-gradient
background-image
,在内容下面加删除线。table
{
min-width: 100%;
}
table td
{
border: 1px solid silver;
position: relative;
}
table td.crossed
{
background-image: linear-gradient(to bottom right, transparent calc(50% - 1px), red, transparent calc(50% + 1px));
}
<table>
<tbody>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td class="crossed">Content</td>
<td>Content</td>
</tr>
</tbody>
</table>
如果您想要划掉单元格的内容,您可以使用伪元素,例如:
table
{
min-width: 100%;
}
table td
{
border: 1px solid silver;
position: relative;
}
table td.crossed::after
{
position: absolute;
content: "";
left:0;
right:0;
top:0;
bottom:0;
background-image: linear-gradient(to bottom right, transparent calc(50% - 1px), red, transparent calc(50% + 1px));
}
<table>
<tbody>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td class="crossed">Content</td>
<td>Content</td>
</tr>
</tbody>
</table>
background-repeat: no-repeat;
解决了这个问题。 - Danieltr
上使用线性渐变? - Mehdi Abbassi <style>
td.diagonalRising
{
background: linear-gradient(to right bottom, #ffffff 0%,#ffffff
49.9%,#000000 50%,#000000 51%,#ffffff 51.1%,#ffffff 100%);
}
td.diagonalFalling
{
background: linear-gradient(to right top, #fff 0%,#fff 49.9%,#000000
50%,#000000 51%,#fff 51.1%,#fff 100%);
}
<style>
上述链接将进一步为您解释。它还支持所有方向,垂直、水平或对角线。
backgroundImage: 'linear(to bottom right, black)'
(在React样式属性中)呢?