dd上的第一个孩子选择器不工作,为什么?第一个孩子选择器不能与dd一起工作
<dl>
<dt>definition term</dt>
<dd>description</dd>
<dd>description</dd>
<dd>description</dd>
</dl>
dd:first-child{
/*styling here not work*/
}
dd上的第一个孩子选择器不工作,为什么?第一个孩子选择器不能与dd一起工作
<dl>
<dt>definition term</dt>
<dd>description</dd>
<dd>description</dd>
<dd>description</dd>
</dl>
dd:first-child{
/*styling here not work*/
}
您应该使用:first-of-type
伪类来代替。
dd:first-of-type {
background-color: gold;
}
这是因为<dd>
不是的第一个孩子其父母。
element:first-child
代表的第一个孩子其父母匹配element
。在这个特殊情况下,<dl>
元素的第一个子元素是<dt>
元素;不是<dl>
。
从MDN:
的
:first-of-type
CSS伪类表示第一个同级 其在它的父元素的儿童名单型。
哪个术语类型引用HTML元素类型。因此dd:first-of-type
在其父代的子树中选择第一个<dd>
元素。
或者,在这种特殊情况下,您可以使用adjacent sibling selector作为:dt + dd
来选择第一个<dd>
元素。 (Demo)。