css 选中伪类与缺省伪类

sh7euo9m  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(129)

checkeddefault伪类有什么区别,因为它们看起来很相似?我很困惑。请有人解释一下这两个属性之间的区别。

* {
  border: none;
}

:checked {
  outline: 3px solid orange;
}

:default {
  outline: 3px solid blue;
}
<input type="checkbox" checked/>
<!-- While :default pseudo-class defined last in CSS file this style win, when :checked pseudo-class defined in CSS file this last style win -->
bf1o4zei

bf1o4zei1#

:default不是必须使用的。它定义了一个默认值。:checked仅在元素被选中时适用。问题是,您最后使用:default也会覆盖之前的:checked选择器。如果您交换它们,它将工作:

* {
  border: none;
}

:default {
  outline: 3px solid blue;
}

:checked {
  outline: 3px solid orange;
}
<input type="checkbox" checked/>
<!-- While :default pseudo-class defined last in CSS file this style win, when :checked pseudo-class defined in CSS file this last style win -->

相关问题