


Detailed explanation of CSS cascading, inheritance, and specific examples
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
