zl程序教程

您现在的位置是:首页 >  前端

当前栏目

通过!important设置css样式优先级

CSS 设置 通过 样式 优先级 Important
2023-09-11 14:19:37 时间

CSS写在不同的地方有不同的优先级,一般 .css文件中的定义 < 元素style中的属性,但是如果使用!important,则会变得不一样,使用!important的css定义是拥有最高的优先级的

//html
<div class="testClass" style="color:red;">
        Important优先级
</div>

//css
.testClass{ 
color:blue !important;
}

以上,在div上面设置了颜色为红色的内联样式,此时的优先级最高,但如果想要将字体颜色设置为蓝色 ,则需要添加!important 像这样:
.testClass{
color:blue !important;
}