CSS 和特异性 - 类与 ID

发布于 2024-11-18 05:43:10 字数 682 浏览 4 评论 0原文

考虑以下 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 技术交流群。

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

发布评论

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

评论(5

╰◇生如夏花灿烂 2024-11-25 05:43:10

您的 a 元素没有类 barLinks。执行此操作:

#rightBar .barLinks a
{
    text-decoration: underline;
}

示例: http://jsfiddle.net/J34mj/2/

Your a element does not have the class barLinks. Do this:

#rightBar .barLinks a
{
    text-decoration: underline;
}

example: http://jsfiddle.net/J34mj/2/

喜爱纠缠 2024-11-25 05:43:10

这不是特异性问题,而是您使用了错误的选择器。应该是这样的:

#rightBar .barLinks a {}

It's not a specificity issue, you are using the wrong selector. It should be this:

#rightBar .barLinks a {}
樱花细雨 2024-11-25 05:43:10
#rightBar a.barLinks
{
    text-decoration: underline;
}

不起作用,因为 class="barLinks" 不在

试试这个;

#rightBar .barLinks a
{
    text-decoration: underline;
}

或者失败了;

#rightBar .barLinks a
{
    text-decoration: underline !important;
}
#rightBar a.barLinks
{
    text-decoration: underline;
}

Won't work because the class="barLinks" isn't on the <a>

Try this;

#rightBar .barLinks a
{
    text-decoration: underline;
}

Or failing that;

#rightBar .barLinks a
{
    text-decoration: underline !important;
}
梦巷 2024-11-25 05:43:10

不应该, 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.

写下不归期 2024-11-25 05:43:10

因此,我在这里看到的问题是,您提到了使用 CSS 选择器访问锚标记元素的错误地址。如果您要寻找更长、更突出的地址路线,那么它应该如下所示:

#rightbar .barElement .barLinks a{
    text-decoration : underline;
}

但是由于您正在寻找更具体的地址路线,因此您的方法是正确的,除了锚标记没有自己的类这一事实,因此 a.barLink 将找不到任何内容。它应该是这样的:

#rightBar .barLinks a{
text-decoration : underline;
}

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:

#rightbar .barElement .barLinks a{
    text-decoration : underline;
}

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 :

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