Home👇Khóa học📖<Tuhoc.cc/>

CSS

selectors

cheatsheet

# > . , * + ~ : { } ( ) [ ]

TypeSelector:a{ }

a
b
c

IDSelector:#a{ }

#a
#b
#c

*IDis not recommended as a selector

ChildSelector:a > b{ }

a
b
c
a
b
a
b

DescendantSelector:a b{ }

a
b
c
a
b
a
b

Combine Descendant & IDSelector:#a b{ }

#a
b
c
a
b
a
b

ClassSelector:.a{ }

.a
.b
.c

Combine the ClassSelector:b.x{ }

a.x
b.x
c.x

Comma CombinatorSelector:a, c{ }

a
b
c
d

UniversalSelector:*{ }

a
b
c
d

Combine UniversalSelector:a *{ }

a
b
c
a
b
a
b

Adjacent SiblingSelector:a + b{ }

a
b
b
b

General SiblingSelector:a ~ b{ }

a
b
b
c
b

First Child PseudoSelector:b:first-child{ }

b
b
b

or

a
b
b

first-child

* In the second diagram,first-childis'a'element, not'b'element. So there is nothing to be selected

Only Child PseudoSelector:

b:only-child{ } or

a :only-child{ }

a
b
a
b
b
a
a
b
c

Last Child PseudoSelector:b:last-child{ }

b
b
b

or

last-child

b
b
c

* In the second diagram,last-childis'c'element, not'b'element. So there is nothing to be selected.

Nth Child PseudoSelector:


b:nth-child(2){ }

1

2

3

b
b
b

or

1

2

3

a
b
b

or

1

2

3

b
a
b

* In the third diagram,nth-child(2)is'a'element, not'b'element. So there is nothing to be selected.


a :nth-child(2){ }

a
a
b
c

Nth Last ChildSelector:


a :nth-last-child(2){ } or

c:nth-last-child(2){ }

a
a
b
c
d

4

3

2

1




a:nth-last-child(2){ } or

b:nth-last-child(2){ } or

d:nth-last-child(2){ }

a
a
b
c
d

4

3

2

1

* In this diagram,nth-last-child(2)is'c'element, not'b'element. So there is nothing to be selected.

First of TypeSelector:b:first-of-type{ }

a
b
a
b

Nth of TypeSelector:


a:nth-of-type(2){ }

1

2

3

4

a
b
a
b
a
b
a


a:nth-of-type(even){ }

1

2

3

4

a
b
a
b
a
b
a


a:nth-of-type(odd){ }

1

2

3

4

a
b
a
b
a
b
a


a:nth-of-type(2n+1){ }

1

2

3

4

a
b
a
b
a
b
a

2n+1

*nis an every positive integer or zero value.

Only of TypeSelector:b:only-of-type{ }

a
b
a
b
b
a
a
b
c

Last of TypeSelector:


b:last-of-type{ }

a
b
b



a :last-of-type{ }

a
b
b
b

or

a
b
b
c
c
d



.x:last-of-type{ }

a
b
c
b.x
c.x
b.x
c.x

or

a
b.x
c.x
b.x
c.x
b
c

* Those items won’t be selected as no.xis presented

EmptySelector:a:empty{ }

a
a
a
b
b

hello

* No children elements or text.

Negation Pseudo-classSelector:


a:not(.x){ }

a
b
a.x
b.x


a:not(:last-of-type){ }

a
b
a
a

AttributeSelector:


[for]{ }

a
a[x]
a[y]
a[z]


a[for]{ }

a
a[x]
b[y]
c[z]

Attribute ValueSelector:a[for=“x”]{ }

a
a[x]
a[y]
a[z]

Attribute StartsSelector:[for^=“x”]{ }

[x]
[xy]
[yz]
[zx]

Attribute EndsSelector:[for$=“x”]{ }

[x]
[xy]
[yz]
[zx]

Attribute WildcardSelector:[for*=“x”]{ }

[x]
[xy]
[yz]
[zx]

* the end *

This cheatsheet is desinged for a quick search on CSS selectors :) There are so many CSS selectors with unfamiliar symbols, > . , * + ~ [ ] etc, so I am often confused with how CSS selectors work. I wish this infographic helps you find proper CSS selectors. Print this PDF out and stick it on the wall. And if you need more details on CSS selectors, check out links below;

* Happy codesign today! *