-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
203 lines (157 loc) · 8.43 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
---
title: CSS selectors
slug: Web/CSS/CSS_selectors
page-type: css-module
spec-urls: https://drafts.csswg.org/selectors/
---
{{CSSRef}}
The **CSS selectors** module defines the patterns to select elements to which a set of CSS rules are then applied along with their {{cssxref("specificity")}}. The CSS selectors module provides us with more than 60 selectors and five combinators. [Other modules](#related_concepts) provide additional pseudo-class selectors and pseudo-elements.
In CSS, selectors are patterns used to match, or select, the elements you want to style. Selectors are also used in JavaScript to enable selecting the DOM nodes to return as a [`NodeList`](/en-US/docs/Web/API/NodeList).
Selectors, whether used in CSS or JavaScript, enable targeting HTML elements based on their type, attributes, current states, and even position in the DOM. Combinators allow you to be more precise when selecting elements by enabling selecting elements based on their relationship to other elements.
## Reference
### Combinators and separators
- `+` ([Next-sibling combinator](/en-US/docs/Web/CSS/Next-sibling_combinator))
- `>` ([Child combinator](/en-US/docs/Web/CSS/Child_combinator))
- `||` ([Column combinator](/en-US/docs/Web/CSS/Column_combinator))
- `~` ([Subsequent sibling combinator](/en-US/docs/Web/CSS/Subsequent-sibling_combinator))
- " " ([Descendant combinator](/en-US/docs/Web/CSS/Descendant_combinator))
- `|` ([Namespace separator](/en-US/docs/Web/CSS/Namespace_separator))
### Selectors
- {{CSSXref(":active")}}
- {{CSSXref(":any-link")}}
- {{CSSXref(":autofill")}}
- {{CSSXref(":blank")}}
- {{CSSXref(":buffering")}}
- {{CSSXref(":checked")}}
- {{CSSXref(":current")}}
- {{CSSXref(":current", ":current()")}}
- {{CSSXref(":default")}}
- {{CSSXref(":defined")}}
- {{CSSXref(":dir", ":dir()")}}
- {{CSSXref(":disabled")}}
- {{CSSXref(":empty")}}
- {{CSSXref(":enabled")}}
- {{CSSXref(":first-child")}}
- {{CSSXref(":first-of-type")}}
- {{CSSXref(":focus")}}
- {{CSSXref(":focus-visible")}}
- {{CSSXref(":focus-within")}}
- {{CSSXref(":fullscreen")}}
- {{CSSXref(":future")}}
- {{CSSXref(":has", ":has()")}}
- {{CSSXref(":hover")}}
- {{CSSXref(":indeterminate")}}
- {{CSSXref(":in-range")}}
- {{CSSXref(":invalid")}}
- {{CSSXref(":is", ":is()")}}
- {{CSSXref(":lang", ":lang()")}}
- {{CSSXref(":last-child")}}
- {{CSSXref(":last-of-type")}}
- {{CSSXref(":link")}}
- {{CSSXref(":local-link")}}
- `:matches()` (obsolete legacy selector alias for {{CSSXref( ":is", ":is()")}})
- {{CSSXref(":modal")}}
- {{CSSXref(":muted")}}
- {{CSSXref(":not", ":not()")}}
- {{CSSXref(":nth-child", ":nth-child()")}}
- {{CSSXref(":nth-of-type", ":nth-of-type()")}}
- {{CSSXref(":nth-last-child", ":nth-last-child()")}}
- {{CSSXref(":nth-last-of-type", ":nth-last-of-type()")}}
- {{CSSXref(":only-child")}}
- {{CSSXref(":only-of-type")}}
- {{CSSXref(":optional")}}
- {{CSSXref(":out-of-range")}}
- {{CSSXref(":past")}}
- {{CSSXref(":paused")}}
- {{CSSXref(":picture-in-picture")}}
- {{CSSXref(":placeholder-shown")}}
- {{CSSXref(":playing")}}
- {{CSSXref(":read-only")}}
- {{CSSXref(":read-write")}}
- {{CSSXref(":required")}}
- {{CSSXref(":root")}}
- {{CSSXref(":scope")}}
- {{CSSXref(":seeking")}}
- {{CSSXref(":stalled")}}
- {{CSSXref(":target")}}
- {{CSSXref(":target-within")}}
- {{CSSXref(":user-invalid")}}
- {{CSSXref(":user-valid")}}
- {{CSSXref(":valid")}}
- {{CSSXref(":visited")}}
- {{CSSXref(":volume-locked")}}
- {{CSSXref(":where", ":where()")}}
- [`:-webkit-` pseudo-classes](/en-US/docs/Web/CSS/WebKit_Extensions#pseudo-classes)
- [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors)
- [Class selector](/en-US/docs/Web/CSS/Class_selectors)
- [ID selectors](/en-US/docs/Web/CSS/ID_selectors)
- [Type selectors](/en-US/docs/Web/CSS/Type_selectors)
- [Universal selectors](/en-US/docs/Web/CSS/Universal_selectors)
## Terms
- {{glossary("Pseudo-class")}} glossary term
- [Functional pseudo-classes](/en-US/docs/Web/CSS/Pseudo-classes#functional_pseudo-classes)
- [Combinators](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#combinators)
- [Simple selector](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure#simple_selector)
- [Compound selector](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure#compound_selector)
- [Complex selector](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure#complex_selector)
- [Relative selector](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure#relative_selector)
- [Selector list](/en-US/docs/Web/CSS/Selector_list)
- [Specificity](/en-US/docs/Web/CSS/Specificity)
## Guides
- [CSS selectors and combinators](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators)
- : Overview of the different types of simple selectors and various combinators defined in the CSS selectors and the CSS pseudo modules.
- [CSS selector structure](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure)
- : Explanation of the structure of CSS selectors and the terminologies introduced in the CSS selectors module, ranging from "simple selector" to "forgiving relative selector list".
- [Pseudo classes](/en-US/docs/Web/CSS/Pseudo-classes)
- : Lists the pseudo-classes, selectors that allow the selection of elements based on state information that is not contained in the document tree, defined in the various CSS modules and HTML.
- [Learn: CSS selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors)
- : Part of CSS building blocks, includes tutorials on [Type, class, and ID selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors/Type_Class_and_ID_Selectors), [Attribute selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors), [Pseudo-classes and pseudo-elements](/en-US/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements), [Combinators](/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators), [Cascade, specificity, and inheritance](/en-US/docs/Learn/CSS/Building_blocks/Cascade_and_inheritance), and [Cascade layers](/en-US/docs/Learn/CSS/Building_blocks/Cascade_layers).
- [Using the `:target` pseudo-class in selectors](/en-US/docs/Web/CSS/CSS_selectors/Using_the_:target_pseudo-class_in_selectors)
- : Learn how to use the {{CSSXref(":target")}} pseudo-class to style the target element a URL's fragment identifier.
- [Learn: UI pseudo-classes](/en-US/docs/Learn/Forms/UI_pseudo-classes)
- : Learn the different UI pseudo-classes available for styling forms in different states.
- [Locating DOM elements using selectors](/en-US/docs/Web/API/Document_Object_Model/Locating_DOM_elements_using_selectors)
- : The selectors API enables using selectors in JavaScript to retrieve element nodes from the DOM.
## Related concepts
- {{CSSXref(":popover-open")}} pseudo-class
- {{CSSXref(":state","state()")}} pseudo-class
- [CSS nesting](/en-US/docs/Web/CSS/CSS_nesting) module
- : [`&` nesting selector](/en-US/docs/Web/CSS/Nesting_selector)
- [CSS scoping](/en-US/docs/Web/CSS/CSS_scoping) module
- {{CSSXref(":host")}} pseudo-class
- {{CSSXref(":host_function", ":host()")}} pseudo-class
- {{cssxref(":host-context", ":host-context()")}} pseudo-class
- {{CSSXref("::slotted")}} pseudo-element
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo-elements) (representing entities not included in HTML)
- {{CSSXref("::after")}}
- {{CSSXref("::before")}}
- {{CSSXref("::file-selector-button")}}
- {{CSSXref("::first-letter")}}
- {{CSSXref("::first-line")}}
- {{CSSXref("::grammar-error")}}
- {{CSSXref("::marker")}}
- {{CSSXref("::placeholder")}}
- {{CSSXref("::selection")}}
- {{CSSXref("::spelling-error")}}
- {{CSSXref("::target-text")}}
- [CSS shadow parts module](/en-US/docs/Web/CSS/CSS_shadow_parts)
- {{CSSXref("::part")}} pseudo-element
- [CSS positioned layout module](/en-US/docs/Web/CSS/CSS_positioned_layout)
- {{CSSxRef("::backdrop")}}
- Other [pseudo-elements](/en-US/docs/Web/CSS/Pseudo-elements)
- {{CSSxRef("::cue")}}
- {{CSSxRef("::cue-region")}}
- {{CSSXref("@namespace")}} at-rule
- {{cssxref("important", "!important")}}
- [Specificity](/en-US/docs/Web/CSS/Specificity)
- [Cascade](/en-US/docs/Web/CSS/Cascade)
- {{domxref("Document.querySelector")}} method
- {{domxref("Document.querySelectorAll")}} method
- {{domxref("NodeList.forEach()")}} method
## Specifications
{{Specifications}}
## See also
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo-elements)
- [CSS cascade and inheritance module](/en-US/docs/Web/CSS/CSS_cascade)
- [CSS nesting module](/en-US/docs/Web/CSS/CSS_nesting)
- [Using shadow DOM](/en-US/docs/Web/API/Web_components/Using_shadow_DOM)