【发布时间】:2014-10-16 18:08:03
【问题描述】:
我有一个带有两个 <div class="sku"> 元素的 aside。我正在尝试使用 CSS 来操作 :first-child 但它不起作用。但是,当尝试访问 :last-child 时,它确实如此。
HTML
<aside>
<h1>Product Name</h1>
<div class="sku">
<h3>
100 – Small
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
23.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
<div class="sku">
<h3>
200 – Large
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
29.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
</aside>
CSS
.sku:first-child {
display:none !important; /*doesn't hide the first child*/
}
.sku:last-child {
display:none !important; /*does hide the first child*/
}
:first-child 为什么不选择第一个 div?
【问题讨论】:
-
请分享您的代码以及您想要完成的工作。如果没有更多信息,告诉我们某些事情不起作用是没有帮助的。
-
这是因为
.sku不是aside元素的第一个 子元素。 CSS 伪类,例如:first-child、:last-child、nth-child()、...查看父级的子树以匹配正确的子元素,而不是element.class的组合。 -
dev.sku不是第一个孩子——h3是。请改用:first-of-type。
标签: css html css-selectors