零基本教你学前端——68-CSS选取器的权重
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在前面,<span style="color: black;">咱们</span>学习了样式表引入的优先级判断。<span style="color: black;">倘若</span>多个<span style="color: black;">选取</span>器都来修饰同一个元素,优先级又该<span style="color: black;">怎样</span>判断呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>来看一个例子。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS<span style="color: black;">选取</span>器权重——一个例子</title>
<style>
#h-one {
color: blue
}
* {
color: orange
}
.header {
color: green
}
h1 {
color: red
}
</style>
</head>
<body>
<h1 class="header" id="h-one" style="color: purple">
学历和能力哪个更<span style="color: black;">要紧</span>?
</h1>
</body>
</html></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">仔细观察一下,h1 元素<span style="color: black;">同期</span>存在内联样式和内部样式的修饰。内部样式的 ID <span style="color: black;">选取</span>器、通用<span style="color: black;">选取</span>器、类<span style="color: black;">选取</span>器和元素<span style="color: black;">选取</span>器,<span style="color: black;">亦</span>都指向了 h1 元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">多个样式<span style="color: black;">功效</span>于同一个元素,是冲突的,到底哪个优先级更高呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,标题文本的颜色你<span style="color: black;">必定</span><span style="color: black;">晓得</span>,是紫色。<span style="color: black;">由于</span>内联样式优先级高于内部样式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>把内联样式删掉,标题的文本会是什么颜色呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>看,是蓝色!说明这四个<span style="color: black;">选取</span>器,ID <span style="color: black;">选取</span>器优先级最高。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">去掉 ID <span style="color: black;">选取</span>器,标题变<span style="color: black;">成为了</span>绿色!说明这三个<span style="color: black;">选取</span>器,类<span style="color: black;">选取</span>器优先级最高。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再去掉类<span style="color: black;">选取</span>器,标题变<span style="color: black;">成为了</span>红色!说明元素<span style="color: black;">选取</span>器优先级高于通用<span style="color: black;">选取</span>器。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后去掉元素<span style="color: black;">选取</span>器,标题变<span style="color: black;">成为了</span>橙色!说明<span style="color: black;">这儿</span>通用<span style="color: black;">选取</span>优先级最低。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>这个例子,<span style="color: black;">咱们</span><span style="color: black;">能够</span>总结一个规律:行内样式优先级大于ID<span style="color: black;">选取</span>器,大于类<span style="color: black;">选取</span>器,大于元素<span style="color: black;">选取</span>器,大于通用<span style="color: black;">选取</span>器。<span style="color: black;">然则</span>,光<span style="color: black;">把握</span>这个规律还<span style="color: black;">不足</span>,遇到<span style="color: black;">有些</span>更<span style="color: black;">繁杂</span><span style="color: black;">选取</span>器,就比较难判断了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>再看一个案例。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS<span style="color: black;">选取</span>器权重——第二个例子</title>
<style>
p { /* 1 */
color: purple;
}
div p { /* 1 + 1 = 2 */
color: green;
}
.box p { /* 10 + 1 = 11 */
color: orange;
}
#box p { /* 100 + 1 = 101 */
color: red;
}
div#box p { /* 1 + 100 + 1 = 102 */
color: blue;
}
</style>
</head>
<body>
<p>我觉得能力更<span style="color: black;">要紧</span>!</p>
<div class="box">
<p>其实这<span style="color: black;">没</span>非是证明自己价值、或被别人认可的一种方式,从客观的<span style="color: black;">方向</span>来讲,当你大学刚毕业的时候,<span style="color: black;">无</span>工作经验,那学历肯定是让别人认可你的<span style="color: black;">第1</span>方式。</p>
</div>
<div id="box">
<p</div>
真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。 对于这个问题,我有不同的看法... 你的见解真是独到,让我受益匪浅。 我深受你的启发,你的话语是我前进的动力。 你的努力一定会被看见,相信自己,加油。 百度seo优化论坛 http://www.fok120.com/ 感谢你的精彩评论,带给我新的思考角度。 “板凳”(第三个回帖的人) 感谢您的精彩评论,为我带来了新的思考角度。
页:
[1]