css - 为什么是:last-child doesn't work when we have more elements inside?

标签 css css-selectors

查看此示例 http://jsfiddle.net/jwmCd/

HTML

<div class="round-buttons">
                <a href="#">hello</a>
                <a href="#">world</a>
    <form method="get" action="/search" id="search">
  <input name="q" type="text" size="40" placeholder="Search...">
</form>
</div>

CSS

.round-buttons a:first-child {background:red}

.round-buttons a:last-child {background:green}

.round-buttons a:last-child {background:green} 不适用于这种情况。

动态地可能有更多链接,我想为第一个和最后一个 anchor 提供不同的样式。

最佳答案

:last-child将仅匹配父元素的最后一个元素,而不管其类型如何(在您的示例中, :last-child 将仅匹配 <input> )。使用 :last-of-type匹配父级中特定类型的最后一个元素,例如:

.round-buttons a:last-of-type {background:green}

关于css - 为什么是:last-child doesn't work when we have more elements inside?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776848/

相关文章:

javascript - 引导多元素旋转木马一次移动一个元素点击?

css - 访问 HTML5 游戏应用程序 Sprite 表的最快方法

javascript - -webkit-溢出-滚动 : auto; not working on touch devices

css - 列表的最后一个子伪类不起作用

python - BeautifulSoup4/CSS选择器如何选择

javascript - 如何根据点击切换图标?

css - 你能让 CSS 样式在复制/粘贴时保持不变吗?

java - 如何通过 Selenium 和 Java 单击“停用”按钮,然后单击“激活”按钮

html - 如何正确使用:not selector to solve . b而不是.a .b?

html - 选择器 :nth-child without a break?