Pseudo Classes
This lists all available pseudo classes (omitting some that is still experimental with no browser support).
Special
:not(...)
Selects elements that do not match the selector inside :not(...)
.
Structural
:root
Selects an element that is the root of the document (synonymous with the html
selector).
:empty
Selects an element without children (other than whitespace characters).
:first-child
Selects an element that is the first child of its siblings.
:last-child
Selects an element that is the last child of its siblings.
:only-child
Selects an element that has no siblings.
:nth-child(...)
Selects elements based on their order in a group of siblings.
If you use the An+B
notation, then it selects elements for n = 0, 1, 2, ... .
odd
— Select elements in odd positions (1, 3, 5, ...)even
— Select elements in even positions (2, 4, 6, ...)3
— Selects the 3rd element3n
— Selects elements in position 3, 4, 9 ...3n+1
— Selects elements in position 1, 4, 7, ...-n+3
— Select the first 3 elements (positions 3, 2, 1)
:nth-last-child(...)
Like :nth-child(...)
, but counts from the last sibling.
:first-of-type
Selects an element that is the first child of this type among its siblings.
:last-of-type
Selects an element that is the last child of this type among its siblings.
:only-of-type
Selects an element that has no other siblings of its type.
:nth-of-type(...)
Like :nth-child(...)
, but counts only elements of its type.
odd
— Select elements of this type in odd positions (1, 3, 5, ...)even
— Select elements of this type in even positions (2, 4, 6, ...)3
— Selects the 3rd element of this type3n
— Selects elements of this type in position 3, 4, 9 ...3n+1
— Selects elements of this type in position 1, 4, 7, ...-n+3
— Select the first 3 elements of this type (positions 3, 2, 1)
:nth-last-of-type(...)
Like :nth-of-type(...)
, but counts from the last sibling.
User action
:hover
Selects an element that is being hovered by the mouse pointer.
:focus
Selects an element that has focus ("currently highlighted"), e.g. by navigating to it using the Tab key
:active
Selects an element that is being activated ("being clicked") by the user, e.g. by clicking or pressing Enter on it
:focus-visible
Selects an element that has focus AND the browser determined that the "focus ring" should be visible.
(Use this instead of :focus
to not display the "focus ring" when the item has been clicked using the mouse.)
Particularly on Chrome:
:focus
selects the item being hightlighted AND when the item is activated by a mouse click.:focus-visible
selects the item being hightlighted, BUT NOT when the item is activated by a mouse click.
Use Chrome -- may not work on Firefox.
Try keyboard-navigation and mouse-click on these buttons.
:focus-within
Selects an element that has focus OR has a decendant that has focus.
Links
:any-link
Selects anything that would match :link
or :visited
.
:link
Selects unvisited links (<a>
, <area>
, or <link>
).
:visited
Selects visited links (<a>
, <area>
, or <link>
).
:target
Selects an element whose id
value is the same as the URL fragment (e.g. https://www.example.com/index#THISTHING
).
:target-within
Selects elements that would be selected by :target
, or has a descendant that would be selected by :target
.
Inputs
:disabled
Selects inputs that are disabled (has disabled
attribute).
:enabled
Selects inputs that are enabled.
:read-only
Selects inputs that are read only (has readonly
attribute).
:read-write
Selects inputs that are editable.
:required
Selects inputs that are required (has required
attribute).
:optional
Selects inputs that are optional.
:default
Selects inputs that are default among a set of elements.
Can be used on:
<input type="checkbox">
<input type="radio">
<option>
<button>
(if it's the form's default submit button).
:checked
Selects inputs that are checked/selected.
Can be used on:
<input type="checkbox">
<input type="radio">
<option>
:indeterminate
Selects inputs that are on indeterminate state.
Can be used on:
<input type="checkbox">
(when itsindeterminate
property is set using JavaScript)<input type="radio">
(when nothing in its group is selected)<progress>
:valid
Selects inputs with valid contents.
:invalid
Selects inputs with invalid contents.
:in-range
Selects inputs whose value is in range of its min
and max
attributes.
:out-of-range
Selects inputs whose value is out of the range of its min
and max
attributes.
Lingustics
:dir(...)
Selects elements based on the directionality of their text contents. (Even when you don't specify the dir="..."
global attribute on them.)
Specify ltr
or rtl
as its argument.
:lang(...)
Selects elements based on the language of their text contents.
The language can be determined by the lang="..."
attribute, <meta>
elements, or other information like HTTP headers.
I'm American
I'm Japanese
References
- Pseudo Classes (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
- :focus-visible (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible
- The :focus-visible Trick (CSS-Tricks) — https://css-tricks.com/the-focus-visible-trick/
- :default (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/:default
- :lang (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/:lang