This has been generated by ChatGPT, and here for my reference.
π§ CSS Selectors Cheat Sheet
Organized by selector type with descriptions, examples, and performance tips.
π© 1. Simple Selectors
Selector |
Description |
Example |
Matches |
Performance |
* |
Universal selector |
* |
All elements |
π΄ Slow |
element |
Type selector |
div |
All <div> elements |
π’ Fast |
#id |
ID selector |
#header |
Element with id="header"
|
π’ Fastest |
.class |
Class selector |
.btn |
Elements with class btn
|
π’ Fast |
[attr] |
Attribute present |
[disabled] |
Elements with that attribute |
π΄ Slow |
[attr=value] |
Attribute equals |
[type="text"] |
Attribute with exact value |
π‘ Moderate |
π¨ 2. Combinator Selectors (Structural Relationships)
Selector |
Description |
Example |
Matches |
Performance |
A B |
Descendant |
div p |
<p> inside <div> (any depth) |
π‘ Moderate |
A > B |
Child |
ul > li |
Direct child only |
π’ Fast |
A + B |
Adjacent sibling |
h1 + p |
First <p> after <h1>
|
π’ Fast |
A ~ B |
General sibling |
h1 ~ p |
All <p> after <h1>
|
π‘ Moderate |
πͺ 3. Functional Pseudo-Classes
Used for structural logic, conditions, and grouping.
Selector |
Description |
Example |
Matches |
Arguments |
Performance |
:nth-child(n) |
Nth child (any type) |
li:nth-child(2n) |
Every 2nd <li>
|
n , even , odd , 2n+1 etc. |
π‘ Moderate |
:nth-of-type(n) |
Nth child of same type |
p:nth-of-type(odd) |
Odd-numbered <p> elements |
Same as above |
π‘ Moderate |
:nth-last-child(n) |
Nth child from end |
li:nth-last-child(2) |
2nd-last <li>
|
Same as above |
π‘ Moderate |
:nth-last-of-type(n) |
Nth of type from end |
p:nth-last-of-type(1) |
Last <p> among siblings |
Same as above |
π‘ Moderate |
:first-child |
First child of parent |
div:first-child |
If first child is a <div>
|
β |
π‘ Moderate |
:last-child |
Last child of parent |
li:last-child |
If last child is a <li>
|
β |
π‘ Moderate |
:not(selector) |
Exclude matches |
:not(.active) |
All except .active
|
Any selector |
π΄ Slow |
:is(a, b) |
OR logic |
:is(h1, h2) |
h1 or h2
|
Comma-separated list |
π’ Fast |
:where(a, b) |
OR (zero specificity) |
:where(nav, aside) |
Like :is , but no specificity |
Comma-separated list |
π’ Fast |
:has(selector) |
Parent with matching child |
div:has(img) |
<div> with <img> inside |
Any selector |
π΄ Slow (limited) |
π’ Special Arguments for :nth-*
Functions
Argument |
Meaning |
Matches |
even |
Every even-numbered item |
2nd, 4th, 6th... |
odd |
Every odd-numbered item |
1st, 3rd, 5th... |
n |
All items (base pattern) |
0, 1, 2, 3... |
2n |
Every 2nd item |
2, 4, 6... |
2n+1 |
Odd items |
Same as odd
|
3n-1 |
Custom pattern |
2, 5, 8... |
π¦ 4. UI State Pseudo-Classes
Selector |
Description |
Example |
Matches |
Performance |
:hover |
On mouse hover |
a:hover |
Link when hovered |
π’ Fast |
:focus |
Element has focus |
input:focus |
Focused input |
π’ Fast |
:checked |
Checked input |
input:checked |
Checked checkbox or radio |
π’ Fast |
:disabled |
Disabled input/button |
button:disabled |
Disabled form controls |
π’ Fast |
:empty |
No content |
div:empty |
Elements with no children or text |
π‘ Moderate |
π₯ 5. Pseudo-Elements
Selector |
Description |
Example |
Matches |
Performance |
::before |
Insert content before |
p::before |
Virtual content before |
π’ Fast |
::after |
Insert content after |
p::after |
Virtual content after |
π’ Fast |
::marker |
List marker |
li::marker |
Bullet/number styling |
π’ Fast |
::placeholder |
Input placeholder style |
input::placeholder |
Placeholder text |
π’ Fast |
π Summary
Category |
Includes |
π© Simple Selectors |
* , element , .class , #id , [attr]
|
π¨ Combinators |
Descendant, Child, Siblings |
πͺ Functional Pseudo-classes |
:nth-* , :not() , :is() , :has()
|
π’ Special Arguments |
even , odd , n , 2n , 3n-1
|
π¦ UI State |
:hover , :focus , :checked , etc. |
π₯ Pseudo-elements |
::before , ::after , ::placeholder
|
Top comments (0)