CSS Pseudo-classes

CSS pseudo-classes are used to define the special state of an element, such as when a user interacts with it or when an element matches a certain condition. These are represented by a colon (:) followed by the pseudo-class name.

The general syntax for using a pseudo-class is:

</>
Copy
selector:pseudo-class {
    property: value;
}

Below is a comprehensive list of all CSS pseudo-classes:

Pseudo-classDescription
:hoverApplies when the user hovers over an element.
:focusApplies when an element gains focus (e.g., an input field).
:activeApplies when an element is being activated (e.g., clicked).
:visitedTargets links that have been visited.
:linkTargets links that have not been visited.
:first-childApplies to the first child of a parent element.
:last-childApplies to the last child of a parent element.
:nth-child(n)Targets elements that are the nth child of their parent.
:nth-last-child(n)Targets elements that are the nth child from the end of their parent.
:only-childApplies to an element that is the only child of its parent.
:first-of-typeApplies to the first element of a specific type within its parent.
:last-of-typeApplies to the last element of a specific type within its parent.
:nth-of-type(n)Targets elements that are the nth of their type within their parent.
:nth-last-of-type(n)Targets elements that are the nth of their type from the end within their parent.
:only-of-typeApplies to an element that is the only one of its type within its parent.
:emptyTargets elements that have no children, including text nodes.
:not(selector)Applies to elements that do not match the specified selector.
:rootTargets the root element of the document (usually html).
:checkedTargets checkboxes or radio buttons that are checked.
:disabledApplies to elements that are disabled.
:enabledApplies to elements that are enabled.
:requiredTargets input fields that are required.
:optionalTargets input fields that are optional.
:validApplies to input fields with valid values.
:invalidApplies to input fields with invalid values.
:in-rangeTargets input fields with values within a specified range.
:out-of-rangeTargets input fields with values outside a specified range.
:read-onlyTargets input fields that are read-only.
:read-writeTargets input fields that are editable.
:placeholder-shownTargets input fields showing placeholder text.
:targetApplies to an element targeted by a URL fragment (e.g., #section1).
:first-letterApplies to the first letter of a block-level element.
:first-lineApplies to the first line of a block-level element.
:beforeInserts content before an element’s content.
:afterInserts content after an element’s content.
:focus-withinApplies to an element if it or any of its descendants have focus.
:focus-visibleApplies when an element is focused and meets user-agent-specific criteria for focus visibility.
:is(selector)Matches any of the selectors in the list.
:where(selector)Matches any of the selectors but has no specificity.
:lang(language)Applies to elements with a matching lang attribute.
:has(selector)Applies to elements containing a specified descendant matching the selector.
CSS Pseudo-classes Table