Everyone knows that the full name of CSS is "cascading style sheet", but it is estimated that many people do not know the meaning of the word "cascading". In fact, "cascading" refers to the overlay of styles. When an element is applied with multiple styles and there are style attributes with the same name, the browser must select an attribute value from them. This process is called "cascading". Style overlay (this name is more popular) follows certain rules. I had always vaguely understood this rule before, but it was not until I read "CSS: The Missing Manual" these days that it suddenly became clear. Below are some of my study notes.
First of all, it needs to be clear that many situations will cause an element to be applied with multiple styles, and the rules of style coverage also need to be determined according to different situations. The specific rules are as follows.
Rule 1: When a style conflict occurs due to inheritance, the nearest ancestor wins.
The inheritance mechanism of CSS allows an element to inherit styles from the ancestor elements that contain it. Consider the following situation:
<html> <head> <title>rule 1</title> <style> body {color:black;} p {color:blue;} </style> </head> <body> <p>welcome to <strong>gaodayue的网络日志</strong></p> </body> </html>
strongFrom body and The color attribute is inherited in p, but since p is closer to strong in the inheritance tree, the text in strong eventually inherits the blue color of p.
规则二:继承的样式和直接指定的样式冲突时,直接指定的样式获胜。
在上面的例子中,假如还指定了strong元素的样式,如:
strong {color:red;}
那么根据规则二,strong中的文字最终显示为红色。
规则三:直接指定的样式发生冲突时,样式权值高者获胜。
样式的权值取决于样式的选择器,权值定义如下表。
CSS Selector | Weight |
---|---|
1 | |
Class selector | 10 |
ID selector | 100 |
Inline style | 1000 |
Pseudo-element (:first-child, etc.) | 1 |
Pseudo-class (:link, etc.) | 10 |
可以看到,内联样式的权值>>ID选择器>>类选择器>>标签选择器,除此以外,后代选择器的权值为每项权值之和,比如”#nav .current a”的权值为100 + 10 + 1 = 111。
规则四:样式权值相同时,后者获胜。
考虑下面这种情况
<pclass="byline">Written by <aclass="email"href="mailto:jean@cosmofarmer. com">Jean Graine de Pomme</a></p>
.byline a {color:red;} p .email {color:blue;}
“.byline a”与”p .email”都直接指定了上面的a元素,且权值都为11,根据规则四,最终显示蓝色。
由于样式表可以是外部的,也可以是内部的,规则四提醒我们要注意外部样式表引入的顺序(及元素的顺序),以及外部样式表与内部样式表的出现位置。一般来说,内部样式表出现在所有外部样式表的引入之后,一般是在之前。
规则五:!important的样式属性不被覆盖。
!important可以看做是万不得已的时候,打破上述四个规则的”金手指”。如果你一定要采用某个样式属性,而不让它被覆盖的,可以在属性值后加上!important,以规则四的例子为例,”.byline a {color:red !important;}”可以强行使链接显示红色。大多数情况下都可以通过其他方式来控制样式的覆盖,不能滥用!important。
The above is the detailed content of Detailed introduction to style override rules in CSS. For more information, please follow other related articles on the PHP Chinese website!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

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