在CSS中,id和class的区别及优先级
四个原则
<!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 type="text/css">
.class1 .class2 .class3 {
font-size: 25px;
}
.class2 .class3 {
font-size: 18px
}
.class3 {
font-size: 12px;
}
</style>
</head>
<body>
<div class="class1">
<p class="class2"> <span class="class3">我是多大字号?</span> </p>
</div>
</body>
</html>
- 原则二: #ID > .class > 标签选择符
<!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 type="text/css">
#id3 {
font-size: 25px;
}
.class3 {
font-size: 18px;
}
span {
font-size: 12px
}
</style>
</head>
<body>
<span id="id3" class="class3">我是多大字号?</span>
</body>
</html>
<!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 type="text/css">
.class1 .class2 .class3 {
font-size: 25px;
}
.class2 .class3 {
font-size: 18px
}
.class3 {
font-size: 12px;
}
</style>
</head>
<body>
<div class="class1">
<p class="class2"> <span class="class3">我是多大字号?</span> </p>
</div>
</body>
</html>
- 原则四:标签#id >#id ; 标签.class > .class
<!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 type="text/css">
span#id3 {
font-size: 18px
}
#id3 {
font-size: 12px
}
span.class3 {
font-size: 18px
}
.class3 {
font-size: 12px
}
</style>
</head>
<body>
<span id="id3">我是多大字号?</span>
<span class="class3">我是多大字号?</span>
</body>
</html>
<!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>id和class的优先级</title>
<style media="screen">
.class1 #id2 .class3 {
font-size: 25px
}
div .class2 span#id3 {
font-size: 18px
}
#id1 .class3 {
font-size: 14px
}
.class1 #id2 .class3 {
font-size: 12px
}
#id1 #id2 {
font-size: 10px
}
</style>
</head>
<body>
<div id="id1" class="class1">
<p id="id2" class="class2"> <span id="id3" class="class3">我是多大字号?</span> </p>
</div>
</body>
</html>