Home Web Front-end CSS Tutorial Three major features of CSS you must know

Three major features of CSS you must know

Mar 13, 2018 am 10:32 AM
css characteristic

This time I will bring you the three major features of CSS that you must know, and what are the precautions for using the three major features of CSS. The following is a practical case, let’s take a look.

Three major features of CSS: inheritance, cascading, priority

1. Inheritance

1. What is inheritance?

Function: to parent Elements set some attributes, which can also be used by sub-elements. We call this inheritance

Note:

1. Not all attributes can be inherited, only those with can be inherited

2. In CSS inheritance, not only sons can inherit, but also descendants

3.Speciality in inheritance

3.1 The text color and underline of the a tag cannot be inherited (that is to say, it cannot be inherited through Obtain)

3.2 The text size of the h tag cannot be inherited (that is to say, it cannot be obtained through inheritance)

Application scenarios:

Generally used to set some common information on the web page, such as the text color, font, text size, etc. of the web page;
body{} >>> Common information is generally set in the body

2. Cascading

1. What is cascading?

Function: Cascading is the ability of CSS to handle conflicts

Notes:

Cascading will only occur if "the same label" is selected in multiple selectors and then the "same attribute" is set.

CSS full name Cascading StyleSheet (Cascading Style Sheet )

For example:

<style>
        p{            color: red;
        }        .para{            color: blue;
        } </style>
 <p id="identity" class="para">我是段落</p>
Copy after login

3. Priority

1.What is priority?

Function: When multiple selectors select the same tag, and when the same attribute is set to the same tag, how to cascade is determined by the priority

2. Three ways of priority judgment

2.1 Indirect selection refers to inheritance
If it is indirect selection, then whoever is closer to the target label will listen.

2.2 Same selector (direct selection)
If they are all directly selected, and they are all the same type of selector, Then whoever writes it later will listen to it

2.3 Different selectors (direct selection)
If they are all directly selected and are not the same type of selector, then the priority of the selector will be followed To cascade:

id>class>tag>wildcard>inheritance>browser default

id>class>label>pass>continue>browse

4. !important

1. What is !important

Function: Used to increase the priority of an attribute in a selector that directly selects a tag, you can The priority of the specified attribute is raised to the highest

Notes:

1.!important can only be used for direct selection, cannot be used for indirect selection

2. The tag selected by the wildcard selector is also directly selected, and you can also use !important to increase the priority

3.!important can only increase the priority of the specified attribute, and other The priority of the attribute will not be increased

4.!important must be written before the semicolon of the attribute value

5.!The exclamation point before important cannot be omitted

 <style>
      p{
         color: red !important; //提升优先级到最高
         font-size: 30px; //不会提升优先级(说明了上面的第3点)
       }  </style>
Copy after login

5. Weight of priority

1. What is the weight of priority?

Function: When multiple selectors are mixed together, we can determine who has the highest priority by calculating the weight

2. Weight calculation rules

2.1 First calculate how many ids there are in the selector. The selector with more ids has the highest priority

2.2 If the number of ids If the number of class names is the same, then look at the number of class names. The one with more class names has the highest priority

2.3 If the number of class names is the same, then look at the number of tag names. The number of tag names The one with more has the highest priority

2.4 If the number of ids is the same, the number of class names is the same, and the number of tag names is the same, then the calculation will not continue, so who writes at the end? Whoever listens
That is to say, if the priorities are the same, then whoever writes next listens to whom

Note:

1). This is only required if the selector directly selects the tag. Calculate the weight, otherwise it will definitely listen to the directly selected selector;

2). The weight of the wildcard is 0

I believe you have mastered the method after reading the case in this article, please pay attention for more exciting things Other related articles on php Chinese website!

Recommended reading:

How to load HTMLString in iOS webView

html5 little knowledge that is easily overlooked

The above is the detailed content of Three major features of CSS you must know. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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 bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

See all articles