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> |

相关推荐
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码3 天前
嵌入式学习路线
学习
毛小茛3 天前
计算机系统概论——校验码
学习
babe小鑫3 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms3 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下3 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。3 天前
2026.2.25监控学习
学习
im_AMBER3 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J3 天前
从“Hello World“ 开始 C++
c语言·c++·学习