zoukankan      html  css  js  c++  java
  • CSS中选择器优先级与!important权重使用

    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>
    
    • 结果图

    .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>
    
    • 结果图

    !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>
    
    • 结果图

    总结

    • 优先级从低到高如:标签选择器、.class选择器、#id选择器、!important属性
  • 相关阅读:
    我不想写题解的题们
    [清华集训2012]模积和
    2013杭电warm up1 Rotation Lock Puzzle
    2013杭电warm_up1 1010 Difference Between Primes
    2013 Multi-University Training Contest 3 (g) The Unsolvable Problem
    2013杭电warm up1 1002 Pet 求树结点的高度
    hdu 3789 奥运排序问题 模拟
    13杭电warmup1 1001 Children's Day
    2013杭州网络预选赛 1004 Save Labman No.004 求异面直线之间距离
    2013成都网络预选赛 1010 A Bit Fun
  • 原文地址:https://www.cnblogs.com/lq0001/p/11946263.html
Copyright © 2011-2022 走看看