CSS Selectors Playground
An interactive playground for learning and testing CSS selectors in a visual way.
Base
- Type (node name)
The type selector matches elements based on their type.
- CSS Class
The
.class
selector selects all elements with the given class name. - ID
The
#id
selector matches the element with the given id. - Universal (any)
The universal selector
*
matches elements of any type. - Root
The
:root
pseudo-class matches the root element of a document, for HTML documents this is always thehtml
element.The selector is often used to define global CSS custom properties.
Combinators
- Selector List
The selector list
,
is a list of CSS selectors separated by commas. It selects all the elements that match any of the selectors in the list. - Descendant
The descendant combinator matches an element that is a descendant of another specified element.
- Direct Child
The child combinator
>
matches elements if they are direct children of the element on the left side of the combinator. - Adjacent Sibling
The adjacent sibling combinator
+
matches if the second element immediately follows the first element. - General Sibling
The general sibling combinator
~
matches the elements that are siblings of a given element. It is similar to the adjacent sibling combinator, but it doesn't require the elements to be immediately after each other.
General
- Not
The
:not(selector list)
pseudo-class matches elements that do not match any of the selectors in the selector list. - Has
The
:has(selector list)
pseudo-class function matches an element if any of the selectors passed as parameter match at least one element.The selector is currently not supported in Firefox.
- Is
The
:is(selector list)
pseudo-class function accepts a list of selectors as argument. It matches all elements that match any of the selectors passed as arguments.The selector is currently not supported in Firefox.
- Where
The
:where(selector list)
pseudo-class function accepts a list of selectors as argument. It matches all elements that match any of the selectors passed as arguments.The selector is currently not supported in Firefox.
Attributes
- Attribute presence
The
[attribute]
selector matches elements that have the specified attribute, regardless of its value. - Attribute value
The
[attribute=value]
selector matches all elements that have the specified attribute with a value exactly equal to a certain value. - Attribute value starts with
The
[attr^=value]
selector matches all elements with the specified attribute whose value starts with a certain value. - Attribute value ends with
The
[attr$=value]
selector matches all elements with the specified attribute whose value ends with with a certain value. - Attribute value contains
The
[attribute*=value]
selector matches all elements that have the specified attribute and the attribute value contains the specified substring. - Attribute contains value in list
The
[attr~=value]
selector matches all elements with the specified attribute whose value contains a list of space-separated values and one of the values is exactlyvalue
.Note: This is a contrived example. Use the
.class
selector to select elements by class instead. - Attribute contains value in list (hyphen)
The
[attr|=value]
selector matches all elements with the specified attribute whose value contains a list of hyphen-separated values and the first value is exactlyvalue
.Note: This is a contrived example. In practice, you should use the language selector.
State
- Hover
The
:hover
pseudo-class matches elements that are currently being hovered over by the mouse cursor. This also matches parent elements of the hovered element. - Active
The
:active
pseudo-class matches elements that are currently activated by the user. - Visited
The
:visited
pseudo-class matches hyperlinks that you visited before. For privacy reasons, the selector has many limitations. You can only style a limited set of properties, likecolor
. Reading the color value of the element via JavaScript will return the value of an unvisited link. Querying visited links via JavaScript is not possible. In some browsers the styles may even be limited to hyperlinks of the same domain. - Focus
The
:focus
pseudo-class matches the element that is currently focused. Interactive elements like buttons and links can be focused by default. For non-interactive elements, you can add thetabindex
attribute to make them focusable. - Focus (visible)
The
:focus-visible
pseudo-class matches the element that is currently focused, if the focus state is visible. Whether the focus state is visible depends on the input method used to focus the element and the element itself. - Focus (within)
The
:focus-within
pseudo-class matches elements that are either focused themselves or have a descendant that is focused. - Checked
The
:checked
pseudo-class matches checkboxes, radio buttons and select options that are currently checked / selected. - Indeterminate
The
:indeterminate
pseudo-class matches checkboxes, radio buttons and progress bars in indeterminate state. - Enabled
The
:enabled
pseudo-class matches elements that are not disabled, like inputs or buttons. - Disabled
The
:disabled
pseudo-class matches elements that are disabled, like inputs or buttons. - Read-only
The
:read-only
pseudo-class matches elements that are read-only, i.e. not editable. - Default
The
:default
pseudo-class matches checkboxes, radio buttons, and select options that are checked/selected by default. - Defined
The
:defined
pseudo-class matches any element that is defined. This matches standard element built in to the browser and custom elements that have been defined usingcustomElements.define()
. Custom elements that have not (yet) been defined will not match this pseudo-class. - Target
The
:target
pseudo-class uses the URL's fragment (the part of the URL after the # symbol) and finds the element with this id. - Placeholder shown
The
:placeholder-shown
pseudo-class matches elements with a placeholder text that is currently visible. - Autofill
The
:autofill
pseudo-class matches an input if it has been autofilled by the browser.
Validation
- In Range
The
:in-range
pseudo-class matches elements whose value is within a range specified by themin
andmax
attributes. - Out of Range
The
:out-of-range
pseudo-class matches elements whose value is out of a range specified by themin
andmax
attributes. - Valid
The
:valid
pseudo-class matches elements that are in a valid state. - Invalid
The
:invalid
pseudo-class matches elements that are in an invalid state. - User Valid
The
:user-valid
pseudo-class is similar to :valid, but it only matches elements in a valid state after the user has interacted with them. - User Invalid
The
:user-inalid
pseudo-class is similar to :invalid, but it only matches elements in an invalid state after the user has interacted with them. - Required
The
:required
pseudo-class matches input elements that are required. - Optional
The
:optional
pseudo-class matches input elements that are optional.
Content
Relations
- First Child
The
:first-child
pseudo-class matches an element that is the first child element of its parent. - Last Child
The
:last-child
pseudo-class matches an element that is the last child element of its parent. - Only Child
The
:only-child
pseudo-class matches an element if it's the only child of its parent. It's the short form of:first-child:last-child
. - First Of Type
The
:first-of-type
pseudo-class matches the first element of its type among siblings. - Last Of Type
The
:last-of-type
pseudo-class matches the last element of its type among siblings. If no type is provided, it will match the last element of any type. - Only Of Type
The
:only-of-type
pseudo-class matches an element if it's the only element of its type among its siblings. - Position nth-child()
The
:nth-child()
pseudo-class matches elements based on their position among a group of siblings, starting with 1 as the first element. - Position nth-child(n)
The
:nth-child()
pseudo-class supports a counter variablen
which starts at 0 and increments by 1 for every element.The first result is
3n = 3 * 0 = 0
which doesn't match any element (because the first element is 1). The second result is3n = 3 * 1 = 3
which matches the third element in the list. - Position nth-child(odd)
The
:nth-child(odd)
selector matches all odd elements. - Position nth-child(even)
The
:nth-child(even)
selector matches all even elements. - Position nth-child(n) with offset
The
:nth-child()
pseudo-class supports a counter variablen
which starts at 0 and increments by 1 for every element. It allows you to define an offset before matching the first element. - Position nth-child(n) with limit
The
:nth-child()
pseudo-class supports a counter variablen
which starts at 0 and increments by 1 for every element. It allows you limit the number of elements being matched. - Position nth-child(n of <selector>)
The
:nth-child()
pseudo-class supports anof <selector>
syntax. It finds the elements matching the specified selector and then matches the elements based in their position in that filtered set of elements.