CSS:穿线颜色与文本颜色不同?

发布于 2024-10-06 05:06:04 字数 131 浏览 5 评论 0原文

我想要带有红色删除线的灰色文本,但这种样式不起作用:

color: #a0a0a0;
text-decoration: line-through 3px red; 

我做错了什么?

I’d like to have gray text with a red strike-through, but this style doesn’t work:

color: #a0a0a0;
text-decoration: line-through 3px red; 

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没有你我更好 2024-10-13 05:06:04

您可以使用两个嵌套元素来模拟所需的效果,例如:

        span.inner {
            color: green;
        }
        span.outer {
            color: red;
            text-decoration: line-through;
        }
<span class="outer">
    <span class="inner">foo bar</span>
</span>

jsfiddle

You can simulate the desired effect with two nested elements, e.g.:

        span.inner {
            color: green;
        }
        span.outer {
            color: red;
            text-decoration: line-through;
        }
<span class="outer">
    <span class="inner">foo bar</span>
</span>

jsfiddle

我不咬妳我踢妳 2024-10-13 05:06:04

没有额外的 DOM(但由于明显的原因可能不适用于某些布局):

<style type="text/css">
    .strikethrough {
        position: relative;
    }

    .strikethrough:before {
        border-bottom: 1px solid red;
        position: absolute;
        content: "";
        width: 100%;
        height: 50%;
    }
</style>

<span class="strikethrough">Foo Bar</span>

这里有一个 jsfiddle 示例。

With no extra DOM (but may not work for some layouts for obvious reasons):

<style type="text/css">
    .strikethrough {
        position: relative;
    }

    .strikethrough:before {
        border-bottom: 1px solid red;
        position: absolute;
        content: "";
        width: 100%;
        height: 50%;
    }
</style>

<span class="strikethrough">Foo Bar</span>

A jsfiddle example here.

南巷近海 2024-10-13 05:06:04

还有另一种方式来看待 CSS2 规范的含义;也就是说,外部文本装饰将设置“line-through”和文本的颜色,但内部颜色声明(在“span”标签中)可用于重置文本颜色。

<p style="text-decoration:line-through;color:red">
<span style="color:#000">
The "line-through" and "color" are declared in 'p', and the inner 'span' 
declares the correct color for the text.
</span>
</p>

There is another way of looking at the meaning of the CSS2 specification; and that is the outer text-decoration will set the color of the "line-through" and text, but an inner color declaration (in a 'span' tag) can be used to reset the text color.

<p style="text-decoration:line-through;color:red">
<span style="color:#000">
The "line-through" and "color" are declared in 'p', and the inner 'span' 
declares the correct color for the text.
</span>
</p>
听,心雨的声音 2024-10-13 05:06:04

不可能用不同的颜色进行穿线。它将采用您使用属性 color 定义的颜色。

请参阅http://www.w3.org/TR/CSS2/ text.html#lined-striking-props

编辑

我想到的是使用带有您喜欢的颜色的 1px * 1px 颜色点的背景图像。

CSS:

.fakeLineThrough {
  background-image: url(lineThroughDot.gif);
  background-repeat: repeat-x;
  background-position: center left;
}

HTML:

<span class="fakeLineThrough">the text</span>

It's not possible to make a line-through with a different color. It will be in the color you define with property color.

see http://www.w3.org/TR/CSS2/text.html#lining-striking-props

EDIT:

what came into my mind is to use a background-image with a 1px * 1px color dot in the color you like.

CSS:

.fakeLineThrough {
  background-image: url(lineThroughDot.gif);
  background-repeat: repeat-x;
  background-position: center left;
}

HTML:

<span class="fakeLineThrough">the text</span>
喵星人汪星人 2024-10-13 05:06:04

CSS2 规范说

文本装饰所需的颜色必须源自设置了“text-decoration”的元素的“color”属性值。即使后代元素具有不同的“颜色”值,装饰的颜色也必须保持相同

我想这意味着你不能这样做。

解决方法可能是使用某种边框并解除它?

The CSS2 specs says

The color(s) required for the text decoration must be derived from the 'color' property value of the element on which 'text-decoration' is set. The color of decorations must remain the same even if descendant elements have different 'color' values

I guess that means that you can't do it that way.

A workaround could be using some kind of border, and lifting it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文