整个 HTML 表格行的穿线/删除线
经过一番研究,我找不到这个问题的答案。有这个,但它并没有真正回答我的问题。我想在 CSS 中“删除”完整的 HTML 表格行,而不仅仅是其中的文本。这有可能吗?从我链接的示例来看,tr 样式似乎在 Firefox 中不起作用。 (无论如何,文本装饰仅适用于文本afaik)
After some research, I couldn't find an answer to this question. There was this but it didn't really answer my question. I would like to "strikethrough" a complete HTML table row in CSS, not just the text in it. Is it at all possible? From the example that I linked, it seems tr styling doesn't even work in Firefox. (And anyway, text-decoration only applies on text afaik)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不确定,但似乎还有其他答案提到了简单直接的纯 CSS 解决方案...
@Ben Slade 的答案是最接近的,但仍然...
只需使用
text-decoration: line-through
在你的 CSS 中!添加相应的类,然后使用!
Not sure but it seems there were other answers mentioning simple and straightforward pure CSS solution...
@Ben Slade's answer is the closest of all, but still...
Just use
text-decoration: line-through
in your CSS! Add corresponding class and then use<tr class="strikethrough">
!我的回答(如下)说这是不可能的。正如@NicoleMorganErickson 所指出的,我错了。请参阅她的回答(并投票!)了解如何做到这一点。简而言之,您使用
:before
伪类创建一个元素,该元素在内容上方的单元格中间绘制边框:(原始答案)
不,它仅使用 CSS 和语义表标记是不可能的。正如 @JMCCreative 所建议的,在视觉上可以使用多种方法在行上放置一条线。
我建议使用
color
、background-color
、font-style:italic
和/或text-decoration 的组合: line-through 使整行明显不同。 (我个人强烈将文本“淡出”为比普通文本更接近背景的颜色,并将其设置为斜体。)
My answer (below) said that it is not possible. I was wrong, as pointed out by @NicoleMorganErickson. Please see her answer (and upvote it!) for how to do it. In short, you use
:before
pseudo-class to create an element that draws a border across the middle of the cell, above the content:(original answer)
No, it is not possible using only CSS and your semantic table markup. As @JMCCreative suggests, it is possible visually using any number of ways to position a line over your row.
I would instead suggest using a combination of
color
,background-color
,font-style:italic
and/ortext-decoration:line-through
to make the entire row obviously different. (I'd personally strongly 'fade out' the text to a color much closer to the background than normal text and make it italic.)我使用 http://www.patternify.com/ 生成 1x1 图像网址。
I used http://www.patternify.com/ to generate the 1x1 image url.
编辑:正如 @Mathieu M-Gosselin 在评论中指出的那样,这实际上将线条放在文本后面。也就是说,如果您的线条与文本颜色相同或者您使用的是小-ish 字体,这仍然效果很好。
无论如何,这是一种非常有效的方法,可以在纯 CSS 中完成此操作,而无需使用伪元素。您可以通过调整
background-size
来更改删除线的粗细。EDIT: As pointed out by @Mathieu M-Gosselin in the comments, this actually puts the line behind the text. That said, if your line is the same color as your text or you are using a small-ish font, this still works pretty well.
For what it's worth, here's a pretty effective way to do it in pure CSS without using pseudo elements. You can change the thickness of the strikethrough line by adjusting the
background-size
.编辑
在最近的 Bootstrap 4.3 ServiceNow Angular.js 项目中,我发现自己必须进行一些更改,而是使用了以下 CSS,类似于 Revoman 的体验:
原始帖子
我喜欢 Nicole Morgan Erickson 的答案,但它可能会导致副作用如果你逐字执行他的解决方案。我在下面添加了一些小调整来保持这个整洁……这样我们就不会使用这个 CSS 全局修改每个表或每个 td。
我还想在行上有一个按钮来删除该行,但为了可见性,我不想用该按钮删除该列。我只是想删除该行的其余部分。为此,我使得每个想要能够显示删除的列都必须通过也用类标记来声明这一点。在此迭代中,您需要将表标记为可删除,并将每个 td 标记为可删除;但是,您可以通过不对任何不可删除的表产生副作用来获得安全性,并且可以控制删除哪些列。
CSS:
用法:
最后,由于我将其与 Bootstrap 一起使用,并将删除视为危险的事情,因此我对颜色进行了一些格式化以匹配我的用途。
Edit
In a recent Bootstrap 4.3 ServiceNow Angular.js project, I found myself having to make some changes, and instead used the following CSS, similar to the experience of Revoman:
Original Post
I like Nicole Morgan Erickson's answer, but it might cause side effects if your implement his solution verbatim. I've add some small tweaks to keep this kosher, below... so that we're not globally modifying every table or every td with this CSS.
I also wanted a button on the row to strike out the row, but I didn't want to strike out the column with the button, for visibility sake. I just wanted to strike out the rest of the row. For this, I made it so that every column that wants to be capable of showing the strike out must declare such by also being marked with a class. In this iteration, you'd need to mark the table as strike-able, and also mark each td as strike-able; but you gain safety by not side effecting any non-strike-able tables, and you gain control of which columns to strike out.
CSS:
Usage:
Lastly, since I'm using this with Bootstrap, and treating the deletions as a dangerous thing to do, I've formatted the colors a little to match my use.
@NicoleMorganErickson,我喜欢你的答案,但我无法让删除线仅影响应用的行。另外,我需要将它应用到多行,因此我将您的解决方案修改为单个类。
CSS:
http://codepen.io/anon/pen/AaFpu
@NicoleMorganErickson, I like your answer, but I could not get the strikeout to affect only the applied row. Also, I needed it to be applied multiple rows so I modified your solution down into a single class.
CSS:
http://codepen.io/anon/pen/AaFpu
是的,你可以。在该行的第一个单元格中创建一个包含 HR 的 div。将 div 向左浮动,并将其宽度指定为其包含元素(在本例中为表格单元格)的 %。它将在该行中的表格单元格上拉伸到您想要的宽度,如果您愿意,甚至可以超出表格的宽度。
这对我有用:
要控制线条的宽度,您必须指定包含 HR 的表格单元格的宽度。对于 HR 元素的样式,他们说您不应使其高度小于 3 像素。
Yes you can. In the first cell of the row you create a div containing a HR. Float the div to the left and specify its width as a % of its containing element, in this case the table cell. It'll stretch as wide as you want across the table cells in that row, even beyond the width of the table if you want.
This works for me:
To control the width of your line you have to specify the width of the table cell containing the HR. For styling HR elements they say you shouldn't make it less than 3px in height.
这是一个对我有用的非常简单的方法:
Here's a very simple way that worked for me:
哦,是的,是的!
CSS:
HTML:
http://codepen.io/nericksx/pen/CKjbe
Oh yes, yes it is!
CSS:
HTML:
http://codepen.io/nericksx/pen/CKjbe