-
元素选择器(Type Selectors):直接通过元素类型进行选择,例如
div
,p
,h1
等。 -
类选择器(Class Selectors):通过元素的
class
属性选择元素,以点(.)开头,例如.classname
。 -
ID选择器(ID Selectors):通过元素的
id
属性选择元素,以井号(#)开头,例如#idname
。 -
属性选择器(Attribute Selectors):根据元素的属性及属性值来选择元素,例如
[type="text"]
。 -
伪类选择器(Pseudo-class Selectors):用于选择元素的特定状态,例如
:hover
,:active
,:first-child
等。 -
伪元素选择器(Pseudo-element Selectors):用于选择元素的特定部分,例如
::before
,::after
。
元素选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <template> <div> <h1>标题</h1> <p>这是一个段落。</p> </div> </template> <style> h1 { color: blue; } p { font-size: 16px; } </style> |
类选择器
1 2 3 4 5 6 7 8 9 10 11 12 | <template> <div> <p class = "highlight" >高亮的段落。</p> </div> </template> <style> .highlight { color: red; font-weight: bold; } </style> |
ID选择器
1 2 3 4 5 6 7 8 9 10 11 | <template> <div> <p id= "main-paragraph" >主要段落。</p> </div> </template> <style> #main-paragraph { background-color: yellow; } </style> |
属性选择器(不用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <template> <div> <input type= "text" placeholder= "输入文本" > <input type= "checkbox" > </div> </template> <style> input[type= "text" ] { border: 1px solid blue; } input[type= "checkbox" ] { width: 20px; height: 20px; } </style> |
伪类选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <template> <div> <a href= "#" >链接</a> </div> </template> <style> a:hover { color: green; } a:active { color: red; } </style> |
伪元素选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <template> <div> <p>欢迎来到我的网站。</p> </div> </template> <style> p::before { content: "【开始】" ; color: orange; } p::after { content: "【结束】" ; color: orange; } </style> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!