C

CSS Tutorial

Clean • Professional

CSS Pseudo-classes

2 minute

CSS Pseudo-classes

When we design modern websites, we often need to style elements not only based on their structure but also based on their state, user interaction, or position in the DOM. That’s exactly where CSS pseudo-classes are super useful.

A pseudo-class is a keyword added to a selector that defines a special state of an element. It allows you to apply styles dynamically — for example, when a user hovers over a button, focuses on an input field, or when an element is the first or nth child of its parent.

Why Pseudo-classes Are Important?

  • They improve interactivity (hover effects, focus styles, active states).

  • They enhance user experience and accessibility.
  • They reduce the need for JavaScript for simple interactions.
  • They give designers fine control over styling specific elements.

Commonly Used CSS Pseudo-classes

:hover – When User Points at an Element

  • Triggered when the mouse pointer hovers over an element.
  • Commonly used for buttons, links, images, or interactive components.

Example:

button:hover {
  background-color: #007BFF;
  color: #fff;
}

:focus – When an Element Gains Focus

  • Applied when an element (like an input or button) is focused, either by mouse click or keyboard navigation (Tab key).
  • Helps accessibility by showing users which field is active.

Example:

input:focus {
  border: 2px solid blue;
  outline: none;
}

:active – While Element is Being Clicked

  • Represents the moment when a user clicks and holds down on an element (like a button or link).

Example:

a:active {
  color: red;
}

:nth-child() – Target Elements Based on Position

  • Lets you style elements based on their order within a parent.
  • Supports formulas like even, odd, or custom (2n+1) patterns.

Example:

li:nth-child(odd) {
  background: #f2f2f2;
}

:first-child and :last-child

  • :first-child → Styles the first element inside a parent.
  • :last-child → Styles the last element inside a parent.

Example:

p:first-child {
  font-weight: bold;
}
p:last-child {
  color: gray;
}

:checked – For Form Inputs

  • Applied to checkboxes, radio buttons, or select options when they are selected.

Example:

input[type="checkbox"]:checked {
  accent-color: green;
}

:not() – Negation Pseudo-class

  • Selects elements that do not match a certain selector.

Example:

​
p:not(.highlight) {
  color: black;
}

​

Flow of Common Pseudo-classes

learn code with durgesh images

CSS Pseudo-classes List

Pseudo-classExampleDescription
:activea:activeSelects the link while it is being clicked (active state).
:any-linka:any-link, area:any-linkSelects any <a> or <area> element with an href attribute (both visited and unvisited links).
:autofillinput:autofillSelects input elements that are auto-filled by the browser.
:checkedinput:checked, option:checkedSelects checked form elements like radio buttons, checkboxes, or selected options.
:defaultinput:default, button:default, option:defaultSelects the default element in a group (like default option in a dropdown).
:defined:definedMatches elements that are defined (standard HTML elements or custom web components).
:dir():dir(ltr), :dir(rtl)Selects elements based on text direction (left-to-right or right-to-left).
:disabledinput:disabled, option:disabledSelects disabled form fields (unusable).
:emptydiv:emptySelects elements that have no children or text.
:enabledinput:enabledSelects enabled form fields.
:first@page :firstApplies styles to the first page of a printed document.
:first-childp:first-childSelects an element if it is the first child of its parent.
:first-of-typep:first-of-typeSelects the first element of its type among siblings.
:focusinput:focusSelects an element when it gains focus (clicked or tabbed into).
:focus-visiblebutton:focus-visibleSelects focused elements only when triggered by keyboard, not mouse.
:focus-withinform:focus-withinSelects a parent element if it or any of its descendants are focused.
:fullscreen:fullscreenMatches elements currently in fullscreen mode.
:has()h2:has(+p)Selects elements that contain specific children or are followed by certain elements.
:hovera:hover, p:hoverApplies styles when a user hovers over an element with the mouse.
:in-rangeinput:in-rangeSelects inputs with values inside the allowed range.
:indeterminateinput:indeterminateSelects form elements in an indeterminate state (like a checkbox that is neither checked nor unchecked).
:invalidinput:invalidSelects form fields with invalid values.
:is():is(ul, ol)Simplifies selectors by grouping multiple element types.
:lang()p:lang(it)Selects elements with a specific language attribute.
:last-childli:last-childSelects the last child of a parent element.
:last-of-typep:last-of-typeSelects the last element of a specific type.
:left@page :leftApplies styles to left-hand pages when printing.
:linka:linkSelects unvisited links.
:modal:modalSelects elements in modal (dialog) state.
:not():not(p)Selects all elements except <p>.
:nth-child()p:nth-child(2)Selects the second child of a parent.
:nth-last-child()p:nth-last-child(2)Selects the second child from the end.
:nth-last-of-type()p:nth-last-of-type(2)Selects the second occurrence of an element type from the end.
:nth-of-type()p:nth-of-type(2)Selects the second <p> element among siblings.
:only-childp:only-childSelects elements that are the only child of a parent.
:only-of-typep:only-of-typeSelects elements that are the only of their type within a parent.
:optionalinput:optionalSelects form fields that are not required.
:out-of-rangeinput:out-of-rangeSelects inputs with values outside the allowed range.
:placeholder-showninput:placeholder-shownSelects inputs or textareas currently showing placeholder text.
:popover-open:popover-openSelects elements currently displaying as a popover.
:read-onlyinput:read-onlySelects inputs that cannot be edited.
:read-writeinput:read-writeSelects editable input fields.
:requiredinput:requiredSelects required form fields.
:right@page :rightApplies styles to right-hand pages when printing.
:root:rootSelects the root of the document (usually <html>).
:scope:scopeDefines the current scope for selectors (useful in nested queries).
:state():state(custom)Selects custom elements in a defined state.
:target:targetSelects the element with an id matching the current URL hash.
:user-invalid:user-invalidMatches invalid form elements after user interaction.
:user-valid:user-validMatches valid form elements after user interaction.
:validinput:validSelects valid form elements.
:visiteda:visitedSelects visited links.
:where():where(ul, ol)Similar to :is(), but with lower specificity.

 

 

Article 0 of 0