The reason why the color of a element cannot inherit the parent element

The reason why the color of a element cannot inherit the parent element

First think about a question:
insert image description here

Why didn't you set the font color of a element and p element, they both have colors, and the colors are not the same.

This involves a knowledge point, inheritance , the parent element fills in the css style related to the text content, which can be inherited by the child elements, but it is not absolute. The a element above is an example. So what caused this situation to happen? This involves the process of page rendering:

As for the browser, he is reading the code line by line, which is also a tree structure
insert image description here

The prerequisite for rendering each element is that all the css attributes of the element must have a value. If there is no value, how does the browser know what the element looks like, and how does it display it on the page? ( Let me tell you that there is a man who has a mouth and a pair of eyes, so please draw his whole body, you must not be able to draw it.) The process of an element from having no attribute value to having all values ​​is called the calculation of attribute value process

The calculation process of attribute value (4 steps):

1. Determine the declared value :
insert image description here

First understand one thing, the style sheet, the browser, he has a style sheet, have you noticed that when you write h1 or other elements, the font is relatively large and bold, this is the style sheet of the browser The role of html is just the structure, and what really needs to play a display role must be controlled by css.

In addition to the browser's default style sheet, there are also css styles written by ourselves, called author style sheets.

Then, the first step in the calculation process of the attribute value is to determine the declaration value. If there is no conflicting declaration in the author's style sheet and the browser's default style sheet, the value will be assigned directly:

insert image description here

2. Cascading conflicts :

Then after the first step, the second step is to deal with cascading conflicts:

insert image description here

If you want to know the principle of css weight, please click here → css weight

3. Use inheritance :

For attributes that still have no value, if the parent element can inherit, the value of the parent element will be inherited

insert image description here

Looking back at the previous question:

insert image description here

4. Use the default value : For those attributes that still have no value after inheritance, use the default value.

insert image description here

Finally, thank you for watching.

Guess you like

Origin blog.csdn.net/qq_42425551/article/details/126726047