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
.classselector selects all elements with the given class name. - ID
The
#idselector matches the element with the given id. - Universal (any)
The universal selector
*matches elements of any type. - Root
The
:rootpseudo-class matches the root element of a document, for HTML documents this is always thehtmlelement.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
.classselector 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
:hoverpseudo-class matches elements that are currently being hovered over by the mouse cursor. This also matches parent elements of the hovered element. - Active
The
:activepseudo-class matches elements that are currently activated by the user. - Visited
The
:visitedpseudo-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
:focuspseudo-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 thetabindexattribute to make them focusable. - Focus (visible)
The
:focus-visiblepseudo-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-withinpseudo-class matches elements that are either focused themselves or have a descendant that is focused. - Checked
The
:checkedpseudo-class matches checkboxes, radio buttons and select options that are currently checked / selected. - Indeterminate
The
:indeterminatepseudo-class matches checkboxes, radio buttons and progress bars in indeterminate state. - Enabled
The
:enabledpseudo-class matches elements that are not disabled, like inputs or buttons. - Disabled
The
:disabledpseudo-class matches elements that are disabled, like inputs or buttons. - Read-only
The
:read-onlypseudo-class matches elements that are read-only, i.e. not editable. - Default
The
:defaultpseudo-class matches checkboxes, radio buttons, and select options that are checked/selected by default. - Defined
The
:definedpseudo-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
:targetpseudo-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-shownpseudo-class matches elements with a placeholder text that is currently visible. - Autofill
The
:autofillpseudo-class matches an input if it has been autofilled by the browser.
Validation
- In Range
The
:in-rangepseudo-class matches elements whose value is within a range specified by theminandmaxattributes. - Out of Range
The
:out-of-rangepseudo-class matches elements whose value is out of a range specified by theminandmaxattributes. - Valid
The
:validpseudo-class matches elements that are in a valid state. - Invalid
The
:invalidpseudo-class matches elements that are in an invalid state. - User Valid
The
:user-validpseudo-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-inalidpseudo-class is similar to :invalid, but it only matches elements in an invalid state after the user has interacted with them. - Required
The
:requiredpseudo-class matches input elements that are required. - Optional
The
:optionalpseudo-class matches input elements that are optional.
Content
Relations
- First Child
The
:first-childpseudo-class matches an element that is the first child element of its parent. - Last Child
The
:last-childpseudo-class matches an element that is the last child element of its parent. - Only Child
The
:only-childpseudo-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-typepseudo-class matches the first element of its type among siblings. - Last Of Type
The
:last-of-typepseudo-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-typepseudo-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 variablenwhich starts at 0 and increments by 1 for every element.The first result is
3n = 3 * 0 = 0which doesn't match any element (because the first element is 1). The second result is3n = 3 * 1 = 3which 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 variablenwhich 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 variablenwhich 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.