CSS :first-child selector

标签 css css-selectors

为什么两个链接都用这个设置加粗?

我希望只有 li.current 正下方的 a-tag 变粗。

CSS

.ul li.current a:first-child{
    font-weight:bold;
}

HTML

<ul>
  <li class="current">
    <a href="">This link</a>
      <ul>
        <li>
          <a href="">Not this</a>
        </li>
      </ul>
  </li>
</ul>

最佳答案

使用:

ul li.current > a:first-child{
    font-weight:bold;
}

jsFiddle example

> 指的是直接子代。因此,当您的规则应用于任何作为 child 的 anchor 时,上述规则仅适用于 ul li.current 的直接 child 。请注意,您也可以删除 :first-child 部分,具体取决于您希望如何格式化链接。

另请注意,在您的代码中有 .ul,您可能只是想要 ul,就像在我的示例中一样。

关于CSS :first-child selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585773/

相关文章:

css - 如何在 IE8 下修复工具提示的宽度

html - Div 在另一个 div 上溢出

javascript - jQuery:获取 jQuery 中隐藏元素的高度

css - 如何匹配有特定 child 的 parent ?

jquery - 图像无响应或无法调整为最大宽度值

javascript - 垂直滚动条适用于 chrome 但不显示

html - 无法加载资源: the server responded with a status of 404 (Not Found) http://localhost:5266/Content/fonts/glyphicons-halflings-regular. woff2

css - 仅选择不具有已定义类的父元素的元素

css - 如何在 CSS 中为无限序列重复第 n 个子规则

html - 使用CSS选择没有兄弟的嵌套第一个子元素