CSS 和特异性 - 类与 ID
考虑以下 html:
<div id="rightBar">
<div class="barElement">
<div class="barText"><a href="#">Not underlined</a><br /></div>
<div class="barLinks"><a href="#">Should be underlined</a></div>
</div>
</div>
和以下 css:
#rightBar a
{
text-decoration: none;
}
#rightBar a.barLinks
{
text-decoration: underline;
}
“应该加下划线”链接没有下划线,不应该这样,因为它继承了类 barLinks 和 id rightbar。另外 '#rightBar a.barLinks' (0, 1, 1, 1) 比 '#rightBar a' (0, 1, 0, 1) 具有更多的特异性,所以它应该覆盖后者,对吗?
我还能如何让“应该加下划线”链接加下划线而不诉诸使用任何内联规范(CSS 或 html)
Consider the following html:
<div id="rightBar">
<div class="barElement">
<div class="barText"><a href="#">Not underlined</a><br /></div>
<div class="barLinks"><a href="#">Should be underlined</a></div>
</div>
</div>
And the following css:
#rightBar a
{
text-decoration: none;
}
#rightBar a.barLinks
{
text-decoration: underline;
}
The 'Should be underlined' link is not underlined, shouldn't it be since it inherits both the class barLinks and the id rightbar. Also the '#rightBar a.barLinks' (0, 1, 1, 1) has more specificity than '#rightBar a' (0, 1, 0, 1), so it should override the latter right?
How else can I get the 'Should be underlined' link to be underlined without resorting to using any inline specifications (both css or html)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的
a
元素没有类barLinks
。执行此操作:示例: http://jsfiddle.net/J34mj/2/
Your
a
element does not have the classbarLinks
. Do this:example: http://jsfiddle.net/J34mj/2/
这不是特异性问题,而是您使用了错误的选择器。应该是这样的:
It's not a specificity issue, you are using the wrong selector. It should be this:
不起作用,因为 class="barLinks" 不在
上
试试这个;
或者失败了;
Won't work because the class="barLinks" isn't on the
<a>
Try this;
Or failing that;
不应该, barLinks 仅适用于标签,如果您将 css 更改为 #rightBar .barLinks a 它应该可以工作。
It shouldn't be, the barLinks is only applicable to a tags, if you change your css to #rightBar .barLinks a it should work.
因此,我在这里看到的问题是,您提到了使用 CSS 选择器访问锚标记元素的错误地址。如果您要寻找更长、更突出的地址路线,那么它应该如下所示:
但是由于您正在寻找更具体的地址路线,因此您的方法是正确的,除了锚标记没有自己的类这一事实,因此 a.barLink 将找不到任何内容。它应该是这样的:
So the problem I see here is that you have mentioned an incorrect address to reach to your anchor tag element using the CSS selector. If you are going for a longer and more prominent address route then it should be as follows:
But since you are looking for a more specific one, your approach was correct except for the fact the anchor tag does not have a class of its own, therefore a.barLink would find nothing. It should rather be as :