如何改变CSS中的删除线/穿线粗细?
我在 CSS 中使用 text-decoration: line-through
,但我似乎找不到任何方法来改变线条粗细,而无需像
这样的不优雅的黑客手段。代码> 或图像覆盖。
有没有优雅的方法来指定穿线的粗细?
I'm using the text-decoration: line-through
in CSS, but I can't seem to find any way to vary the line thickness without inelegant hacks like <hr>
or image overlays.
Is there any elegant way to specify the thickness of a line-through?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
现代解决方案是使用 text-decoration-thickness< /a> 属性。
您还可以使用 text-decoration-color 更改颜色 属性。
对于较旧的浏览器,您可以使用以下解决方法之一:
这是一个纯 CSS 方法,不需要任何不必要的包装元素。作为额外的好处,您不仅可以调整删除线的粗细,还可以与文本颜色分开控制其颜色:
使用 RGBa 颜色使删除线半透明:
或者甚至使三振成为渐变!只需使用
背景
与高度
组合来代替边框
:这适用于 IE9(无渐变)及更高版本 - 甚至 IE8,如果您使用单冒号
:after
语法并手动写入负margin-top
值而不是使用calc()
。主要缺点是这只适用于单行文本。但是,嘿,你拿走你能得到的;-)
The modern solution is to use the text-decoration-thickness property.
You can also change the color using the text-decoration-color property.
For older browsers, you can use one of these workarounds:
Here's a pure CSS method that doesn't require any unnecessary wrapper elements. As an added bonus, not only can you adjust the thickness of the strikeout, but you can control its color separately from the text color:
Use RGBa colors to make the strikeout semi-transparent:
Or even make the strikeout a gradient! Just use a
background
combined with aheight
, in place of aborder
:This works in IE9 (sans gradient) and up – or even IE8 if you use the single-colon
:after
syntax and manually write the negativemargin-top
value instead of usingcalc()
.The main downside is that this only works on a single line of text. But hey, you take what you can get ;-)
这似乎是一个长期存在的问题,没有多行删除线的理想解决方案。
方便地,使用 CSS 渐变,您可以轻松调整线条粗细,如下所示:
在此处查看演示和完整的供应商前缀:http://codepen .io/pearlchen/pen/dhpxu
This seems to be a longstanding question without an ideal solution for multi-line strikethroughs.
Conveniently, using CSS gradients, you can easily adjust your line thickness like so:
See the demo and full vendor prefixing here: http://codepen.io/pearlchen/pen/dhpxu
简短的回答:不。这取决于字体,下划线的粗细也是一样的——它随着文本的粗细而变化
short answer: no. it depends on the font, it's the same for the thickness of underline—it changes with the thickness of the text
如今这非常简单,使用以下 CSS
您可以根据需要调整厚度。
可以像这样改变颜色
主要浏览器支持
This is super easy these days, Use the following CSS
You can adjust the thickness according to your needs.
Can change the color like this
Supported by main browsers
我有一个想法,但需要为每个厚度级别添加一个额外的元素。
html
css
顺便说一句,第二个跨度中的空格是特殊的 - 您必须使用 alt+0160 或 alt+255。
当您尝试精确定位时,您也可以在负顶部使用像素单位。
还有另一种选择,首先使用 text-decoration,然后使用
或
样式,看看是否可以微移垂直放置而不移动文本。html
css
两者都在这里工作正常,但请记住使用过渡文档类型,因为
已被弃用。I have an idea but it would require adding an additional element for each level of thickness.
html
css
BTW the spaces in the second span are specials - you will have to use alt+0160 or alt+255.
You can use pixels unit too on the negative top when ull try to position it precisely.
There is another alternative which involve using first text-decoration and then style
<strike>
or<del>
and see if you can nudge it vertically without moving the text with it.html
css
Both are working fine here, but remember to use a transitional doctype cause
<strike>
has been deprecated.我找到了另一种多行文本方法:
http://output.jsbin.com/weqovenopi/1/
此方法假设重复图像(1px 宽度和 npx 高度)。它的工作方式也与字体大小无关。
只有一个缺点 - 背景呈现在文本下方。
I've found another approach for multiline text:
http://output.jsbin.com/weqovenopi/1/
This approach assumes repeating an image (1px width and npx height). Also it works independent on the font-size.
Only one disadvantage - background renders under the text.
不会。
但是,如果删除线颜色与文本颜色相同,您可以轻松地在背景中使用自定义图像。
如果您需要不同的颜色,那么覆盖自定义删除线图像是唯一的方法。
No.
However, if the strike-through color is the same as the text color, you can easily get away with using a custom image in the background.
If you require different colors, then overlaying the custom strike-through image is the only way to go.
线条粗细由字体(系列、大小等)决定。 CSS 中没有规定可以更改此 http://www.w3。 org/TR/REC-CSS1/#text-decoration
The line thickness is determined by the font (family, size, etc.). There is no provision in CSS for changing this http://www.w3.org/TR/REC-CSS1/#text-decoration
我意识到这已经很旧了,但是有一种方法可以使用嵌套跨度标签来做到这一点:
删除线取决于字体的大小,因此如果将外部跨度加倍,则线条的粗细将增加两倍。然后,你需要将内部的一减半。垂直对齐是必要的,否则该线太高,使其看起来几乎是一条上线。
实际应用:http://jsfiddle.net/moodleboy/deep3qw8/
适用于
Chrome< /strike>/FF,但不适用于 Safari、IE10 或 Opera。适用于 Mac 上的 Chrome,但不适用于 Windows。I realize this is old, but there is a way to do it using nested span tags:
Strikethrough is dependent upon the size of the font, so if you double the outer span it will make the line twice as thick. Then, you need to reduce the inner one by half. The vertical-align is necessary or else the line is too high, making it appear to almost be an overline.
In action: http://jsfiddle.net/moodleboy/deep3qw8/
Works in
Chrome/FF, but not Safari, IE10 or Opera. Works on Chrome on Mac, but not Windows.这并没有回答问题,但相关的是它解决了使用脚本缺乏独特的删除线的问题。我不是纯粹主义者,但我相信这是一个 x 浏览器解决方案。
This does not answer the question, but is relevant in that it solves the lack of a unique strike-through using scripting. I am not a purist, but I believe this is a x-browser solution.
我在这里找不到合适的方法,所以我使用了
带有 线性渐变 和 < 的背景图像a href="https://developer.mozilla.org/en-US/docs/Web/CSS/length#Units" rel="nofollow noreferrer">ex CSS 长度单位 。
不幸的是,这意味着使用不同的字体将在稍微不同的位置呈现删除线(例如,如果字体具有不同的 x 高度)。
I couldn't find an appropriate method here so I used
background-image with a linear-gradient and ex CSS length units.
Unfortunately this means that using different font faces will render the strikethrough in a slightly different position (if the fonts have different x-heights for example).