css的继承性与层叠性参考一下两个demo即可
1.
<!DOCTYPE html>
<html lang="en">
<!--css3选择器-->
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
/*1. 继承性*/
/*有一些属性,当给自己设置的时候,自己的后代都继承上了,即css的继承性*/
/*color, text-开头的, line-开头的, font-开头的 这些关于文字样式的都能继承;
所有关于盒子的、定位的、布局的属性都不能继承*/
body{
color: red;font-size: 20px;
}
/*2. 层叠性 权重的衡量(id class 标签)不进位,如果权重一样,以写在后面的为准*/
/*id选择器 1 class选择器 1 标签选择器 1 记作 1 1 1;
开始数之前一定要看一下是否选中文字所在的内层标签*/
#box1 .hezi2 p{
color: red;
}
/*1,0,3*/
div div #box3 p{
color: green;
}
/*0,3,4 交集选择器是一个标签选择器和一个类选择器*/
div.hezi1 div.hezi2 div.hezi3 p{
color: blue;
}
</style>
</head>
<body>
<!--1. 继承性-->
<div>
<ul>
<li>
<p>我的颜色</p>
</li>
</ul>
<p>我的颜色</p>
</div>
<div>
<ul>
<li>郑州</li>
<li>郑州</li>
<li>郑州</li>
</ul>
</div>
<!--2. 层叠性-->
<div class="hezi1" id="box1">
<div class="hezi2" id="box2">
<div class="hezi3" id="box3">
<p>我的颜色</p>
</div>
</div>
</div>
</body>
</html>
2.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
/*如果没有直接选中,通过继承性得来的权重为0;
如果都是0,就近原则*/
#box{
color: red;
}
p{
color: blue;
}
/*都没选中,就近原则*/
.hezi1 .hezi2{
color: red;
}
.hezi3{
color: blue;
}
</style>
</head>
<body>
<div id="box">
<p>哈哈哈哈</p>
</div>
<div class="hezi1" id="box1">
<div class="hezi2" id="box2">
<div class="hezi3" id="box3">
<p>我的颜色</p>
</div>
</div>
</div>
</body>
</html>
3. 典型例题
<!DOCTYPE html>
<html lang="en">
<!--css3选择器-->
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
/*两个都选中了,表示box1的后代div都被选中了*/
#box1 div{
color: red;
}
#box3{
color: blue;
}
</style>
</head>
<body>
<div id="box1" class="c1">
<div id="box2" class="c2">
<div id="box3" class="c3">
我的颜色
</div>
</div>
</div>
</body>
</html>
4. important属性
<!DOCTYPE html>
<html lang="en">
<!--css3选择器-->
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
/*!important提升的是一个属性而不是一个选择器,无法提升继承的权重;*/
p{
color: red;
font-size: 50px !important;
}
#para1{
color: blue;
}
.c1{
color: green !important;
}
/*都没有选中时不影响就近原则*/
ul{
color: red !important;
}
h3{
color: blue;
}
</style>
</head>
<body>
<div>
<p id="para1" class="c1">我的颜色</p>
</div>
<div>
<ul>
<li>
<h3>哈哈哈哈</h3>
</li>
</ul>
</div>
</body>
</html>