CSS 和特异性 - 类 vs id

标签 css class css-specificity text-decorations

考虑以下 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;
}

'Should be underlined' 链接没有带下划线,不应该是因为它继承了类 barLinks 和 id rightbar。此外,'#rightBar a.barLinks' (0, 1, 1, 1) 比 '#rightBar a' (0, 1, 0, 1) 具有更高的特异性,因此它应该覆盖后者,对吗?

在不使用任何内联规范(css 或 html)的情况下,我还能如何让“应该加下划线”链接加下划线

最佳答案

您的 a 元素没有类 barLinks。这样做:

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

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

关于CSS 和特异性 - 类 vs id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550296/

相关文章:

html - 如何仅输出带值的下拉菜单(并给它们命名)- Contact Form 7

javascript - 使用 JS 和 iframe 将样式标签附加到头部

c++ - 根据重复次数对输入文本中的单词进行排序?

java - 如何从java中的try catch函数返回一个值?

ID 内的 CSS 类胜出

css - CSS 特异性如何影响网站性能和页面加载时间

html - 在 div 中无缝连接两张图片

javascript - 如何制作一个动态的 Canvas /网格/行/列,以适应浏览器的宽度和高度,同时保持纵横比?

python - 如何在类参数中调用函数

html - 列表项的 CSS 颜色没有改变