!important 是 CSS 中用来 提高某条样式优先级 的规则。
!important 的样式会 覆盖普通样式,即使普通样式权重更高也无效selector {
property: value !important;
}
CSS 优先级综合考虑:
!important 优先于普通样式!important 的作用原理!important 会将样式规则的优先级提升到最高级别。
CSS 优先级顺序(从高到低):
!important/* 优先级:0,1,0 (ID选择器) */
#myElement {
color: blue;
}
/* 优先级:1,0,0 (ID选择器 + !important) */
#myElement {
color: red !important;
}
/* 优先级:0,0,1 (元素选择器 + !important) */
div {
color: green !important; /* 这个会生效! */
}
结果: 文字颜色为绿色,因为 !important 拥有最高优先级。
/* Bootstrap 按钮样式 */
.btn-primary {
background-color: #007bff;
border-color: #007bff;
}
/* 自定义主题色 */
.btn-primary {
background-color: #e74c3c !important;
border-color: #c0392b !important;
}
/* 工具类通常使用 !important */
.text-red {
color: #e53e3e !important;
}
.text-center {
text-align: center !important;
}
.mb-0 {
margin-bottom: 0 !important;
}
.hidden {
display: none !important;
}
<div style="color: blue; font-size: 16px;">这个div有内联样式</div>
/* 正常情况下无法覆盖内联样式 */
div {
color: red; /* 无效 */
}
/* 使用 !important 可以覆盖 */
div {
color: red !important; /* 生效! */
font-size: 20px !important; /* 生效! */
}
@media print {
.no-print {
display: none !important;
}
.print-only {
display: block !important;
}
* {
background: white !important;
color: black !important;
box-shadow: none !important;
}
}
!important 的问题与风险过度使用 !important 会导致 "特异性战争",需要不断使用更强的 来覆盖。
!important/* 问题示例 */
.button { color: blue !important; }
#submitBtn { color: red !important; }
body .form .button.submit { color: green !important; }
button[type="submit"].submit#submitBtn { color: yellow !important; }
/* 很难追踪哪个样式会生效 */
.header { color: black !important; }
.page-header { color: blue !important; }
#main-header { color: red; }
.header.title { color: green !important; }
在开发者工具中,很难确定哪个 !important 规则在影响元素。
/* 工具类应该总是生效 */
.sr-only {
position: absolute !important;
width: 1px !important;
height: 1px !important;
padding: 0 !important;
margin: -1px !important;
overflow: hidden !important;
clip: rect(0, 0, 0, 0) !important;
white-space: nowrap !important;
border: 0 !important;
}
.no-margin {
margin: 0 !important;
}
.full-width {
width: 100% !important;
}
/* 覆盖 Bootstrap 或其他框架 */
.ant-btn-primary {
background: #1890ff !important;
}
.material-button {
box-shadow: none !important;
}
/* 用户自定义样式覆盖网站样式 */
.ad-banner {
display: none !important;
}
.autoplay-video {
display: none !important;
}
@media print {
.sidebar, .advertisement, .navigation {
display: none !important;
}
* {
-webkit-print-color-adjust: exact !important;
color-adjust: exact !important;
}
}
/* 重要的状态类 */
.is-hidden {
display: none !important;
}
.is-visible {
display: block !important;
}
.has-error {
border-color: #e53e3e !important;
}
!important/* 低特异性 */
.button { color: blue; }
/* 更高特异性 */
body .container .button { color: red; }
/* 或使用 ID */
#submit-button { color: green; }
/* 后面的规则会覆盖前面的 */
.button { color: blue; }
.button { color: red; } /* 这个生效 */
:root {
--button-color: blue;
}
.button {
color: var(--button-color);
}
/* 轻松覆盖 */
.special-button {
--button-color: red;
}
/* 定义图层优先级 */
@layer base, components, utilities;
@layer base {
.button { color: blue; }
}
@layer utilities {
.button { color: red; } /* 这个生效,因为utilities层在后面 */
}
/* 好的实践 */
.utility-class {
display: none !important; /* 工具类可以适当使用 */
}
.component-style {
color: blue; /* 常规样式避免使用 */
}
/* 必须使用 !important 来覆盖第三方库 */
.third-party-override {
background: red !important; /* 覆盖 Bootstrap 默认样式 */
}
/* 打印样式需要 !important */
@media print {
.no-print {
display: none !important; /* 确保打印时隐藏 */
}
}
/* BEM 方法论通常不需要 !important */
.block__element--modifier {
color: red; /* 高特异性,很少需要 !important */
}
/* 工具类可以使用 !important */
.u-text-center {
text-align: center !important;
}
.u-hidden {
display: none !important;
}
在开发者工具中,!important 规则会有一个特殊的标识:
/* 在 Chrome DevTools 中显示为 */
color: red !important; /* 会有 !important 标记 */
@layer base, themes, utilities;
@layer base {
.button { color: blue; }
}
@layer themes {
.button { color: red; } /* 这个生效 */
}
@layer utilities {
.text-red { color: red; }
}
:where() 伪类降低特异性/* 高特异性 */
header nav ul li a { color: blue; }
/* 低特异性 */
:where(header nav ul li) a { color: red; }