1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Pseudo Classes
45% OFFBLACK FRIDAY SALE
Sale ends in
03Days
12Hrs
21Mins
31Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Pseudo Classes

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

03Days
12Hrs
21Mins
31Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Pseudo-Classes

Overview

HTML tags are static, but the user's interaction with the webpage is deeply dynamic. Pseudo-classes allow CSS to 'listen' for these temporary states (like hovering over a button or clicking a link) and apply styles conditionally, all without writing a single line of JavaScript. They act as automated state machines built directly into the CSS engine.

Syntax

css
/* 1. Interaction States */
.btn:hover { background: blue; }     /* User's mouse is over the element */
.btn:active { transform: scale(0.9); } /* The exact moment the user clicks */
.input:focus { border: 2px solid gold; } /* User is typing inside the input */

/* 2. Structural States (DOM Navigation) */
/* Target the very first <li> in a list */
ul li:first-child { border-top: none; }

/* Target every even-numbered row (Stripe effect!) */
tr:nth-child(even) { background: #eee; }

/* Target the 3rd <li> specifically */
li:nth-child(3) { color: red; }

/* 3. Form Validation States */
input:invalid { border-color: red; }
input:disabled { opacity: 0.5; }

Common Pitfalls

  • The 'LoVe HAte' rule for styling links. If you write your pseudo-classes in the wrong order, they will override each other and break. You must declare them exactly in this order: :link, :visited, :hover, :active (LVHA).
  • Confusing :nth-child with :nth-of-type. p:nth-child(2) selects the paragraph ONLY if it is literally the second child in its parent div. If there is an <h1> above it, the paragraph is the second child. But if there are two <h1>s above it, it fails. p:nth-of-type(2) ignores all other elements and strictly finds the second paragraph.

Interview Questions

What is the accessibility issue with using :hover to reveal critical information (like a dropdown menu)?

Mobile touch screens do not have a 'hover' state. A user's finger is either touching the screen (:active) or not. If you hide critical UI behind a pure :hover state, mobile users can never access it.

Real-World Example

Using structural pseudo-classes to build a responsive CSS Grid without manual classes.

example
css
.grid { display: grid; gap: 10px; }

/* 
  If a card is the very first child, make it span the entire row!
  Every subsequent child will just act normally.
*/
.grid .card:first-child {
  grid-column: 1 / -1; 
  font-size: 1.5rem;
}

Check Your Knowledge

Test your understanding of Pseudo-Classes with these quick questions.

Question 1

Which pseudo-class targets an element the exact moment a user clicks and holds down their mouse button on it?

Take CSS Quiz
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Pseudo-Classes

Overview

HTML tags are static, but the user's interaction with the webpage is deeply dynamic. Pseudo-classes allow CSS to 'listen' for these temporary states (like hovering over a button or clicking a link) and apply styles conditionally, all without writing a single line of JavaScript. They act as automated state machines built directly into the CSS engine.

Syntax

css
/* 1. Interaction States */
.btn:hover { background: blue; }     /* User's mouse is over the element */
.btn:active { transform: scale(0.9); } /* The exact moment the user clicks */
.input:focus { border: 2px solid gold; } /* User is typing inside the input */

/* 2. Structural States (DOM Navigation) */
/* Target the very first <li> in a list */
ul li:first-child { border-top: none; }

/* Target every even-numbered row (Stripe effect!) */
tr:nth-child(even) { background: #eee; }

/* Target the 3rd <li> specifically */
li:nth-child(3) { color: red; }

/* 3. Form Validation States */
input:invalid { border-color: red; }
input:disabled { opacity: 0.5; }

Common Pitfalls

  • The 'LoVe HAte' rule for styling links. If you write your pseudo-classes in the wrong order, they will override each other and break. You must declare them exactly in this order: :link, :visited, :hover, :active (LVHA).
  • Confusing :nth-child with :nth-of-type. p:nth-child(2) selects the paragraph ONLY if it is literally the second child in its parent div. If there is an <h1> above it, the paragraph is the second child. But if there are two <h1>s above it, it fails. p:nth-of-type(2) ignores all other elements and strictly finds the second paragraph.

Interview Questions

What is the accessibility issue with using :hover to reveal critical information (like a dropdown menu)?

Mobile touch screens do not have a 'hover' state. A user's finger is either touching the screen (:active) or not. If you hide critical UI behind a pure :hover state, mobile users can never access it.

Real-World Example

Using structural pseudo-classes to build a responsive CSS Grid without manual classes.

example
css
.grid { display: grid; gap: 10px; }

/* 
  If a card is the very first child, make it span the entire row!
  Every subsequent child will just act normally.
*/
.grid .card:first-child {
  grid-column: 1 / -1; 
  font-size: 1.5rem;
}

Check Your Knowledge

Test your understanding of Pseudo-Classes with these quick questions.

Question 1

Which pseudo-class targets an element the exact moment a user clicks and holds down their mouse button on it?

Take CSS Quiz

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A