CSS has two core concepts-inheritance and cascading. The properties of a generic text class are inherited, that is, the CSS properties of an element are passed to internally nested elements. An element may have a source of one or more styles, and when the property conflicts, the properties that appear after the Cascade are determined based on the load order and weight size. Let's look at a cascading mechanism table and observe the stacking pattern against the example:
Inline ID class Element
Embedded 1 0 0 0
ID 0 1 0 0
class, attributes, pseudo-classes 0 0 1 0
Elements, pseudo-elements 0 0 0 1
Wildcard characters * 0 0 0 0
!important Highest
To set the font color for all types, the results are as follows:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<TITLE>CSS Priority Testing </title>
<style>
#test { color:orange;}
. test{ color:yellow; }
p{ color:green; }
*{ Color:cyan; }
p{ !important; Color:blue; }
span{ color:purple; }
</style>
<body>
<p style= "color:red" > Inline red </p>
<p id= "test" >id Orange </p>
<p class= "test" >class Yellow </p>
<p> Elements Green </p>
<p> with Cyan </p>
<p> Top Blue </p>
<p> <span> Sub-elements Purple </span> </p>
</body>
The results shown in the page are as follows:
You can see that the CSS properties of the element and wildcard settings do not display their own text color, the rest of them correctly display the set color, check the inline, ID, and class style changes, the result is as follows:
Can be seen, the Web page is first inherited, will be assigned to the HTML and the body of the cyan decision, in the CSS color settings for the P element, followed by the weight of the selection of the wildcard, the element, the highest color, the last lock class name set yellow, by this comparison, the previous 6 groups can get results, The final span check results in the following:
Now the reason is also at a glance, the span element inherits progressively, here the P element text is blue, and then the span element is stacked, there are the cyan and element purple, because the element has a high priority, all the elements set by the purple.
For the same elements, set the embedded, ID, class, and element selector settings color, embedded font color red, ID orange, class yellow, Element green, the code is as follows:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>CSS same element priority test</title> <style>#test{Color:Orange;}. Test{Color:Yellow; }P{Color:Green; } </style></Head><Body> <PID= "Test"class= "Test"style= "Color:red">Same element test</P></Body></HTML>
The results shown in the Web page are as follows:
Check its transformation rules:
Element selector has the lowest priority, green bottom, Pseudo-class yellow second, ID orange cascade, embedded priority highest, red at the top, and finally show red.
Summing up, the HTML cascade is by the wildcard, elements and pseudo-elements, class and Pseudo-class, ID, embedded from the bottom to the upper layer cascade, for the same level of selectors, with!important; Element attributes inherit from the ancestor of the largest level, and in turn determine the inheritance until the content of the element is set with the innermost layer of the CSS style.
CSS cascading and inheritance