The CSS selector priority with! Important weights use
.class
The selector is higher than the tag selector.#id
Selector higher than the.class
selector.- The tag selector is the lowest priority selector.
!important
Attribute value of its weight with the highest priority, is greater than all the selectors.
Label selector and selector .class
- Let us enter the tag selector and
.class
selectors who have higher priority practice, practice content such as: theHTML
pageh2
tab to set the text color. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>优先级</title>
<style>
h2{
color: red; /*红色*/
}
.box{
color: springgreen; /*绿色*/
}
</style>
</head>
<body>
<h2 class="box">微笑是最初的信仰</h2>
</body>
</html>
-
Results Figure
.class selectors and id selectors
- Let's move
.class
selector andid
selectors who have higher priority practice, practice content such as: theHTML
pageh2
tab to set the text color. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>优先级</title>
<style>
h2{
color: red; /*红色*/
}
.box{
color: springgreen; /*绿色*/
}
#box{
color:blue; /*蓝色*/
}
</style>
</head>
<body>
<h2 class="box" id="box">微笑是最初的信仰</h2>
</body>
</html>
-
Results Figure
! Important weights use
- Now we know the tag selector lowest priority, then I add the tag selector
!important
attribute it, who the higher priority it? - ! Important weight using the following format:
color: red !important; /*红色*/
-
Note: The
属性:值 !important
attribute value can be separated by a space. - Let us enter the
!important
property using the practice, take a look at!important
property how much power ha. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>!important使用</title>
<style>
h2{
color: red !important; /*红色*/
}
.box{
color: springgreen; /*绿色*/
}
#box{
color:blue; /*蓝色*/
}
</style>
</head>
<body>
<h2 class="box" id="box">微笑是最初的信仰</h2>
</body>
</html>
-
Results Figure
to sum up
- From low to high priority, such as: the tag selector,
.class
a selector,#id
a selector,!important
Properties