我有一个带有两个
为什么
<div class="sku">
元素的 aside
。我试图使用CSS操纵 :first-child
,但它不起作用。然而,尝试访问 :last-child
时确实起作用。
JSFiddle
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()
等)会查找父元素的子元素树以匹配正确的子元素,而不是元素.类
的组合。 - Hashem Qolamidev.sku
不是第一个子元素,h3
是。请尝试使用:first-of-type
。 - wolfemm