百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>CSS教程> 理解CSS的选中器优先级和!important权重
分享文章到:

理解CSS的选中器优先级和!important权重

发布时间:01/01 来源:未知 浏览: 关键词:

引荐:css视频教程

CSS中的选中器优先级与!important权重

  • .class选中器要高于标签选中器。
  • #id选中器要高于.class选中器。
  • 标签选中器是优先级最低的选中器。
  • !important的属性它的权重值优先级最高的,大于所有的选中器。

标签选中器和.class选中器

让我们进入标签选中器和.class选中器谁的优先级高实践,实践内容如:将HTML页面中的h2标签设定文本色彩。

代码块

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>优先级</title>
    <style>
        h2{
           color: red; /*红色*/
       }
       .box{
            color: springgreen; /*绿色*/
       }
    </style>
</head>
  
<body>
    <h2 class="box">微笑是最初的崇奉</h2>
</body>
</html>

结果图

1.png

.class选中器和id选中器

让我们进入.class选中器和id选中器谁的优先级高实践,实践内容如:将HTML页面中的h2标签设定文本色彩。

代码块

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>优先级</title>
    <style>
       h2{
           color: red; /*红色*/
       }
       .box{
            color: springgreen; /*绿色*/
       }
       #box{
           color:blue; /*蓝色*/
       }
    </style>
</head>
  
<body>
   <h2 class="box" id="box">微笑是最初的崇奉</h2>
</body>
</html>

结果图

2.png

!important权重使用

此刻我们知道了标签选中器优先级最低,那么笔者将标签选中器增加!important属性呢,谁的优先级更高呢?
!important权重使用格局如下:

color: red !important; /*红色*/

留意:属性:值 !important属性值用空格隔开即可。

让我们进入 !important属性使用实践,看看!important属性威力有多大哈。

代码块

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>!important使用</title>
    <style>
       h2{
           color: red !important; /*红色*/
       }
       .box{
            color: springgreen; /*绿色*/
       }
       #box{
           color:blue; /*蓝色*/
       }
    </style>
</head>
  
<body>
   <h2 class="box" id="box">微笑是最初的崇奉</h2>
</body>
</html>

结果图

3.png

总结

优先级从低到高如:标签选中器、.class选中器、#id选中器、!important属性

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板