Skip to content

CSS 笔记 - CSS 属性继承

1. 如果一个元素的某个属性没有叠层值(即当前节点某个属性值覆盖),则可能会继承某个祖先元素的值,

例如: 在 <body> 元素上加 font-family 属性,DOM 树的中 body 后代元素会继承,继承属性会顺序的传递给后代,直到它被层叠值覆盖。

2. 不是所有的属性都能被继承,主要以下几种(不完全枚举)

主要跟文本相关的属性

  • color
  • font
  • font-family
  • font-size
  • font-weight
  • font-variant
  • font-style
  • lint-height
  • letter-spacing
  • text-align
  • text-indent
  • text-transform
  • white-space
  • word-spacing

列表属性

  • list-style
  • list-style-type
  • list-style-position
  • list-style-image

表格的边框属性

  • border-collapse
  • border-spacing

使用开发者工具调试样式

样式检查器会显示所检查元素的每个选择器,并且按照优先级顺序排列

img

如上图可以清晰的了解元素的行内样式、选择器样式、继承样式以及被覆盖的继承值等信息

Released under the MIT License.