85【CSS选择器简介】

|-------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 | <!DOCTYPE html> <html> <head> ``<``title``>第3个程序</``title``> </head> <body> ``<h1>我是第1行</h1> ``<h1>我是第2行</h1> ``<h2>我是第3行</h2> </body> </html> |

运行后效果

很多朋友可能发现了,这里没有换行符<br>为啥自动换行了,因为<h1>和<h2>组件(标签)自带换行属性

css是控制html组件(标签)属性的,那么css要如何锁定这个组件
常见的有3种

①:标签选择器( 标签名{}****)

我们现在要改变前2行的颜色,这两行用到的组件(标签)都是h1,我们可以使用标签选择器,对所有的<h1>标签起效果

|-------|------------------------------|
| 1 2 3 | h1{ ``color: red ``} |

|-------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | <html> <head> ``<``title``>第10个程序</``title``> ``<style ``type``=``"text/css"``> ``h1{ ``color: red ``} ``</style> </head> <body> ``<h1>我是第1行</h1> ``<h1>我是第2行</h1> ``<h2>我是第3行</h2> </body> |

运行后效果

②:类选择器(.+class名)

|-------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html> <head> ``<``title``>第10个程序</``title``> ``<style ``type``=``"text/css"``> ``.css1{ ``color: red ``} ``</style> </head> <body> ``<h1 class=``"css1"``>我是第1行</h1> ``<h1>我是第2行</h1> ``<h2 class=``"css1"``>我是第3行</h2> </body> </html> |

运行后结果

③:id选择器(#+id名)

|----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | <html> <head> ``<``title``>第10个程序</``title``> ``<style ``type``=``"text/css"``> ``#css1{ ``color: red; ``} ``</style> </head> <body> ``<h1 >我是第1行</h1> ``<h1>我是第2行</h1> ``<h2 id=``"css1"``>我是第3行</h2> </body> |

运行后效果

重点:每个组件的id一般是唯一的,class可以通用
下方代码被认为是不规范的

|-----|---------------------------------------------------------------|
| 1 2 | <h1 id=``"css1"``>我是第2行</h1> <h2 id=``"css1"``>我是第3行</h2> |

下方代码被认为是允许的

|-----|---------------------------------------------------------------------|
| 1 2 | <h1 class=``"css1"``>我是第2行</h1> <h2 class=``"css1"``>我是第3行</h2> |

相关推荐
我的xiaodoujiao5 小时前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
复利人生 复利日知录 赋能循环6 小时前
2026年复利精进:我的每日觉醒与成长密码
学习·思维模型·知识复利·复利·独立
sakiko_6 小时前
UIKit学习笔记4-使用UITableView制作滚动视图
笔记·学习·ios·swift·uikit
晓梦林7 小时前
MAZESEC-X1靶场学习笔记
笔记·学习
我的xiaodoujiao8 小时前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
TImCheng06098 小时前
职场人AI学习周期评估:不同学习路径的时间成本
人工智能·学习
周末也要写八哥8 小时前
C4D/Cinema 4D 2026超详细下载与安装教程(附资源包)
学习·数学建模
xinzheng新政8 小时前
openclaw dashboard报错 internal server error
学习
nashane9 小时前
HarmonyOS 6学习:RichEditor宽度“暴力”计算与富文本截图避坑
学习·harmonyos 5
鹿鸣悠悠9 小时前
【AI学习】全链路、并发、响应时间
学习