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
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft