CSS Selectors Playground
An interactive playground for learning and testing CSS selectors in a visual way.
Base
The type selector matches elements based on their type.
The
.classselector selects all elements with the given class name.The
#idselector matches the element with the given id.The universal selector
*matches elements of any type.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
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.The descendant combinator matches an element that is a descendant of another specified element.
The child combinator
>matches elements if they are direct children of the element on the left side of the combinator.The adjacent sibling combinator
+matches if the second element immediately follows the first element.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
The
:not(selector list)pseudo-class matches elements that do not match any of the selectors in the selector list.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.
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.
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
The
[attribute]selector matches elements that have the specified attribute, regardless of its value.The
[attribute=value]selector matches all elements that have the specified attribute with a value exactly equal to a certain value.The
[attr^=value]selector matches all elements with the specified attribute whose value starts with a certain value.The
[attr$=value]selector matches all elements with the specified attribute whose value ends with with a certain value.The
[attribute*=value]selector matches all elements that have the specified attribute and the attribute value contains the specified substring.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.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
The
:hoverpseudo-class matches elements that are currently being hovered over by the mouse cursor. This also matches parent elements of the hovered element.The
:activepseudo-class matches elements that are currently activated by the user.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.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.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.The
:focus-withinpseudo-class matches elements that are either focused themselves or have a descendant that is focused.The
:checkedpseudo-class matches checkboxes, radio buttons and select options that are currently checked / selected.The
:indeterminatepseudo-class matches checkboxes, radio buttons and progress bars in indeterminate state.The
:enabledpseudo-class matches elements that are not disabled, like inputs or buttons.The
:disabledpseudo-class matches elements that are disabled, like inputs or buttons.The
:read-onlypseudo-class matches elements that are read-only, i.e. not editable.The
:defaultpseudo-class matches checkboxes, radio buttons, and select options that are checked/selected by default.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.The
:targetpseudo-class uses the URL's fragment (the part of the URL after the # symbol) and finds the element with this id.The
:placeholder-shownpseudo-class matches elements with a placeholder text that is currently visible.The
:autofillpseudo-class matches an input if it has been autofilled by the browser.
Validation
The
:in-rangepseudo-class matches elements whose value is within a range specified by theminandmaxattributes.The
:out-of-rangepseudo-class matches elements whose value is out of a range specified by theminandmaxattributes.The
:validpseudo-class matches elements that are in a valid state.The
:invalidpseudo-class matches elements that are in an invalid state.The
:user-validpseudo-class is similar to :valid, but it only matches elements in a valid state after the user has interacted with them.The
:user-inalidpseudo-class is similar to :invalid, but it only matches elements in an invalid state after the user has interacted with them.The
:requiredpseudo-class matches input elements that are required.The
:optionalpseudo-class matches input elements that are optional.
Content
The
:emptypseudo-class matches elements that have no content.The
:lang()pseudo-class matches elements based on the language of their content.The
:dir()pseudo-class matches elements based on the directionality of their text content.
Relations
The
:first-childpseudo-class matches an element that is the first child element of its parent.The
:last-childpseudo-class matches an element that is the last child element of its parent.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.The
:first-of-typepseudo-class matches the first element of its type among siblings.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.The
:only-of-typepseudo-class matches an element if it's the only element of its type among its siblings.The
:nth-child()pseudo-class matches elements based on their position among a group of siblings, starting with 1 as the first element.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.The
:nth-child(odd)selector matches all odd elements.The
:nth-child(even)selector matches all even elements.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.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.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.The
:nth-last-child()pseudo-class matches elements based on their position among a group of siblings, counting from the end.The
:nth-of-type()pseudo-class matches elements based on their position among siblings of the same type.The
:nth-last-of-type()pseudo-class matches elements based on their position among siblings of the same type.