#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Pseudo-class

A pseudo-class is a keyword added to a selector with a single colon that targets an element in a particular state or position, such as a:hover or li:first-child. It lets you style things that are not written in the HTML: whether a link has been visited, whether an input has focus, or whether a list item is third in its list. You use pseudo-classes whenever styling should react to user interaction or document structure without extra classes.

State-based pseudo-classes

These match elements based on what the user is doing or what state a form control is in.

  • :hover matches while the pointer is over the element.
  • :active matches while the element is being pressed.
  • :focus matches the element that currently has keyboard focus. :focus-visible matches only when the browser decides a focus ring should show, usually for keyboard users, so mouse clicks on buttons do not leave an outline.
  • :checked, :disabled, :required, :invalid and :placeholder-shown describe form controls.
  • :link and :visited describe links that have and have not been visited.

Structural pseudo-classes and the an+b formula

Structural pseudo-classes pick elements by their position among siblings. :first-child, :last-child and :only-child are straightforward. :nth-child() takes a formula an+b: the browser counts n up from 0 and selects every resulting position. So 2n gives 2, 4, 6 (even rows), 2n+1 gives 1, 3, 5 (odd rows), and -n+3 gives the first three items. The keywords even and odd are shortcuts.

/* Zebra-striped table rows */
tbody tr:nth-child(even) { background: #f6f8fa; }

/* No divider after the last item */
.menu li:not(:last-child) { border-bottom: 1px solid #e5e5e5; }

/* Keyboard-only focus ring */
button:focus-visible { outline: 3px solid #2563eb; outline-offset: 2px; }

/* Style a label next to a checked checkbox */
input[type="checkbox"]:checked + label { text-decoration: line-through; }

/* Highlight a form field that contains an invalid value */
input:invalid:not(:placeholder-shown) { border-color: #dc2626; }

/* Links in the classic LVHA order */
a:link    { color: #1d4ed8; }
a:visited { color: #6d28d9; }
a:hover   { color: #1e40af; }
a:active  { color: #b91c1c; }

Logical helpers: :not(), :is() and :where()

:not() excludes elements matching its argument, which is cleaner than writing a rule and then undoing it. :is() groups several selectors so you avoid repetition: :is(h1, h2, h3) a means links inside any of those headings. :where() works like :is() but adds zero specificity, which makes it ideal for default styles that should be easy to override.

On specificity in general, a pseudo-class counts the same as a class. So a:hover beats a plain a rule but loses to .nav a.

Where people go wrong

The first confusion is :first-child versus :first-of-type. p:first-child matches a paragraph only if it is the very first child of its parent. If a heading comes first, no paragraph matches. p:first-of-type matches the first paragraph regardless of other element types before it. The same applies to :nth-child() and :nth-of-type().

The second is link order. Because all four link pseudo-classes have the same specificity, the last rule wins. If :hover is written before :visited, visited links never show the hover colour. Keep the order link, visited, hover, active.

Finally, avoid removing focus outlines with outline: none without adding a replacement. Keyboard users rely on them to see where they are.

Takeaway

Pseudo-classes let CSS respond to state and structure. Learn the hover and focus family for interaction, :nth-child() for patterns, and :not(), :is() and :where() for writing shorter, cleaner selectors. The next lesson covers pseudo-elements, which use two colons and style parts of an element rather than its state.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course