Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Module: CSS Selectors #28062

Merged
merged 36 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c2e2ea4
new selectors module page
estelle Jun 23, 2023
9911797
guide prose
estelle Jun 24, 2023
93aa559
creation of selector structure page
estelle Jun 26, 2023
40246e9
pseudo element module
estelle Jun 26, 2023
4acea89
lots of work on css pseudos. The selector structure and selectors and…
estelle Jun 27, 2023
2743f9b
i think i finished structure and selectors and combinators. woot.
estelle Jun 27, 2023
f8c56f9
more selectors in other specs
estelle Jun 27, 2023
0d24bfe
finish selector structure
estelle Jun 27, 2023
5f0e1fa
remove conflicting docs
estelle Jul 11, 2023
28325d9
Merge branch 'main' into selectors2
estelle Jul 11, 2023
4fbdb17
css selectors
estelle Jul 11, 2023
1070d8d
Merge branch 'main' into selectors2
estelle Jul 11, 2023
076e923
merge
estelle Jul 11, 2023
b2f60b8
editss to pseudo
estelle Jul 13, 2023
557bd0c
Merge branch 'main' into selectors2
estelle Jul 18, 2023
037b3dc
remove odd files from PR
estelle Jul 18, 2023
ac21e9c
edits to selectors
estelle Jul 19, 2023
c2fed1d
finish selectors
estelle Jul 19, 2023
30be86d
redirects
estelle Jul 19, 2023
a3ac2e7
redirects
estelle Jul 19, 2023
8bb4278
Merge branch 'main' into selectors2
estelle Jul 19, 2023
97aebbc
/en-US/docs/Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_s…
estelle Jul 19, 2023
40bb6b6
/en-US/docs/Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_s…
estelle Jul 19, 2023
508dd33
/en-US/docs/Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_s…
estelle Jul 19, 2023
e21f3d0
damn urls
estelle Jul 19, 2023
e15116b
js
estelle Jul 19, 2023
05f1594
intro
estelle Jul 19, 2023
31e6950
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 20, 2023
11d866d
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
044e850
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
dd8b38b
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
b10ad6d
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
7e0857d
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
08ce023
Apply suggestions from code review
estelle Jul 21, 2023
5e74c44
remove redundant links
estelle Jul 21, 2023
987449a
Update files/en-us/web/css/css_selectors/index.md
estelle Jul 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 157 additions & 118 deletions files/en-us/web/css/css_selectors/index.md
Original file line number Diff line number Diff line change
@@ -1,166 +1,205 @@
---
title: CSS selectors
slug: Web/CSS/CSS_Selectors
page-type: landing-page
slug: Web/CSS/CSS_selectors
page-type: css-module
spec-urls: https://drafts.csswg.org/selectors/
---

{{CSSRef("Selectors")}}

**CSS selectors** define the pattern to select elements to which a set of CSS rules are then applied.

CSS selectors can be grouped into the following categories based on the type of elements they can select.

## Basic selectors

- [Universal selector](/en-US/docs/Web/CSS/Universal_selectors)

- : Selects all elements. Optionally, it may be restricted to a specific namespace or to all namespaces.

**Syntax:** `*` `ns|*` `*|*`

**Example:** `*` will match all the elements of the document.

- [Type selector](/en-US/docs/Web/CSS/Type_selectors)

- : Selects all elements that have the given node name.

**Syntax:** `elementname`

**Example:** `input` will match any {{HTMLElement("input")}} element.

