page contents

谈谈CSS选择器权重优先级和!important权重

本文讲述了谈谈CSS选择器权重优先级和!important权重!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-01-JOWUHRgd61dce3ea0c0e2.png

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>

最终效果图:

attachments-2022-01-Wb5FX4kD61dce37b2ffce.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>


最终效果图:

attachments-2022-01-jLm8GxAM61dce38aa8a1d.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>

最终效果图:

attachments-2022-01-KKalo5MW61dce39c53aa7.png

总结

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

 更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

如果你想用Python开辟副业赚钱,但不熟悉爬虫与反爬虫技术,没有接单途径,也缺乏兼职经验
关注下方微信公众号:Python编程学习圈,获取价值999元全套Python入门到进阶的学习资料以及教程,还有Python技术交流群一起交流学习哦。attachments-2022-06-N9YDJe0K62b27d21601cd.jpeg

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1300 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章