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

相关推荐
辰海Coding8 小时前
MiniSpring框架学习笔记-解决循环依赖的简化IoC容器
笔记·学习
晓梦林9 小时前
cp520靶场学习笔记
android·笔记·学习
心中有国也有家10 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
Upsy-Daisy10 小时前
AI Agent 项目学习笔记(八):Tool Calling 工具调用机制总览
人工智能·笔记·学习
LuminousCPP11 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
魔法阵维护师11 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#
_李小白12 小时前
【android opencv学习笔记】Day 17: 目标追踪(MeanShift)
android·opencv·学习
一只机电自动化菜鸟13 小时前
一建机电备考笔记(40) 建筑机电施工—排水管道施工(含考频+题型)
经验分享·笔记·学习·职场和发展·课程设计
2301_8187305613 小时前
numpy的学习(笔记)
学习·numpy
GHL28427109013 小时前
Logon failed, use ctrl+c to cancel basic credential prompt
学习·prompt