样式覆盖

解决的问题:在开发中,我们可以结合自己的实际情况,借助CSS样式的权重来覆盖框架的样式,做出符合本项目要求的样式。

先复习一下相关的CSS知识。

样式的继承

定义:父元素设置了某属性,子属性也会有该属性

以下是会被继承的CSS样式属性

  1. azimuth, border-collapse, border-spacing,
  2. caption-side, color, cursor, direction, elevation,
  3. empty-cells, font-family, font-size, font-style,
  4. font-variant, font-weight, font, letter-spacing,
  5. line-height, list-style-image, list-style-position,
  6. list-style-type, list-style, orphans, pitch-range,
  7. pitch, quotes, richness, speak-header, speaknumeral,
  8. speak-punctuation, speak, speechrate,
  9. stress, text-align, text-indent, texttransform,
  10. visibility, voice-family, volume, whitespace,
  11. widows, word-spacing
  12. /* 文本相关属性 */
  13. font-family, font-size, font-style,
  14. font-variant, font-weight, font, letter-spacing,
  15. line-height, text-align, text-indent, texttransform,word-spacing
  16. /* 列表相关属性 */
  17. list-style-image, list-style-position,
  18. list-style-type, list-style

样式的覆盖

覆盖规则:

  • 根据引入方式确定优先级

优先级由高到低依次为:内联样式 → 内部样式表 → 外部样式表 → 浏览器缺省

  • 同一级别的,后写的覆盖先写的

就是文件上代码行号更靠下的优先级更高

  • 加有”!important”的样式,优先级最高

即无论哪一种情况,只要在样式上加了important,那么该样式的优先级最高。

  • 选择器优先级

在选择器不同的情况下,给每种选择器制定一个权值,计算命中的一个元素的所有选择器的总权值,值高的获胜。

元素选择器: 1
类选择器: 10
ID选择器: 100
内联选择器: 1000