css - :last-child pseudo class confusion

标签 css css-selectors pseudo-class

this JSFiddle ,最后一个 .tab 类没有得到正确的 border-radius 效果(右上角圆 Angular )。

我认为我的逻辑是正确的,在这种情况下,:last-child 选择 .tabbed 的最后一个 .tab

我做错了什么?

CSS:

body {
    background: black;
    color: white;
    padding: 5px; }

.tabbed {
    height: 550px;
}

.tabbed .tab {
    padding: 6px 14px;
    background: rgba(255,255,255,0.25);
    border: 1px solid rgba(255,255,255,0.4);
    border-radius: 0px;
    border-left-width: 0;
    float: left;
}

.tabbed .tab:first-child {
    border-radius: 3px 0 0 0;
    border-left-width: 1px;
}

.tabbed .tab:last-child {
    border-radius: 0 3px 0 0;
}

HTML:

<ul class='tabbed'>
    <li class='tab'>Menu 1</li>
    <li class='tab'>Menu 2</li>
    <li class='tab'>Menu 3</li>
    <li class='tab'>Menu 4</li>
    <li class='tab'>Menu 5</li>
    <li> <br/><br/> </li>
    <li class='dummy'>Content 1</li>
    <li class='dummy'>Content 2</li>
    <li class='dummy'>Content 3</li>
    <li class='dummy'>Content 4</li>
    <li class='dummy'>Content 5</li>
</ul>

最佳答案

根据这个documentation :

The :first-child pseudo class means "if this element is the first child of its parent". :last-child means "if this element is the last child of its parent". Note that only element nodes (HTML tags) count, these pseudo-classes ignore text nodes.

另请参阅 W3C doc

它不适用于您的情况,因为最后一个 .tab 不是父 ul 的最后一个 child 。

如果您创建两个列表,则会按预期应用 css。

关于css - :last-child pseudo class confusion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693357/

相关文章:

html - CSS: Overflow:Hidden - 隐藏特定元素和显示其他元素的问题

php - 如果包含特定文本,则隐藏 RSS 提要

css - 在 Bootstrap 面板中浏览器忽略了第 nth-child

css - :hover pseudoclass doesn't work with universal selector *

border - CSS中的圆帽下划线

html - 对齐div中的内容

css - 使用CSS选择dom元素选择包含大于X的数字的类名

css - 第 N 个子元素定位到错误的元素

css - 如何为 ':hover' 和 'a:before' 编写 'a:after' 条件?

css - 使用 :target pseudo-class 时出现打印问题