← 返回完整题库
CSS简单

css选择器有哪些?优先级分别是什么?哪些属性可以继承?

参考答案

选择器

  • 基础:*、标签、.class、#id、[attr]、::pseudo-element
  • 组合:后代(空格)、子 >、相邻 +、通用兄弟 ~
  • 分组:,
  • 逻辑伪类::not()、:is()、:where()、:has()

优先级(从高到低)

  1. !important
  2. 内联样式
  3. ID 选择器(100)
  4. class / 属性 / 伪类(10)
  5. 标签 / 伪元素(1)
  6. 通配(0)

同权重比出现顺序,后者胜。:where() 权重为 0,:is()/:not() 取参数中最高者。

可继承属性

  • 文本:color、font 系列、line-height、text-align、text-indent、letter-spacing、word-spacing、white-space、direction
  • 列表:list-style 系列
  • 表格:border-collapse、caption-side
  • 其他:visibility、cursor、quotes

不可继承:margin/padding、border、background、width/height、display、position、float、z-index。

本题来自 计算机知识面试题库,共 921 道题。