以下是20种CSS选择器的详细解析,帮助您精准定位页面元素,按使用频率和功能分类整理:
元素选择器
p { color: red; }
类选择器
.btn-primary { background: blue; }
class
属性匹配元素ID选择器
#header { height: 60px; }
id
属性匹配元素通配符选择器
* { margin: 0; }
后代选择器
div p { line-height: 1.5; }
子元素选择器
ul > li { list-style: none; }
相邻兄弟选择器
h2 + p { margin-top: 10px; }
通用兄弟选择器
h2 ~ p { color: #666; }
存在属性选择器
[disabled] { opacity: 0.5; }
精确匹配选择器
[type="email"] { border-color: blue; }
包含子串选择器
[class*="btn-"] { padding: 8px; }
开头匹配选择器
[href^="https"]::after { content: "