批改状态:合格
老师批语:一个元素长啥样, 与一个女孩是一样的, 要么是天生的默认值, 要么是化妆后的自定义值, 理解了这个, 一切都舒服了
1.样式表的来源 2.选择器的优先级 3.源码的顺序
<style>style="..."<link > 引入tag < class < id < style(当前元素属性)当前选择器,以元素最后定义的属性为准| 案例 | id | class | tag | 标示 |
|---|---|---|---|---|
html body header h1 |
0 | 0 | 4 | 0,0,4 |
body header.page-header h1 |
0 | 1 | 3 | 0,1,3 |
.page-header .title |
0 | 2 | 0 | 0,2,0 |
#page-title |
1 | 0 | 0 | 1,0,0 |
!important尽可能不要用id
为什么要少用或不用 id?
样式复用class 来完成样式表inherit : 继承initail : 初始值(依赖浏览器)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>/* 1.简单优先级 */.title {color: red;}#page-title {color: green;}/* 2.复杂优先级 *//*0,0,4 */html body header h1 {color: red;}/* 0,1,3 */body header.page-header h1 {color: sienna;}/* 0,2,0 */.page-header .title {color: slateblue;}/* 1,0,0 *//* 不建议使用id,级别太高,平常建议使用class */#page-title {color: rgb(128, 0, 96);}</style><title>CSS测试</title></head><body><header class="page-header"><!-- 标签元素优先级最高style > id > class > tag(html标签)--><!-- <h1 id="page-title" class="title" style="color: hotpink">我在php中文网学习php</h1> --><h1 id="page-title" class="title">我正在php中文网学习php</h1></header></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号