优先级和继承

CSS 优先级和继承速查表

规则类型说明示例是否支持
内联样式直接写在元素上的 style 属性,优先级最高<div style="color:red">
ID 选择器使用 ID 选择器,优先级高#header {}
类选择器、属性选择器、伪类优先级次于 ID 选择器.btn {}, [type="text"] {}, :hover {}
元素选择器、伪元素优先级最低div {}, p::before {}
!important覆盖所有优先级,即使内联样式color: red !important;
继承某些属性会从父元素自动继承color, font-family
继承失败属性大多数盒模型属性不继承,如 margin、paddingmargin, padding
样式来源顺序后定义的样式会覆盖先定义的样式两个同级样式,后一个生效
!important 冲突当两个规则都使用 !important 时,优先级高的选择器生效#id !important > .class !important