• 通过id设置的css属性和通过元素设置的css属性冲突了,优先级哪个高?


    ---恢复内容开始---

    <!DOCTYPE html>
    <html>
    <head>
        <title>div test</title>
    </head>
    
    <style> 
    
    
     div{
    
             height: 300px;
             width:200px;
             background: blue;
     }
    
    #main{
    
        background: red;
    }
    
    #footer{
    
        background:grey;
    }
    
    </style>
    
    <body>
    
        <div id="header"></div>
        <div id="main"></div>
        <div id="footer"></div>
    
    </body>
    
    </html>

    以上代码先是设置了所有的div的背景色是蓝色,这样body中的三个div的背景色就都是蓝色的了,后来又通过id的形式设置了main这个div的背景色是red,这个属性会覆盖掉通过div设置的蓝色背景,footer也是一样的道理。

    可如果是这样结果是什么样子的呢?

    <!DOCTYPE html>
    <html>
    <head>
        <title>div test</title>
    </head>
    
    <style> 
    #main{
    
        background: red;
    }
    
    #footer{
    
        background:grey;
    }
    
     div{
    
             height: 300px;
             width:200px;
             background: blue;
     }
    
    
    </style>
    
    <body>
    
        <div id="header"></div>
        <div id="main"></div>
        <div id="footer"></div>
    
    </body>
    
    </html>

    main和footer这两个div的颜色最后会是蓝色的吗?经过chrome测试,发现并没有。

    这说明浏览器渲染html的时候并不是从上到下按照css代码的编写顺序执行的,而是id的css属性会覆盖掉元素的css属性,和css代码写在前面后面没有关系。

    ---恢复内容结束---

    <!DOCTYPE html>
    <html>
    <head>
        <title>div test</title>
    </head>
    
    <style> 
    
    
     div{
    
             height: 300px;
             width:200px;
             background: blue;
     }
    
    #main{
    
        background: red;
    }
    
    #footer{
    
        background:grey;
    }
    
    </style>
    
    <body>
    
        <div id="header"></div>
        <div id="main"></div>
        <div id="footer"></div>
    
    </body>
    
    </html>

    以上代码先是设置了所有的div的背景色是蓝色,这样body中的三个div的背景色就都是蓝色的了,后来又通过id的形式设置了main这个div的背景色是red,这个属性会覆盖掉通过div设置的蓝色背景,footer也是一样的道理。

    可如果是这样结果是什么样子的呢?

    <!DOCTYPE html>
    <html>
    <head>
        <title>div test</title>
    </head>
    
    <style> 
    #main{
    
        background: red;
    }
    
    #footer{
    
        background:grey;
    }
    
     div{
    
             height: 300px;
             width:200px;
             background: blue;
     }
    
    
    </style>
    
    <body>
    
        <div id="header"></div>
        <div id="main"></div>
        <div id="footer"></div>
    
    </body>
    
    </html>

    main和footer这两个div的颜色最后会是蓝色的吗?经过chrome测试,发现并没有。

    这说明浏览器渲染html的时候并不是从上到下按照css代码的编写顺序执行的,而是id的css属性会覆盖掉元素的css属性,和css代码写在前面后面没有关系。

  • 相关阅读:
    编译和和运行amor
    用好C语言的中库,系统错误的处理
    C语言中的宏
    时隔多年,再次实现的链表
    脚本更改桌面背景
    python爬虫 一个security的RSA加密爬虫
    mysql 8.0版本
    mysql5.7的并行复制
    kinshard中间件问题
    Springboot2-@Transactional 事务不生效
  • 原文地址:https://www.cnblogs.com/yfish/p/6390031.html
Copyright © 2020-2023  润新知