• css样式表中的样式覆盖顺序


    #navigator {
        height: 100%;
         200;
        position: absolute;
        left: 0;
        border: solid 2 #EEE;
    }
    
    .current_block {
        border: solid 2 #AE0 !important;
    }
    

      

    查找一些教材中(w3schools等),只说css的顺序是“元素上的style” > “文件头上的style元素” >“外部样式文件”,但对于样式文件中的多个相同样式的优先级怎样排列,没有详细说明。经过测试和继续搜索,得知优先级如下排列:

    1. 样式表的元素选择器选择越精确,则其中的样式优先级越高:

    id选择器指定的样式 > 类选择器指定的样式 > 元素类型选择器指定的样式

    所以上例中,#navigator的样式优先级大于.current_block的优先级,即使.current_block是最新添加的,也不起作用。

    2. 对于相同类型选择器指定的样式,在样式表文件中,越靠后的优先级越高

    注意,这里是样式表文件中越靠后的优先级越高,而不是在元素class出现的顺序。比如.class2 在样式表中出现在.class1之后:

    .class1 {
        color: black;
    }
    
    .class2 {
        color: red;
    }
    

      

    而某个元素指定class时采用 class="class2 class1"这种方式指定,此时虽然class1在元素中指定时排在class2的后面,但因为在样式表文件中class1处于class2前面,此时仍然是class2的优先级更高,color的属性为red,而非black。

    3. 如果要让某个样式的优先级变高,可以使用!important来指定:

    .class1 {
        color: black !important;
    }
    
    .class2 {
        color: red;
    }
    

      

    此时class将使用black,而非red。

    对于一开始遇到的问题,有两种解决方案:

    1. 将border从#navigator中拿出来,放到一个class .block中,而.block放到.current_block之前:

    #navigator {
        height: 100%;
         200;
        position: absolute;
        left: 0;
    }
    
    .block {
        border: solid 2 #EEE;
    }
    
    .current_block {
        border: solid 2 #AE0;
    }
    

      

    需要默认为#navigator元素指定class="block"

    2. 使用!important:

    #navigator {
        height: 100%;
         200;
        position: absolute;
        left: 0;
        border: solid 2 #EEE;
    }
    
    .current_block {
        border: solid 2 #AE0 !important;
    }
    

       此时无需作任何其他改动即可生效。可见第二种方案更简单一些。

    转载位置:http://spartan1.iteye.com/blog/1526735

  • 相关阅读:
    How can I pretty-print JSON in python?
    怎样取出cobbler kopts中设置的参数?
    提问的智慧
    mysql中binary相加的问题
    sql之left join、right join、inner join的区别
    js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)
    python setup.py uninstall
    Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    idea中的插件,可以快速将类中的属性转换成Json字符串
    上传照片时候,可以通过配置文件和写配置类,实现控制上传文件的大小
  • 原文地址:https://www.cnblogs.com/laogai/p/3335171.html
Copyright © 2020-2023  润新知