优先级
- !important
- 行内样式
- ID 选择符
- 类选择符
- 标签选择符 | 伪类选择符 | 属性选择符
- 伪对象选择符
- 通配选择符 | 关系选择符 | 否定伪类
可以看到内联样式(通过元素中 style 属性定义的样式)的优先级大于任何选择器;而给属性值加上 !important 又可以把优先级提至最高,就是因为它的优先级最高,所以需要谨慎使用它,以下有些使用注意事项:
- 一定要优先考虑使用样式规则的优先级来解决问题而不是 !important
- 只有在需要覆盖全站或外部 CSS 的特定页面中使用 !important
- 永远不要在你的插件中使用 !important
- 永远不要在全站范围的 CSS 代码中使用 !important
继承性
CSS 属性很多,但并不是所有的属性默认都是能继承父元素对应属性的,那哪些属性存在默认继承的行为呢?一定是那些不会影响到页面布局的属性,可以分为如下几类:
- 字体相关:font-family、font-style、font-size、font-weight 等
- 文本相关:text-align、text-indent、text-decoration、text-shadow、letter-spacing、word-spacing、white-space、line-height、color 等
- 列表相关:list-style、list-style-image、list-style-type、list-style-position 等
- 其他属性:visibility、cursor 等