{{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 provide additional pseudo-class selectors and pseudo-elements.
estelle marked this conversation as resolved.
Show resolved Hide resolved

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 which DOM nodes to return as a [`NodeList`](/en-US/docs/Web/API/NodeList).
estelle marked this conversation as resolved.
Show resolved Hide resolved

Selectors, whether used in CSS or JS, enable targeting HTML elements based on their type, attributes, current states, and even position in the DOM. Combinators enable being more precise in the elements you select by enabling specifying elements based on their relationship to other elements.
estelle marked this conversation as resolved.
Show resolved Hide resolved
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Reference

### Combinators and seperators
estelle marked this conversation as resolved.
Show resolved Hide resolved

- {{CSSXref("Adjacent_sibling_combinator","+")}} ([next-, or adjacent-", sibling combinator](/en-US/docs/Web/CSS/Adjacent_sibling_combinator))
- {{CSSXref("Child_combinator", ">")}} ([child combinator](/en-US/docs/Web/CSS/Child_combinator))
- {{CSSXref("||")}} ([column combinator](/en-US/docs/Web/CSS/Column_combinator)) {{Experimental_Inline}}
- {{CSSXref("General_sibling_combinator", "~")}} ([general-, or subsequent-, sibling combinator](/en-US/docs/Web/CSS/General_sibling_combinator))
- {{CSSXref("descendant_combinator", " ")}} ([descendant combinator](/en-US/docs/Web/CSS/Descendant_combinator))
- [`|`](/en-US/docs/Web/CSS/Namespace_separator) ([namespace separator](/en-US/docs/Web/CSS/Namespace_separator))
estelle marked this conversation as resolved.
Show resolved Hide resolved

### Selectors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can we name this as Pseudo-class selectors and move the list starting from "Attribute selectors" to a separate section ### Selectors?
  • Also perhaps we can first list ### Selectors (Attribute to Universal bullets) and then ### Pseudo-class selectors
Suggested change
### Selectors
### Pseudo-class selectors


- {{CSSXref(":active")}}
- {{CSSXref(":any-link")}}
- {{CSSXref(":autofill")}}
- {{CSSXref(":blank")}}
- {{CSSXref(":buffering")}}
- {{CSSXref(":checked")}}
- {{CSSXref(":closed")}}
estelle marked this conversation as resolved.
Show resolved Hide resolved
- {{CSSXref(":current")}}
- {{CSSXref(":current", ":current()")}}
Comment on lines +35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these both seem to be the same page so maybe have just one entry?

- {{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")}}
- {{CSSXref( ":is", ":matches()")}}
estelle marked this conversation as resolved.
Show resolved Hide resolved
- {{CSSXref(":modal")}}
- {{CSSXref(":muted")}}
- {{CSSXref(":not", ":not()")}}
- {{CSSXref(":nth-child", ":nth-child()")}}
- {{CSSXref(":nth-col", ":nth-col()")}}
- {{CSSXref(":nth-last-child", ":nth-last-child()")}}
- {{CSSXref(":nth-last-col", ":nth-last-col()")}}
- {{CSSXref(":nth-last-of-type", ":nth-last-of-type()")}}
- {{CSSXref(":nth-of-type", ":nth-of-type()")}}
- {{CSSXref(":only-child")}}
- {{CSSXref(":only-of-type")}}
- {{CSSXref(":open")}}
estelle marked this conversation as resolved.
Show resolved Hide resolved
estelle marked this conversation as resolved.
Show resolved Hide resolved
- {{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)
Comment on lines +96 to 97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors)
- [Class selector](/en-US/docs/Web/CSS/Class_selectors)
### Selectors
- [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors)
- [Class selectors](/en-US/docs/Web/CSS/Class_selectors)


- : Selects all elements that have the given `class` attribute.

**Syntax:** `.classname`

**Example:** `.index` will match any element that has `class="index"`.

- [ID selector](/en-US/docs/Web/CSS/ID_selectors)
- [Type selector](/en-US/docs/Web/CSS/Type_selectors)
- [Universal selector](/en-US/docs/Web/CSS/Universal_selectors)
estelle marked this conversation as resolved.
Show resolved Hide resolved

- : Selects an element based on the value of its `id` attribute. There should be only one element with a given ID in a document.

**Syntax:** `#idname`

**Example:** `#toc` will match the element that has `id="toc"`.

- [Attribute selector](/en-US/docs/Web/CSS/Attribute_selectors)

- : Selects all elements that have the given attribute.

**Syntax:** `[attr]` `[attr=value]` `[attr~=value]` `[attr|=value]` `[attr^=value]` `[attr$=value]` `[attr*=value]`

**Example:** `[autoplay]` will match all elements that have the `autoplay` attribute set (to any value).

## Grouping selectors
## Definitions
estelle marked this conversation as resolved.
Show resolved Hide resolved

- {{glossary("Pseudo-class")}} glossary term
- [Functional pseudo class](/en-US/docs/Web/CSS/Pseudo-classes#functional_pseudo-classes)
- [Combinator](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#combinators)
estelle marked this conversation as resolved.
Show resolved Hide resolved
- [Simple selector](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#simple-selector)
- [Compound selector](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#compound-selector)
- [Complex selector](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#complex-selector)
- [Relative selector](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#relative-selector)
- [Selector list](/en-US/docs/Web/CSS/Selector_list)
- {{CSSXref("Specificity")}}
estelle marked this conversation as resolved.
Show resolved Hide resolved

- : The `,` selector is a grouping method that selects all the matching nodes.

**Syntax:** `A, B`

**Example:** `div, span` will match both {{HTMLElement("span")}} and {{HTMLElement("div")}} elements.

## Combinators

- [Descendant combinator](/en-US/docs/Web/CSS/Descendant_combinator)

- : The " " (space) combinator selects nodes that are descendants of the first element.

**Syntax:** `A B`

**Example:** `div span` will match all {{HTMLElement("span")}} elements that are inside a {{HTMLElement("div")}} element.

- [Child combinator](/en-US/docs/Web/CSS/Child_combinator)

- : The `>` combinator selects nodes that are direct children of the first element.

**Syntax:** `A > B`
## Guides

**Example:** `ul > li` will match all {{HTMLElement("li")}} elements that are nested directly inside a {{HTMLElement("ul")}} element.
- [CSS selectors and combinators](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators)

- [General sibling combinator](/en-US/docs/Web/CSS/General_sibling_combinator)
- : Overview of the different types of simple selectors and the various combinators defined in the CSS selectors and pseudo modules.
estelle marked this conversation as resolved.
Show resolved Hide resolved

- : The `~` combinator selects siblings. This means that the second element follows the first (though not necessarily immediately), and both share the same parent.
- [CSS selector structure](/en-US/docs/Web/CSS/CSS_selectors/Selector_structure)

**Syntax:** `A ~ B`
- : The structure of CSS selectors and the terminology introduced in the selectors module, from "simple selector" to "forgiving relative selector list".
estelle marked this conversation as resolved.
Show resolved Hide resolved

**Example:** `p ~ span` will match all {{HTMLElement("span")}} elements that follow a {{HTMLElement("p")}}, immediately or not.
- [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have this here, it is not really a guide? We have it covered in the previous list. Also we are not listing other selector types here in the Guides section


- [Adjacent sibling combinator](/en-US/docs/Web/CSS/Adjacent_sibling_combinator)

- : The `+` combinator matches the second element only if it _immediately_ follows the first element.

**Syntax:** `A + B`

**Example:** `h2 + p` will match the first {{HTMLElement("p")}} element that _immediately_ follows an {{HTMLElement("Heading_Elements", "h2")}} element.

- [Column combinator](/en-US/docs/Web/CSS/Column_combinator) {{Experimental_Inline}}

- : The `||` combinator selects nodes which belong to a column.

**Syntax:** `A || B`

**Example:** `col || td` will match all {{HTMLElement("td")}} elements that belong to the scope of the {{HTMLElement("col")}}.

## Pseudo-classes and pseudo-elements
- : Selects elements that have the given attribute, with optional substring values.

- [Pseudo classes](/en-US/docs/Web/CSS/Pseudo-classes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment - do we need to list it here? We have it covered in the previous list

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept pseudo and expanded the explaination


- : The `:` pseudo allow the selection of elements based on state information that is not contained in the document tree.
- : Pseudo-classes allow the selection of elements based on state information that is not contained in the document tree.

**Example:** `a:visited` will match all {{HTMLElement("a")}} elements that have been visited by the user.
- [Learn: CSS selectors](/en-US/docs/Learn/CSS/Building_blocks/Selectors)

- [Pseudo elements](/en-US/docs/Web/CSS/Pseudo-elements)
- : 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).

- : The `::` pseudo represent entities that are not included in HTML.
- [Using the `:target` pseudo-class in selectors](/en-US/docs/Web/CSS/CSS_selectors/Using_the_:target_pseudo-class_in_selectors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to after "CSS selector structure"? That way all of the pages under "CSS_selectors" will be listed together followed by all the Learn guides.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this wasn't so unrelated it would make sense, but I am unclear why target is where it is.


**Example:** `p::first-line` will match the first line of all {{HTMLElement("p")}} elements.
- : Learn how to use the {{CSSXref(":target")}} pseudo-class to style the target element a URL's fragment identifier.

## Structure of a selector
- [Learn: UI pseudo-classes](/en-US/docs/Learn/Forms/UI_pseudo-classes)

The term 'selector' can refer to one of the following:
- : Learn the different UI pseudo-classes available for styling forms in different states.

- Simple selector
- [Locating DOM elements using selectors](/en-US/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors)

- : A selector with a single component, such as a single id selector or type selector, that's not used in combination with or contains any other selector component or combinator. A given element is said to match a simple selector when that simple selector accurately describes the element. All [basic selectors](#basic_selectors), attributes, and single [pseudo-classes and pseudo-elements](#pseudo-classes_and_pseudo-elements) are simple selectors.
- : The selectors API enables using selectors in JavaScript to retrieve element nodes from the DOM.

- Compound selector
## Related concepts
estelle marked this conversation as resolved.
Show resolved Hide resolved

- : A sequence of [simple selectors](#simple_selector) that are not separated by a [combinator](#combinators). A compound selector represents a set of simultaneous conditions on a single element. A given element is said to match a compound selector when the element matches all the simple selectors in the compound selector.
- CSS scoping module

In a compound selector, the [type selector](/en-US/docs/Web/CSS/Type_selectors) or a [universal selector](/en-US/docs/Web/CSS/Universal_selectors) in a compound selector must come first in the sequence of selectors. Only one type selector or universal selector is allowed in the sequence. Since whitespace represents the [descendant combinator](/en-US/docs/Web/CSS/Descendant_combinator), no whitespace is allowed between the simple selectors in a compound selector.
- {{CSSXref(":host")}} pseudo-class
- {{CSSXref(":host_function", ":host()")}} pseudo-class
- {{cssxref(":host-context", ":host-context()")}} pseudo-class
- {{CSSXref("::slotted")}} pseudo-element

**Example:** `a#selected {...}`
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo) (representing entities not included in HTML)

- Complex selector
- {{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")}}

- : A sequence of one or more simple and/or [compound selectors](#compound_selector) that are separated by [combinators](#combinators). A complex selector represents a set of simultaneous conditions on a set of elements. These conditions apply in the context of relationships described by the combinators. A given element is said to match a complex selector when the element matches compound selectors and the combinators between the compound selectors.
- [CSS shadow parts module](/en-US/docs/Web/CSS/CSS_shadow_parts)

**Examples:** `a#selected > .icon {...}`, `.box h2 + p {...}`, `a .icon {...}`
- {{CSSXref("::part")}} pseudo-element

- Relative selector
- [CSS positioned layout module](/en-US/docs/Web/CSS/CSS_positioned_layout)

- : A selector representing an element relative to one or more anchor elements preceded by a combinator. Relative selectors that don't begin with an explicit [combinator](#combinators) have an implied [descendant combinator](/en-US/docs/Web/CSS/Descendant_combinator).
- {{CSSxRef("::backdrop")}}

**Examples:** `+ div#topic > #reference {...}`, `> .icon {...}`, `dt:has(+ img) ~ dd {...}`
- Other [pseudo-elements](/en-US/docs/Web/CSS/Pseudo-elements)

- [Selector list](/en-US/docs/Web/CSS/Selector_list)
- {{CSSxRef("::cue")}}
- {{CSSxRef("::cue-region")}}

- : A comma-separated list of [simple](#simple_selector), [compound](#compound_selector), or [complex](#complex_selector) selectors. If the constituent selector type of a selector list is important but unspecified, it is called a _complex selector list_. A given element is said to match a selector list when the element matches any (at least one) of the selectors in that selector list. Read more about when a selector list is deemed [invalid](/en-US/docs/Web/CSS/Selector_list#invalid_selector_list) and how to construct a [forgiving selector list](/en-US/docs/Web/CSS/Selector_list#forgiving_selector_list).
- {{CSSXref("@namespace")}} at-rule

**Example:** `#main, article.heading {...}`
- {{cssxref("important", "!important")}}
- {{cssxref("Specificity")}}
- [The cascade](/en-US/docs/Web/CSS/Cascade)
estelle marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("Document.querySelector")}} method
- {{domxref("Document.querySelectorAll")}} method
- {{domxref("NodeList.forEach()")}} method

## Specifications

{{Specifications}}

See the [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes#specifications) and [pseudo-element](/en-US/docs/Web/CSS/Pseudo-elements#specifications) specification tables for details on those.

## See also

- [`:has()` pseudo class](/en-US/docs/Web/CSS/:has)
- [CSS Specificity](/en-US/docs/Web/CSS/Specificity)
- [Selector list](/en-US/docs/Web/CSS/Selector_list)
- [CSS pseudo-element module](/en-US/docs/Web/CSS/CSS_pseudo)
- [CSS cascade and inheritance module](/en-US/docs/Web/CSS/CSS_cascade)
- [Cascade origin and importance](/en-US/docs/Web/CSS/Cascade)
estelle marked this conversation as resolved.
Show resolved Hide resolved
- [Using shadow DOM](/en-US/docs/Web/API/Web_components/Using_shadow_DOM)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Using the :target pseudo-class in selectors
slug: Web/CSS/CSS_Selectors/Using_the_:target_pseudo-class_in_selectors
slug: Web/CSS/CSS_selectors/Using_the_:target_pseudo-class_in_selectors
page-type: guide
---

Expand Down