MyInternships.in

CSS References

CSS Pseudo-classes Reference

A pseudo-class is a keyword added to a selector that targets elements in a special state — being hovered, focused, checked, or occupying a particular position among siblings. Pseudo-classes start with a single colon. This reference groups them into interaction, structural, form and logical categories, each with an example.


Interaction (user action) pseudo-classes

Pseudo-classExampleDescription
:hovera:hoverElement the pointer is currently over.
:focusinput:focusElement that has keyboard/programmatic focus.
:focus-withinform:focus-withinElement containing a focused descendant.
:focus-visiblebutton:focus-visibleFocused element that should show a focus ring (usually keyboard).
:activebutton:activeElement being activated (pressed).
:visiteda:visitedA link the user has already visited.
:linka:linkAn unvisited link.
:target:targetElement whose id matches the current URL fragment.

Structural pseudo-classes

Pseudo-classExampleDescription
:root:rootThe root element (<html>); ideal for custom properties.
:first-childli:first-childFirst child among its siblings.
:last-childli:last-childLast child among its siblings.
:only-childp:only-childAn element with no siblings.
:nth-child(n)li:nth-child(2n)Matches by position; accepts even, odd, 3n+1, etc.
:nth-last-child(n)li:nth-last-child(1)Position counted from the end.
:first-of-typep:first-of-typeFirst element of its type among siblings.
:last-of-typep:last-of-typeLast element of its type among siblings.
:nth-of-type(n)p:nth-of-type(odd)Matches by position among the same element type.
:only-of-typeimg:only-of-typeThe only element of its type among siblings.
:emptydiv:emptyAn element with no children or text.

Form and input pseudo-classes

Pseudo-classExampleDescription
:checkedinput:checkedA checked checkbox, radio or selected option.
:disabledinput:disabledA disabled form control.
:enabledinput:enabledAn enabled form control.
:requiredinput:requiredA control with the required attribute.
:optionalinput:optionalA control without the required attribute.
:validinput:validA control whose value passes validation.
:invalidinput:invalidA control whose value fails validation.
:in-rangeinput:in-rangeA number/date within its min–max range.
:out-of-rangeinput:out-of-rangeA value outside its allowed range.
:read-onlyinput:read-onlyA control the user cannot edit.
:placeholder-showninput:placeholder-shownAn input currently showing its placeholder.
indeterminate:indeterminateA checkbox/radio in an indeterminate state.

Logical and other pseudo-classes

Pseudo-classExampleDescription
:not(x)p:not(.lead)Matches elements that do NOT match selector x.
:is(a, b):is(h1, h2) aMatches if the element matches any selector in the list.
:where(a, b):where(h1, h2) aLike :is() but adds zero specificity.
:has(x)article:has(img)Parent selector — matches if it contains x.
:lang(x)p:lang(fr)Elements in a particular language.
:defaultbutton:defaultThe default control in a form or group.
:dir(x):dir(rtl)Elements with a given text direction.
💡

Order link pseudo-classes as :link, :visited, :hover, :active (LoVe-HAte) so later rules correctly override earlier ones.

ℹ️

:has() is the long-awaited parent selector and is now supported in all modern browsers, enabling styles that react to an element's contents.

Related CSS Topics

Keep learning with these closely related tutorials.

Ready to use your CSS skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships