本文摘自PHP中文网,作者青灯夜游,侵删。
推荐:css视频教程
CSS中的选择器优先级与!important权重
.class
选择器要高于标签选择器。#id
选择器要高于.class
选择器。- 标签选择器是优先级最低的选择器。
!important
的属性它的权重值优先级最高的,大于所有的选择器。
标签选择器和.class选择器
让我们进入标签选择器和.class
选择器谁的优先级高实践,实践内容如:将HTML
页面中的h2
标签设置文本颜色。
代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!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 >
|
结果图
.class选择器和id选择器
让我们进入.class
选择器和id
选择器谁的优先级高实践,实践内容如:将HTML
页面中的h2
标签设置文本颜色。
代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!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 >
|
结果图
!important权重使用
现在我们知道了标签选择器优先级最低,那么笔者将标签选择器添加!important
属性呢,谁的优先级更高呢?
!important权重使用格式如下:
注意:属性:值 !important
属性值用空格隔开即可。
让我们进入 !important
属性使用实践,看看!important
属性威力有多大哈。
代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!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 >
|
结果图
总结
优先级从低到高如:标签选择器、.class
选择器、#id
选择器、!important
属性
更多编程相关知识,请访问:编程教学!!
以上就是了解CSS的选择器优先级和!important权重的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
css quotes属性怎么用
jsp怎么引入css
html如何让链接不变色
css如何设置边框
css怎么设置宽为100vw
css怎么设置input颜色
css怎么调整字体
css 怎么设置全屏背景图片
css如何设置背景图片的大小
css如何修改html标签的title样式?(代码示例)
更多相关阅读请进入《css》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 了解CSS的选择器优先级和!important权重