Pseudo classes
Last updated
Was this helpful?
Last updated
Was this helpful?
target element by their state than their tag name or attributes. As such they are quite useful for providing feedback to user's actions. There are many different standard and experimental pseudo-classes, but there are ones that get used more often than others.
An <a>
can have different states: the two most basic are :link
and :visited
. The pseudo-class refers to any <a>
tag with an href
attribute that had not been visited by the user. Whereas the pseudo-class refers to any <a>
that has already been visited by the user.
The :link
and :visited
pseudo-classes are often associated with the :hover
and :active
pseudo-classes, which we will talk about in the next section. Because these styles have equal specificity, they can be easily overridden by another. So it is important to follow the LVHA-order (:link
, :visited
, :hover
, :active
)
The mouse can trigger many states of an element, as such there are many different pseudo-classes that can be used. Two of the most common are :hover
and :active
. The pseudo-class matches an element when the user interacts with it using pointing device, typically by moving the mouse cursor over the element. The pseudo-class targets an element that is being activated by user, which typically occurs when the use is pressing down on the primary mouse button.
While generally associated with <a>
and <button>
elements, the :hover
and :active
pseudo-classes can be applied to most elements.
The position an element appears among its siblings can also be targeted. Pseudo-classes in the group can be extremely useful when targeting certain elements within a large group.
A form, and specifically the inputs of a form, may have several different states. The following pseudo-classes can be used to target the different states of form inputs.
The and will target the first element and the last element, respectively, of a group of sibling elements if it matches the prefix selector.
The matches elements based on their position in a group of siblings. This pseudo-class will take a single argument that describes the pattern for matching the element. The values can be a number, a keyword (odd
, even
) or a functional notation.
The pseudo-class represents an element that has received focus, which is generally triggered by the clicking on or tabbing to the element.
The pseudo-class represents any <input>
, <select>
, or <textarea>
that has the required
attribute. NOTE: The required
attribute is used to force the user to provide a value for that specific form element before the form will be submitted.
The pseudo-class represents any <input>
, <select>
, or <textarea>
that does not have the required
attribute.
The pseudo-class represents any <input>
or form element whose value validates successfully.
The pseudo-class represents any <input>
or form element whose value fails to validate.
The pseudo-class represents any <input>
or <textarea>
that is not editable by the user.