CSS中!important的作用

提升指定样式规则的应用优先权。

  • IE6及以下浏览器有个比较显式的支持问题存在,!important在同一条规则集里不生效。请看下述代码:

    示例代码:

    div { color: #f00 !important; color: #000; }

    在上述代码中,IE6及以下浏览器div的文本颜色为#000,!important并没有覆盖后面的规则;其它浏览器下div的文本颜色为#f00

  • IE6及以下浏览器要使!important生效,可用以下代码:

    示例代码:

    div { color: #f00 !important; } div { color: #000; }

    在上述代码中,IE6及以下浏览器中div的文本颜色表现与其它浏览器一致,都为#f00

  IE6及更早浏览器下,!important在同一条规则集内不生效。

示例:

 <!DOCTYPE html>
<html>
<head>
<title>important的使用</title>
<meta name="content-type" content="text/html; charset=UTF-8">
<style>
.button {
position: relative;
background-color: #4CAF50;
border-radius:8px;
font-size: 28px;
color: #FFFFFF;
padding: 20px;
width: 200px;
text-align: center;
transition-duration: 1.5s;
overflow: hidden;
cursor: pointer;
}
.button:hover{
box-shadow:0 4px 8px 0 rgba(0,0,0,0.1),0 6px 20px 0 rgba(0,0,0,0.299);
} .button:after {
content: "";
background: #90EE90;
display: block;
position: absolute;
padding-top: 300%;
padding-left: 350%;
margin-left: -20px!important;
margin-top: -120%;
opacity: 0;
transition:1s;
} .button:active:after {
padding: 0;
margin: 0;
opacity: 1;
transition:0s;
}
</style>
</head>
<body> <button class="button">Click Me</button>
<p>第31行的margin-left设置了!important,因此后面的第39行是不能更改margin-left的值。
</body>
</html>

参考文章:

http://www.runoob.com/css3/css3-buttons.html

http://www.css88.com/book/css/rules/!important.htm

本文为博主原创文章,若需转载请注明出处。

上一篇:深入理解JavaScript系列(8):S.O.L.I.D五大原则之里氏替换原则LSP


下一篇:WIN10 64位下VS2015 MFC直接添加 halcon 12的CPP文件实现视觉检测