Home>Article>Web Front-end> Detailed explanation of CSS cascading, inheritance, and specific examples
The so-called cascading refers to the superposition of multiple CSS styles, which means that the styles set later will cascade (cover) the previous styles.The premise of cascading is that the priorities of CSS selectors are the same,For example , when the inline CSS style sheet is used to define the
mark font size as 12 pixels, and the linked-in
mark color is red, then the paragraph text will be displayed as 12 pixels red, that is, the two styles produce superimposed.
The result displayed by the browser is a div with a length and width of 200 pixels and a green background color. The reason is that the background color defined in the box is covered by the background color defined in wrap
through the browser F12 inspection element, you can also see
CSS inheritance means that the style of the child container will inherit the style of the parent container. But not all styles can be inherited. Only some styles can be inherited, such as text-related font size, color, font style, line height, mouse style, etc.
Box-related styles cannot be inherited, such as: width and height, background color, margins, floating, absolute positioning, etc.
The effect is as follows:
You can see that both the p tag and the child div inherit the font color and size of the parent div, but the child div does not inherit the width, height and background color of the parent div,
Inherited styles are shown in solid lines, while other styles are blurred.
Note: Proper use of inheritance can simplify code and reduce the complexity of CSS styles. However, if all elements in a web page inherit a large number of styles, it will be difficult to determine the source of the style. Therefore, inheritance can be used for common styles in web pages such as fonts and text attributes. For example, font, font size, color, line spacing, etc. can be set uniformly in the body element, and then affect all text in the document through inheritance.
When defining CSS styles, two or more rules often apply to the same element, and then priority issues arise. What style does the element display at this time?
First of all, let’s give a priority conclusion:
Inline style > In-page style > External reference style > Browser default style
important > Inline > ID > Pseudo class | Class | Attribute selection > Label > Pseudo object > Wildcard > Inheritance
Let’s look at the example below. By default, the color of the font is determined by the browser settings
When we add a style to the body, the font color changes to red, indicating that the inherited style > browser default style
When we add a wildcard style, the font color turns to gray
It shows that the wildcard style is better than the inherited style. Try the style priority of the tag selector again
It can be seen that the priority of the tag selector is better than the wildcard style. Other types of styles can be tested by yourself, among which !important You can change the weight of the style
You can see that the font color displayed in the h1 title is blue, because the !important attribute can increase the weight of the style. Here is a brief explanation of the weight of css and each style type in css Each has its own weight
1. The weight of the inline style sheet is up to 1000;
2. The weight of the ID selector is 100
3. The weight of the Class selector is 10
4. HTML The weight of the tag selector is 1
Sometimes we apply different style rules on the same element, such as
The above is the detailed content of Detailed explanation of CSS cascading, inheritance, and specific examples. For more information, please follow other related articles on the PHP Chinese website!