Home  >  Article  >  Web Front-end  >  Detailed introduction to style override rules in CSS

Detailed introduction to style override rules in CSS

黄舟
黄舟Original
2017-07-22 10:36:041049browse

Everyone knows that the full name of CSS is "cascading style sheet", but it is estimated that many people do not know the meaning of the word "cascading". In fact, "cascading" refers to the overlay of styles. When an element is applied with multiple styles and there are style attributes with the same name, the browser must select an attribute value from them. This process is called "cascading". Style overlay (this name is more popular) follows certain rules. I had always vaguely understood this rule before, but it was not until I read "CSS: The Missing Manual" these days that it suddenly became clear. Below are some of my study notes.

First of all, it needs to be clear that many situations will cause an element to be applied with multiple styles, and the rules of style coverage also need to be determined according to different situations. The specific rules are as follows.

Rule 1: When a style conflict occurs due to inheritance, the nearest ancestor wins.

The inheritance mechanism of CSS allows an element to inherit styles from the ancestor elements that contain it. Consider the following situation:

<html>
<head>
<title>rule
 1</title>
<style>
body
 {color:black;}
p
 {color:blue;}
</style>
</head>
<body>
    <p>welcome
 to <strong>gaodayue的网络日志</strong></p>
</body>
</html>


strongFrom body and The color attribute is inherited in p, but since p is closer to strong in the inheritance tree, the text in strong eventually inherits the blue color of p.

规则二:继承的样式和直接指定的样式冲突时,直接指定的样式获胜。

在上面的例子中,假如还指定了strong元素的样式,如:

strong
 {color:red;}

那么根据规则二,strong中的文字最终显示为红色。

规则三:直接指定的样式发生冲突时,样式权值高者获胜。

样式的权值取决于样式的选择器,权值定义如下表。

##Tag Selector 1
CSS Selector Weight
Class selector 10
ID selector 100
Inline style 1000
Pseudo-element (:first-child, etc.) 1
Pseudo-class (:link, etc.) 10

可以看到,内联样式的权值>>ID选择器>>类选择器>>标签选择器,除此以外,后代选择器的权值为每项权值之和,比如”#nav .current a”的权值为100 + 10 + 1 = 111。

规则四:样式权值相同时,后者获胜。

考虑下面这种情况

<pclass="byline">Written
 by <aclass="email"href="mailto:jean@cosmofarmer.
 com">Jean
 Graine de Pomme</a></p>
.byline
 a {color:red;}
p
 .email {color:blue;}

“.byline a”与”p .email”都直接指定了上面的a元素,且权值都为11,根据规则四,最终显示蓝色。

由于样式表可以是外部的,也可以是内部的,规则四提醒我们要注意外部样式表引入的顺序(及2cdf5bf648cf2f33323966d7f58a7f3f元素的顺序),以及外部样式表与内部样式表的出现位置。一般来说,内部样式表出现在所有外部样式表的引入之后,一般是在9c3bca370b5104690d9ef395f2c5f8d1之前。

规则五:!important的样式属性不被覆盖。

!important可以看做是万不得已的时候,打破上述四个规则的”金手指”。如果你一定要采用某个样式属性,而不让它被覆盖的,可以在属性值后加上!important,以规则四的例子为例,”.byline a {color:red !important;}”可以强行使链接显示红色。大多数情况下都可以通过其他方式来控制样式的覆盖,不能滥用!important。

The above is the detailed content of Detailed introduction to style override rules in CSS. For more information, please follow other related articles on the PHP Chinese website!

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