HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

CSS Tutorial

CSS Introduction

What is CSSCSS SyntaxHow to Add CSSCSS Comments

CSS Foundations

Cascade & InheritanceCSS SpecificityCSS UnitsBox SizingCSS VariablesCSS Math Functions

CSS Selectors

CSS Element SelectorCSS Class SelectorCSS ID SelectorCSS Universal SelectorCSS Group SelectorCSS CombinatorsCSS Attribute SelectorsCSS Pseudo ClassesCSS Pseudo Elements

CSS Colors and Backgrounds

CSS ColorsCSS HEX ColorsCSS RGB ColorsModern ColorsCSS Background ColorCSS Background ImageLayered Backgrounds

CSS Text & Fonts

CSS Text ColorCSS Text AlignmentCSS Text DecorationCSS Font FamilyCSS Font SizeWeb Fonts & TypographyText Wrapping

CSS Box Model

CSS MarginCSS PaddingCSS BorderCSS OutlineCSS Width and Height

CSS Layout

Normal FlowCSS DisplayCSS PositionAnchor PositioningCSS FloatCSS OverflowCSS Z-indexIntrinsic SizingAspect Ratio & Object FitMulti-column Layout

CSS Flexbox

Flex ContainerFlex DirectionJustify ContentAlign ItemsFlex WrapFlex Item Sizing

CSS Grid

CSS Grid ContainerCSS Grid ColumnsCSS Grid RowsCSS Grid GapCSS Grid Layout ExampleGrid PlacementGrid AreasCSS Subgrid

CSS Lists, Tables & Forms

CSS ListsCSS TablesCSS Form StylingCSS Input Fields

CSS Effects

CSS ShadowsCSS GradientsCSS FiltersCSS OpacityClipping & MaskingBlending & BackdropScroll Snap

CSS Animations & Transitions

CSS TransformCSS 2D TransformCSS 3D TransformCSS TransitionsEntry & Exit TransitionsCSS AnimationsScroll AnimationsView TransitionsMotion Paths

CSS Responsive Design

CSS Media QueriesResponsive LayoutMobile First DesignContainer QueriesPreference Media QueriesFeature Queries

Modern CSS Authoring

Cascade LayersNesting & ScopeLogical PropertiesGenerated Content

CSS Accessibility

Accessible CSSCSS ThemingModern Form StylingCSS User Interface

CSS Output & Performance

Print StylesCSS PerformanceCSS ArchitectureDebugging CSS

CSS Reference

CSS At-rulesCSS FunctionsCSS Tutorial PDF

CSS Projects

Responsive CSS Project
  1. Home
  2. /
  3. Tutorials
  4. /
  5. CSS Tutorial
  6. /
  7. CSS Pseudo Classes

CSS Selectors

CSS Pseudo Classes

CSS Pseudo Classes are used to define a special state of an HTML element.

They allow you to style elements based on:

  • User interaction
  • Element position
  • Form state
  • Dynamic conditions

Pseudo classes start with a colon (:) and are widely used to create interactive and responsive user interfaces. The supporting concepts are explained in CSS Attribute Selectors and CSS Pseudo Elements.

Common examples include:

  • :hover
  • :focus
  • :first-child
  • :last-child
  • :nth-child()

Syntax

CSS Pseudo Classes Syntax

css

selector:pseudo-class {
    property: value;
}

CSS Pseudo Classes Example

css

a:hover {
    color: red;
}

Changes the link color when the mouse pointer hovers over it.

Attributes

Pseudo ClassDescriptionExample
:hoverStyles an element when hovereda:hover
:focusStyles an element when focusedinput:focus
:first-childSelects the first child elementp:first-child
:last-childSelects the last child elementp:last-child
:nth-child()Selects a specific childli:nth-child(2)

Match elements that are currently open

The :open pseudo-class matches elements with an open and closed state, including <details>, <dialog>, and supported picker controls. It describes the live state rather than the presence of an HTML attribute, so it remains accurate when the state changes through user interaction or browser behavior.

Focused example

css

details:open {
  border-color: #2563eb;
}

dialog:open {
  opacity: 1;
  transform: translateY(0);
}

Example

CSS Pseudo Classes Complete Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Pseudo Classes Example</title>
    <style>
        a:hover {
            color: red;
        }
        input:focus {
            border: 2px solid blue;
            outline: none;
        }
        li:first-child {
            color: green;
            font-weight: bold;
        }
        li:last-child {
            color: orange;
        }
        li:nth-child(2) {
            color: purple;
        }
    </style>
</head>
<body>
    <a href="#">Hover over this link</a>
    <br><br>
    <input type="text" placeholder="Click here">
    <br><br>
    <ul>
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
        <li>React</li>
    </ul>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
:hover11212
:open13313313626.5

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

Notes

  • Pseudo classes are dynamic and respond to user actions.
  • :hover is commonly used for buttons, links, and menus.
  • :focus improves accessibility and form usability.
  • :nth-child() supports formulas.

Example:

nth-child Formula Example

css

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

Styles all odd-numbered list items.

Popular Pseudo Classes

Pseudo ClassUse Case
:hoverMouse interaction
:focusActive form field
:activeClicked element
:first-childFirst item in a group
:last-childLast item in a group
:nth-child()Specific item selection

Conclusion

CSS Pseudo Classes allow you to style elements based on their state, position, or user interaction. They are essential for creating interactive, accessible, and visually engaging websites without using JavaScript.

PreviousCSS Attribute SelectorsNextCSS Pseudo Elements