📡 You're offline — showing cached content
New version available!
Quick Access

CSS Pseudo-Classes and Pseudo-Elements

Learn CSS pseudo-classes (:hover, :focus, :nth-child, :not) and pseudo-elements (::before, ::after, ::placeholder) with UI examples.

EzyCoders Admin August 7, 2026 7 min read 4 views
CSS Pseudo-Classes and Pseudo-Elements
Share: Twitter LinkedIn WhatsApp

What is it?

Pseudo-classes select elements based on state or position. Pseudo-elements create virtual sub-elements for styling specific parts of an element.

Why does it matter?

Pseudo-classes and pseudo-elements let you add rich interactive states and decorative effects without extra HTML — custom checkboxes, zebra tables, tooltips, and accessible focus styles.

Learn CSS pseudo-classes (:hover, :focus, :nth-child, :not) and pseudo-elements (::before, ::after, ::placeholder) with UI examples.

Real-World Use Cases

  • 🎨 Custom checkbox styling - ::before on a label creates a visible checkbox while the real input is hidden — fully accessible.
  • 📋 Zebra-stripe tables - :nth-child(even) applies background to every other row — no class needed on each row.
  • 🔍 Form validation feedback - :valid and :invalid style inputs green or red based on HTML5 validation state.
  • 💬 Tooltips without JavaScript - ::after creates a tooltip on :hover — pure CSS, no JS needed.

State Pseudo-Classes

a:link    { color: #6366f1; }
a:visited { color: #7c3aed; }
a:hover   { color: #4f46e5; }
a:focus   { outline: 2px solid #6366f1; outline-offset: 2px; }

input:focus    { border-color: #6366f1; }
input:disabled { opacity: 0.5; cursor: not-allowed; }
input:valid    { border-color: #10b981; }
input:invalid  { border-color: #ef4444; }

li:first-child     { font-weight: bold; }
li:last-child      { border-bottom: none; }
tr:nth-child(even) { background: #f8fafc; }  /* zebra stripe */
:not(.disabled)    { cursor: pointer; }

Pseudo-Elements

/* ::before and ::after — virtual content elements */
.btn::before {
    content: "";  /* required even when empty */
    display: block;
}

/* Decorative arrow after external links */
a[href^="https"]::after { content: " v"; font-size: 0.75em; }

/* Custom placeholder */
input::placeholder { color: #94a3b8; font-style: italic; }

/* Style first line */
p::first-line { font-size: 1.1em; font-weight: 600; }

CSS-Only Tooltip

.tooltip-trigger { position: relative; cursor: help; }

.tooltip-trigger::after {
    content: attr(data-tooltip);
    position:  absolute;
    bottom:    calc(100% + 8px);
    left:      50%;
    transform: translateX(-50%);
    background: #1e293b;
    color:     white;
    padding:   6px 12px;
    border-radius: 6px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity:   0;
    pointer-events: none;
    transition: opacity 200ms;
}
.tooltip-trigger:hover::after { opacity: 1; }

Q: What is the difference between :nth-child and :nth-of-type?

:nth-child(n) counts ALL sibling elements regardless of type. :nth-of-type(n) counts only siblings of the same element type. If a div has h2 followed by three p tags, p:nth-child(2) selects the first p (it is the 2nd child overall), while p:nth-of-type(1) selects the first p counting only p elements.

🧩
Test Your Knowledge
50 questions · Takes ~1500 seconds

Answer these questions to confirm you understood the article. Instant feedback on every answer.

Q1
What is the primary purpose of this topic in Career development?
Q2
In a Career technical interview, what do interviewers test?
Q3
What is the time/space complexity consideration for this Career topic?
Q4
When should a Career developer apply this technique?
Q5
What is the most common mistake when using this Career pattern?
Q6
Best practice for Career concept #6?
Q7
Best practice for Career concept #7?
Q8
Best practice for Career concept #8?
Q9
Best practice for Career concept #9?
Q10
Best practice for Career concept #10?
Q11
Best practice for Career concept #11?
Q12
Best practice for Career concept #12?
Q13
Best practice for Career concept #13?
Q14
Best practice for Career concept #14?
Q15
Best practice for Career concept #15?
Q16
Best practice for Career concept #16?
Q17
Best practice for Career concept #17?
Q18
Best practice for Career concept #18?
Q19
Best practice for Career concept #19?
Q20
Best practice for Career concept #20?
Q21
Best practice for Career concept #21?
Q22
Best practice for Career concept #22?
Q23
Best practice for Career concept #23?
Q24
Best practice for Career concept #24?
Q25
Best practice for Career concept #25?
Q26
Best practice for Career concept #26?
Q27
Best practice for Career concept #27?
Q28
Best practice for Career concept #28?
Q29
Best practice for Career concept #29?
Q30
Best practice for Career concept #30?
Q31
Best practice for Career concept #31?
Q32
Best practice for Career concept #32?
Q33
Best practice for Career concept #33?
Q34
Best practice for Career concept #34?
Q35
Best practice for Career concept #35?
Q36
Best practice for Career concept #36?
Q37
Best practice for Career concept #37?
Q38
Best practice for Career concept #38?
Q39
Best practice for Career concept #39?
Q40
Best practice for Career concept #40?
Q41
Best practice for Career concept #41?
Q42
Best practice for Career concept #42?
Q43
Best practice for Career concept #43?
Q44
Best practice for Career concept #44?
Q45
Best practice for Career concept #45?
Q46
Best practice for Career concept #46?
Q47
Best practice for Career concept #47?
Q48
Best practice for Career concept #48?
Q49
Best practice for Career concept #49?
Q50
Best practice for Career concept #50?
EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment