如何让 CSS 文本装饰覆盖起作用?
有些日子我发誓我要疯了。这是那些日子之一。我认为我的 CSS 在这里相当简单,但它似乎不起作用。我缺少什么?
我的 CSS 看起来像这样:
ul > li {
text-decoration: none;
}
ul > li.u {
text-decoration: underline;
}
ul > li > ul > li {
text-decoration: none;
}
ul > li > ul > li.u {
text-decoration: underline;
}
我的 HTML 看起来像这样:
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
但它是这样的:
Some days I swear I'm going mad. This is one of those days. I thought my CSS was fairly straight-forward here, but it just doesn't seem to be working. What am I missing?
My CSS looks like this:
ul > li {
text-decoration: none;
}
ul > li.u {
text-decoration: underline;
}
ul > li > ul > li {
text-decoration: none;
}
ul > li > ul > li.u {
text-decoration: underline;
}
And my HTML looks like this:
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
Yet it comes up like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
text-decoration
的行为与其他与字体/文本相关的样式(如font-weight
)不同。应用text-decoration
也会影响所有嵌套元素。看看这个:
http://www.w3.org/TR/CSS21/text .html#propdef-text-decoration
摘录:
我从以下位置获取信息: http://csscreator.com/node/14951
text-decoration
does not behave the same as other font/text related styling likefont-weight
. Applyingtext-decoration
will affect all nested elements as well.Check this out:
http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
Excerpt:
I've got the info from: http://csscreator.com/node/14951
在这些情况下,您可以摆脱应用于父元素的
text-decoration
:不流动的元素,例如浮动元素和绝对定位元素
原子内联级元素,例如内联块和内联表格
但是如果您使用
li{display:inline-block}
,那么您就没有项目符号(您会丢失display:list-item
)并且项目会出现一个挨着一个。然后,要每行一个项目,您可以使用
要添加项目符号,您可以使用
::before
伪元素。但是,项目符号不应该带有下划线,因此您需要将它们排除在外,或者将它们设置为原子内联级别。此行为在 CSS 2.1 和 CSS 文本装饰模块级别 3:
You get rid of
text-decoration
applied to a parent element in those cases:Out-of-flow elements, such as floated and absolutely positioned ones
Atomic inline-level elements, such as inline blocks and inline tables
But if you use
li{display:inline-block}
, then you don't have bullets (you losedisplay:list-item
) and the items appear one next to the others.Then, to have one item per line, you can use
And to add the bullets, you can use
::before
pseudo-elements. However, bullets shouldn't be underlined, so you will need to take them out-of-flow or make them atomic inline-level too.This behavior is specified in CSS 2.1 and CSS Text Decoration Module Level 3:
okw的回答上面完美地解释了为什么如果不做一些其他改变就不能做你所要求的事情。不,你不会生气!
可能的解决方法:
border-bottom
?span class="u"
标记中? (为了防止文本装饰装饰嵌套元素)祝你好运!
o.k.w.'s answer above explains perfectly why you can't do what you are asking without some other changes. No, you're not going mad!
Possible workarounds:
border-bottom
?span class="u"
tag? (to prevent the text-decoration from decorating nested elements)Best of luck!
您看到所看到内容的原因是您的规则
优先于:
因为指定了一个类,并且该类的权重比元素选择器总和的权重更大。
编辑:你可以尝试的是:
并尝试一下(也许你必须使用li.u而不仅仅是.u) 。
但是,根据内容,您可能希望将带下划线的部分包含在 q、em 或 strong 标签中,并且设置这些标签的样式而不是使用类。这样你就可以描述你的内容并设计它的样式。
The reason you´re seeing what you're seeing is that your rule
takes preference over:
as a class is specified and that has more weight than the element selectors together.
Edit: What you could try is:
and play around with that (perhaps you will have to use li.u instead of just .u).
However, depending on the content you might want to wrap the underlined parts in q, em or strong tags and style these tags instead of using a class. That way you would be describing your content as well as styling it.
我在使用外部主题/CSS 时遇到了类似的问题,因此我无法修改它以删除
text-decoration: none;
。就我而言,我有一个带有链接的子元素,但该链接没有按预期加下划线。我尝试使用display: inline-block;
正如其他人提到的,但它没有效果。对我有用的是像您所做的那样覆盖
text-decoration
,但还包括!important
来强制它覆盖父级的text-decoration
>。因此,对于您的特定示例,我想这可能会起作用(我没有测试它来确认):
I ran into a similar issue when using an external theme/CSS, so I couldn't modify it to remove the
text-decoration: none;
. In my case, I had a child element with a link, but the link wasn't being underlined as expected. I tried usingdisplay: inline-block;
as others mentioned, but it has no effect.What worked for me was overriding the
text-decoration
as you had done, but also including!important
to force it to override the parent'stext-decoration
.So for your particular example, I imagine this may do the trick (I did not test it to confirm):
我发现的最简洁的方法是将下划线设置为背景颜色。
The cleanest approach I’ve found is just to set the underline to the background’s colour.