练习一:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="权重,练习">
<meta name="description" content="对网站的描述">
<title>第1题</title>
<style type="text/css">
#father #son{
color:blue; /* 权重: 2个id选择器 0个类选择器*/
}
#father p.c2{
color:black; /* 权重: 1个id选择器 1个类选择器 1个标签选择器*/
}
div.c1 p.c2{
color:red; /* 权重: 0个id选择器 2个类选择器 2个标签选择器*/
}
#father{
color:green !important; /* 没有命中到p标签 */
}
div#father.c1{
color:yellow; /* 没有命中到p标签 */
}
</style>
</head>
<body>
<div id="father" class="c1">
div内容
<p id="son" class="c2">
p标签内容
</p>
</div>
</body>
</html>
练习2:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第2题</title>
<style type="text/css">
#father{
color:red;
}
p{
color:blue;
}
</style>
</head>
<body>
<div id="father">
<p>p标签文本</p>
</div>
</body>
</html>
练习3:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第3题</title>
<style type="text/css">
div p{
color:yellow;
}
#father{
color:red;
}
p.c2{
color:blue;
}
</style>
</head>
<body>
<div id="father" class="c1">
<p class="c2">p标签文本</p>
</div>
</body>
</html>
练习4:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第4题</title>
<style type="text/css">
div div{
color:yellow; /* 权重: 0个id选择器 0个类选择器 2个标签选择器*/
}
/* 标签是从右向左找 */
div{
color:red; /* 权重: 0个id选择器 0个类选择器 1个标签选择器*/
}
</style>
</head>
<body>
<div>
<div>
<div>
多层div包含的文本...
</div>
</div>
</div>
</body>
</html>
练习5:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第5题</title>
<style type="text/css">
div div div div div div div div div div div div {
color:yellow; /* 权重: 0个id选择器 0个类选择器 12个标签选择器*/
}
.haha{
color:blue; /* 权重: 0个id选择器 1个类选择器 12个标签选择器*/
}
</style>
</head>
<body>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div class="haha">
很多层div包含的文本
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
练习6:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第6题</title>
<style type="text/css">
.c1 .c2 div{
color:blue ; /* 权重: 0个id选择器 2个类选择器 1个标签选择器*/
}
div #box3{
color:green ; /* 权重: 1个id选择器 0个类选择器 1个标签选择器*/
}
#box1 div{
color:yellow ; /* 权重: 1个id选择器 0个类选择器 1个标签选择器 */
}
</style>
</head>
<body>
<div id="box1" class="c1">
<div id="box2" class="c2">
<div id="box3" class="c3">
哈哈哈
</div>
</div>
</div>
</body>
</html>