The CSS pseudo-class
The pseudo-class are keywords added to a selector that specifies a special state of a selector or a set of selectors. There are different types of pseudo-classes depending on the behavior we want to emulate.
The syntax
of a pseudo-class consist in a colon, :
and the name of the
pseudo-class. In case of the pseudo-class acts as a function, the keyword is combine
with a pair of parentheses and its arguments.
Display pseudo-classes
The display pseudo-classes enable select elements based on their display state like fullscreen, modal, etc…
:fullscreen
: This property matches elements that are in fullscreen mode, for example a video:modal
: This property matches elements are in modal state.:picture-in-picture
: This property is used for elements in PIP stateNOTE: This property is relative new and doesn't have full compatibility
Input pseudo-classes
The input pseudo-classes relate to form elements and enable selecting some HTML attributes after and before the user interacts with them.
:autofill
Match with an input element which was auto-filed by the browser. It means that the browser sets the login and password input values without the user interaction.:enabled
Any HTML element could be in enable or disable state. These states are more common in interactive elements like inputs or buttons. In this case, the enable pseudo-class discards any element with thedisable
attribute set to true.:disabled
Any HTML element in disable state.:read-only
The difference between aread-only
input and adisabled
input is that the read-only is sent with the form, and the disabled element is not.:read-write
Select elements which are editable by the user:placeholder-show
:checked
:indeterminate
Match input with the attribute indeterminate. This is the case of some inputs as a checkbox that we can’t determine if the value is true or false.:blank
:valid
,:invalid
anduser-invalid
The difference betweenuser-invalid
andinvalid
is that the first one requires an user interaction.in-range
and:out-of-range
:required
and:optional
. An input is required if it has the required attribute set to true (or not false)
Location pseudo-classes
These pseudo-classes are related to the states of the links. There are a lot of options:
any-link
,link
, andvisited
. These pseudo-classes affect links that were visited or not visited, visited and not visited respectively.local-link
: matches only links whose absolute URLs are the same as the target URL.:target
Matches elements that act as the target of a link. And:target-within
is the same, but in this case, the target element has a child who acts as another target.
Tree structural pseudo-classes
Represent the position of an element in the DOM tree
:root
:empty
:nth-child
:first-child
:only-child
:nth-of-type
User action pseudo-classes
These pseudo-classes respond to a user interaction like selecting or hovering an element
:hover
: Pass the mouse over an element.:active
: Matches when a user activates an element. An example could be a button that is activated while the user is pressing it with the mouse:focus
: Matches when the user sets the focus on an element.:focus-visible
: The main difference between focus and focus-visible is that the first one affects all elements, and the second one, only affects elements that the user agent considers should be visible that the user is focused on. For example, a button can be focused, but in most case, we don’t want it displays a ring around it. But, if the user navigates with the keyboard, the focus ring is mandatory, so we can use the focus-visible to only highlight the focus when it is required.:focus-within
: Affect to the focused element and a container element which has the focused element
Functional pseudo-classes
Accepts a selector or negation list as parameters
:is()
: Allows you to select any element that matches a variety of options:not()
: Allows you to discard selecting elements that match some selection:where()
: Like is, but without adding specify weight:has()
: Match any element that has relative selectors that match with the selectors inside the has function.
For examples go to W03-session