search
HomeWeb Front-endCSS TutorialDeep understanding of CSS selector priority order

There are many selectors in CSS, such as class selectors, tag selectors, ID selectors, etc. The priority order of different selectors is also different. Today I will talk to you about the priority of CSS selectors. Friends in need can refer to the order, and the use of !important. I hope it will be useful to you.

1. !important indicates the highest priority . IE6 browser does not recognize !important .

Example:

Under normal circumstances, the style written below has a higher priority than the style above

demo1{
color:red;
color:green; /*绿色的优先级高于红色,所有浏览器都会显示绿色*/
}

If !important is added, its priority will be higher. IE6 is relatively stupid and I don’t know it.

demo2 {
color:red !important; /*除了ie6,其他浏览器会认为红色优先级高,显示红色字体*/
color:green; /*ie6浏览器则顺序读取css所以显示绿色*/
}

But note that ie6 does not recognize the priority of !important, but it does not mean that ie6 does not recognize the style attribute with !important.

demo3{
color:red;
color:green !important; /*包括ie6,所有浏览器都显示绿色字体,ie6只是不认识优先级罢了*/
}

2. CSS (Cascading Style Sheets) cascading style sheets. In practical applications, there are generally three cascading methods.

Priority: Inline style sheet (inside the tag) > Embedded style sheet (in the current file) > External style sheet (in the external file).

1. External style (applied to multiple web pages)

In the external style sheet, the CSS code is stored as a separate file. For example, the style.css file contains all styles. External cascading in HTML is introduced using the tag or the @import statement.

The sample code is as follows:

<link rel="stylesheet" href="style.css" type="text/css" /> //link 链接
@import url("style.css"); //@import 导入

The similarities and differences between @import and @import can be found in its article

2. Inline (applied to the current page)

The CSS code of the portal website is usually embedded, which is commonly known as inline style (Inline Style), which uses the

The sample code is as follows:

<style type="text/css">
    body{font-family:Arial,Helvetica,sans-serif;}
</style>

Inline CSS can effectively reduce HTTP requests, improve page performance, and relieve server pressure. Since the browser can only render the page after loading the CSS, it can prevent the CSS file from being read and causing the page to run naked.

3. Embedded (applied to specific tags)

The most basic way of writing CSS is to add code directly to the modified markup element.

The sample code is as follows:

<div style="font-family:Arial,Helvetica,sans-serif;">饿了么</div>

Although this is more intuitive, it greatly increases the page size and does not conform to the design idea of ​​separation of structure and performance.

3. When the weights are different, the browser determines which selector css style to use based on the weight. The style with the higher weight will be used.

Weight rules: The weight of label (P, span) is 1, the weight of class selector is 10, and the weight of id selector is up to 100.

Example:

div {color: red;} /*Tag, weight is 1*/

div span {color: green;} /*Two tags , the weight is 1 1=2*/

div>span {color: purple;} /*The weight is the same as above, so the proximity principle is adopted*/

.main {color : white;} /*Class selector, weight value is 10*/

div span.warning {color: purple;} /*Weight value is 1 1 10=12*/

#main .con p {color: yellow;} /*The weight is 100 10 1=111*/

The above is the detailed content of Deep understanding of CSS selector priority order. 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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software