ABCDEFGHIJKLMNOPQRSTUVWXYZ
1
Cheat-Sheet for CSS selectors
2
SelectorExampleExample descriptionCSS
3
.class.introSelects all elements with class="intro"1
4
#id#firstnameSelects the element with id="firstname"1
5
**Selects all elements2
6
elementpSelects all <p> elements1
7
element,elementdiv,pSelects all <div> elements and all <p> elements1
8
element elementdiv pSelects all <p> elements inside <div> elements1
9
element>elementdiv>pSelects all <p> elements where the parent is a
<div> element
2
10
element+elementdiv+pSelects all <p> elements that are placed immediately after <div> elements2
11
[attribute][target]Selects all elements with a target attribute2
12
[attribute=value][target=_blank]Selects all elements with target="_blank"2
13
[attribute~=value][title~=flower]Selects all elements with a title attribute containing the word "flower"2
14
[attribute|=language][lang|=en]Selects all elements with a lang attribute value starting with "en"2
15
:linka:linkSelects all unvisited links1
16
:visiteda:visitedSelects all visited links1
17
:activea:activeSelects the active link1
18
:hovera:hoverSelects links on mouse over1
19
:focusinput:focusSelects the input element which has focus2
20
:first-letterp:first-letterSelects the first letter of every <p> element1
21
:first-linep:first-lineSelects the first line of every <p> element1
22
:first-childp:first-childSelects every <p> elements that is the first child of its parent2
23
:beforep:beforeInsert content before every <p> element2
24
:afterp:afterInsert content after every <p> element2
25
:lang(language)p:lang(it)Selects every <p> element with a lang attribute value starting with "it"2
26
element1~element2p~ulSelects every ul element that are preceded by a p element3
27
[attribute^=value]a[src^="https"]Selects every a element whose src attribute value begins with "https"3
28
[attribute$=value]a[src$=".pdf"]Selects every a element whose src attribute value ends with ".pdf"3
29
[attribute*=value]a[src*="w3schools"]Selects every a element whose src attribute value contains the substring "w3schools"3
30
:first-of-typep:first-of-typeSelects every p element that is the first p element of its parent3
31
:last-of-typep:last-of-typeSelects every p element that is the last p element of its parent3
32
:only-of-typep:only-of-typeSelects every p element that is the only p element of its parent3
33
:only-childp:only-childSelects every p element that is the only child of its parent3
34
:nth-child(n)p:nth-child(2)Selects every p element that is the second child of its parent3
35
:nth-last-child(n)p:nth-last-child(2)Selects every p element that is the second child of its parent, counting from the last child3
36
:nth-of-type(n)p:nth-of-type(2)Selects every p element that is the second p element of its parent3
37
:nth-last-of-type(n)p:nth-last-of- type(2)Selects every p element that is the second p element of its parent, counting from the last child3
38
:last-childp:last-childSelects every p element that is the last child of its parent3
39
:root:rootSelects the document’s root element3
40
:emptyp:emptySelects every p element that has no children (including text nodes)3
41
:target#news:targetSelects the current active #news element (clicked on a URL containing that anchor name)3
42
:enabledinput:enabledSelects every enabled input element3
43
:disabledinput:disabledSelects every disabled input element3
44
:checkedinput:checkedSelects every checked input element3
45
:not(selector):not(p)Selects every element that is not a p element3
46
::selection::selectionSelects the portion of an element that is selected by a user3
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100