Skip to main content

结构伪类 only-child

  • :only-child,是父元素中唯一的子元素。
  • CSS 伪类 :only-child 匹配没有任何兄弟元素的元素。等效的选择器还可以写成 :first-child:last-child 或者:nth-child(1):nth-last-child(1),当然,前者的权重会低一点。
/* Selects each <p>, but only if it is the */
/* only child of its parent */
p:only-child {
background-color: lime;
}
备注

在起初定义,被选择的元素必须拥有一个父级元素。从 Selectors Level 4 开始,这不再是必须的了。


Loading Github Gist ...
Details

Loading Github Gist ...
Details