完整的 CSS 选择器速查表:带图片的实用指南😍

2025-06-04

完整的 CSS 选择器速查表:带图片的实用指南😍

CSS 选择器看似颇具挑战性,因为它们提供了丰富的选项来选择需要设置样式的 HTML 元素,因此了解各种可用选项及其工作原理至关重要。此外,选择器还允许您根据元素、类、ID、属性,甚至伪元素的结构及其与 HTML 中其他元素的关系来定位它们。


📒 因此,我提供了 CSS 选择器的完整指南/速成课程/备忘单(无论您怎么称呼它),以使您的生活更轻松。


基本选择器

姓名 CSS 描述 结果
Universal Selector * Select all elements 结果
Type div Select elements of that type` 结果
Class .c Select elements with that class 结果
Id #i Select elements with that id 结果

选择器组合

姓名 CSS         描述 结果
Descendant div a Select elements that are descendants of the first element 结果
Direct Child div > a Select elements that are direct children of the first element 结果
General Sibling div ~ a Select elements that are siblings of the first element and come after the first element 结果
Adjacent Sibling div + a Select elements that are siblings of the first element and come directly after the first element 结果
Or div, a Select elements that match any selector in the list 结果
And div.c Select elements that match all the selector combinations 结果

属性

姓名 CSS     描述 结果
Has [a] Select elements that have that attribute 结果
Exact [a=”1”] Select elements that have that attribute with exactly that value 结果
Begins With [a^=”1”] Select elements that have that attribute which start with that value 结果
Ends With [a$=”1”] Select elements that have that attribute which end with that value 结果
Substring [a*=”1”] Select elements that have that attribute which contain that value anywhere 结果

伪元素选择器

姓名 CSS       描述 结果
Before div::before Creates an empty element directly before the children of selected element 结果
After div::after Creates an empty element directly after the children of selected element 结果

伪类状态

姓名 CSS       描述
Hover button:hover Select elements that are hovered by the mouse
Focus button:focus Select elements that are focused.
Required input:required Select inputs that are required
Checked input:checked Select checkboxes/radio buttons that are checked
Disabled input:disabled Select inputs that are disabled

伪类位置/其他

姓名 CSS 描述 结果
First Child a:first-child Select elements that are the first child inside a container 结果
Last Child a:last-child Select elements that are the last child inside a container 结果
Nth Child a:nth-child(2n) Select elements that are the nth child inside a container based on the formula 结果
Nth Last Child a:nth-last-child(3) Select elements that are the nth child inside a container based on the formula counting from the end 结果
Only Child a:only-child Select elements that are the only child inside a container 结果
First Of Type a:first-of-type Select elements that are the first of a type inside a container 结果
Last Of Type a:last-of-type Select elements that are the last of a type inside a container 结果
Nth Of Type a:nth-of-type(2n) Select elements that are the nth of a type inside a container based on the formula 结果
Nth Last Of Type a:nth-last-of-type(2) Select elements that are the nth of a type inside a container based on the formula counting from the end 结果
Only Of Type a:only-of-type Select elements that are the only of a type inside a container 结果
Not a:not(.c) Select all elements that do not match the selector inside the not selector 结果

结论

我希望我能快速地向你解释完所有这些 CSS 选择器。如果文章中有任何错误或我遗漏了什么,请在评论区指出。

感谢阅读本文。我们下篇文章再见👨‍💻

文章来源:https://dev.to/arafat4693/complete-css-selector-cheat-sheet-a-hands-on-guide-with-images-5ff1
PREV
抛弃 div 标签:使用这些 HTML 标签
NEXT
Git 速成课程