Home  >  Article  >  Web Front-end  >  css weight_html/css_WEB-ITnose

css weight_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:50:55801browse

我们在使用CSS对网页元素定义样式时经常会遇到这种情况:要对一般元素应用一般样式,然后在更特殊的元素上覆盖它们。那么我们怎么样来保证我们所新定义的元素样式能覆盖目标元素上原有的样式呢?

在CSS中,会根据选择器的特殊性来决定所定义的样式规则的次序,具有更特殊选择器的规则优先于具有一般选择器的规则,如果两个规则的特殊性相同,那么后定义的规则优先。

那么,又怎么来计算选择器的特殊性呢?下面这张图介绍了特殊性的计算方法:

我们把特殊性分为4个等级,每个等级代表一类选择器,每个等级的值为其所代表的选择器的个数乘以这一等级的权值,最后把所有等级的值相加得出选择器的特殊值。

4个等级的定义如下:

  1. 第一等:代表内联样式,如: style=””,权值为1000。
  2. 第二等:代表ID选择器,如:#content,权值为100。
  3. 第三等:代表类,伪类和属性选择器,如.content,权值为10。
  4. 第四等:代表类型选择器和伪元素选择器,如div p,权值为1。

例如上图为例,其中#NAV为二等选择器,.ACTIVE为三等选择器,UL、LI和A为四等选择器。则整个选择器表达式的特殊性的值为1*100+1*10+3*1=113

下面是一些计算示例:

注意:通用选择器(*),子选择器(>)和相邻同胞选择器(+)并不在这四个等级中,所以他们的权值都为0。

我们再来看一个具体的例子:假如有以下组样式规则,你能判断出HTML代码中的两个标题是什么颜色吗?

01 #content div#main-content h2{

02     color:red;

03 }

04 #content #main-content>h2{

05     color:blue

06 }

07 body #content div[id="main-content"] h2{

08     color:green;

09 }

10 #main-content div.paragraph h2{

11     color:orange;

12 }

13 #main-content [class="paragraph"] h2{

14     color:yellow;

15 }

16 div#main-content div.paragraph h2.first{

17     color:pink;

18 }

以下是HTML代码:

01

02     

03         

CSS简介

04         

CSS(Cascading Style Sheet,可译为“层叠样式表”或“级联样式表”)是一组格式设置规则,用于控制Web页面的外观。

05         

06             

使用CSS布局的优点

07             

1、表现和内容相分离 2、提高页面浏览速度 3、易于维护和改版 4、使用CSS布局更符合现在的W3C标准.

08         

09     

10

Have you judged it? The answer is: both titles are red!

Let’s calculate the specificity value of each of the six style rules:

  • The first specificity value = 2*100 2*1=202
  • The value of the second speciality=2*100 1=201
  • The value of the third speciality=1*100 1*10 3*1=113
  • The fourth special Value of specialness=1*100 1*10 2*1=112
  • Value of fifth specialness=1*100 1*10 1*1=111
  • Sixth speciality The value of sex=1*100 2*10 3*1=123
  • Clearly, the first style rule won the championship of this style selector specificity contest with its high score of 202. Although some of the following rules may seem more complicated, the specificity is not about whose selector expression is longer, but the ID selector is king!

    Understanding the specificities of selectors is important, especially when fixing bugs, because you need to understand which rules take precedence and why.

    If you encounter a CSS rule that doesn't seem to be working, it's likely that a specificity conflict has occurred. Please add the ID of one of its parent elements to your selector to make it more specific. If this solves the problem, there's probably a more specific rule somewhere else in the stylesheet that overrides yours. If this is the case, you may want to review your code to resolve specificity conflicts and make the code as concise as possible.

    Reprinted from the link: http://www.nowamagic.net/librarys/veda/detail/1606

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn