Home  >  Article  >  Web Front-end  >  Detailed explanation of CSS cascading, inheritance, and specific examples

Detailed explanation of CSS cascading, inheritance, and specific examples

零下一度
零下一度Original
2017-06-24 13:40:263671browse

Cascading

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.

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5  6     <title>CSS层叠性</title> 7     <style> 8         .box { 9             background-color: red;10             height: 200px;11             width: 200px;12         }13 14         .wrap {15             background-color: green;16         }17     </style>18 </head>19 <body>20     <div class="box wrap">21         22     </div>23 </body>24 </html>

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

Inheritance

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.

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>CSS继承性</title> 6  7     <style type="text/css"> 8         .parent { 9             color: red;10             font-size: 20px;11             height: 300px;12             width: 300px;13             background-color: green;14         }15 16         .child {17             height: 100px;18             width: 100px;19         }20     </style>21 22 </head>23 <body>24     <div class="parent">25         我是父div26         <p>27             我是段落28         </p>29 30         <div class="child">我是子div</div>31     </div>32 </body>33 </html>

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.

Speciality (Priority)

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!

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