Get the App
SLTechnology News&Howtos  ›  Development  › 

Example analysis of CSS selector and declaration

Shulou Source: shulou.com Published: 2022-06-01 13:13:33 10月03日 Update

This article mainly introduces the example analysis of CSS selector and declaration, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Grouping of CSS selectors

Selectors are grouped, and the grouped selectors share the same declaration. Separate the selectors that need to be grouped with commas.

In the following example, all the title elements are grouped. All the title elements are green.

H2,h3,h4,h5,h6,h7 {color: green;}

CSS inheritance

Body {font-family: Verdana, sans-serif;}

Inheritance and its problems

In CSS, child elements inherit attributes from parent elements. The body element of the site will use the Verdana font (provided the font exists on the user's system).

With CSS inheritance, child elements inherit the attributes owned by the highest-level element, without the need for additional rules.

All child elements of body (such as p, td, ul, ol, ul, li, dl, dt, and dd) will display Verdana fonts, as will child elements of child elements.

However, not all browsers work in this way, and we can use the "redundancy rule" to deal with the problem that old browsers cannot understand inheritance.

Redundancy rule

Body {font-family: Verdana, sans-serif;} p, td, ul, ol, li, dl, dt, dd {font-family: Verdana, sans-serif;}

Child elements uninherit

What if you don't want the "Verdana, sans-serif" font to be inherited by all the child elements?

For example, you want the font of the paragraph to be Times. Then, create a separate rule for p, and it will get rid of the rules of the parent element:

Body {font-family: Verdana, sans-serif;} td, ul, ol, ul, li, dl, dt, dd {font-family: Verdana, sans-serif;} p {font-family: Times, "Times New Roman", serif } Thank you for reading this article carefully. I hope the article "sample Analysis of CSS selectors and statements" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Elements selections fonts grouping articles rules examples analysis redundancy attributes titles rules browsers questions browsing maximum same value examples interests Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple MariaDB Docker Huawei