← Back to Overview Chapter 05: Pseudo Selectors

1. Decorative ::before Background Shapes

Using pseudo-elements to render decorative geometry behind images using z-index: -1 and various transform/position offsets.

💡 Stacking Context Fix (isolation: isolate): Notice that by adding isolation: isolate to .relative, each element forms its own independent stacking context. This ensures the khaki ::before circles sit behind the <img> but stay in front of the card background, rather than disappearing underneath the container!
Certificate
top: 0% left: 0%
Certificate
top: -25% left: 30%
Certificate
top: 50% left: 50%
Certificate
top: 0% left: 0% translate(10%, 5%)
Certificate
top: 50% left: 50% scale(0.5)
Certificate
top: 50% left: 50% scale(1.5)
overflow: hidden
.relative {
    position: relative;
    border: 2px solid red;
    isolation: isolate; /* Creates independent stacking context */
}
.relative::before {
    content: "";
    background-color: khaki;
    width: 100%;
    padding-bottom: 100%;
    border-radius: 50%;
    position: absolute;
    top: 50%; left: 50%;
    z-index: -1;
    transform: scale(1.5);
}

2. The :has() Parent Selector (#13)

Style parent containers based on the status of their internal children or form validation states.

Click the checkbox above. Notice how the parent card border lights up with a teal shadow via .card:has(input:checked)!

Styled with form:has(input:invalid:not(:placeholder-shown))
/* Styles card when internal checkbox is checked */
.card:has(input[type="checkbox"]:checked) {
    border-color: #008080;
    background-color: #f0fdfa;
    box-shadow: 0 4px 12px rgba(0, 128, 128, 0.18);
}

/* Styles form container when email field is invalid */
form:has(input:invalid) {
    border: 2px solid #ef4444;
}

3. :is(), :where() & :focus-visible (#14, #15)

:is() simplifies compound selectors (taking the highest specificity), while :where() always has zero (0) specificity for hassle-free defaults.

Heading Styled via :where()

Because :where() has 0 specificity, any utility class can easily override it without needing !important.

:focus-visible Accessibility:
Mouse click has no ugly outline; pressing Tab produces a crisp 3px accessibility ring!
/* Zero-specificity baseline styles */
:where(h1, h2, h3) { margin: 0; }

/* Accessible keyboard-only focus indicator */
button:focus { outline: none; }
button:focus-visible { outline: 3px solid #2563eb; outline-offset: 3px; }

4. :user-valid & :user-invalid (#16)

Unlike :invalid (which turns red immediately upon page load before the user even types), :user-invalid only displays validation feedback after user interaction and blur!

Type an invalid email, click outside, and see it turn red smoothly!
input:user-valid   { border-color: #10b981; background-color: #f0fdf4; }
input:user-invalid { border-color: #ef4444; background-color: #fef2f2; }

5. ::backdrop & Top Layer <dialog> (#17)

HTML5 native <dialog> elements reside in the browser's Top Layer. Style the background scrim directly with dialog::backdrop.

✨ Native <dialog> Modal

Notice the blurred, darkened backdrop behind this dialog! It is styled purely in CSS using dialog::backdrop { backdrop-filter: blur(6px); }.

dialog::backdrop {
    background-color: rgba(15, 23, 42, 0.65);
    backdrop-filter: blur(6px); /* Blurs page content behind modal! */
}

6. Micro-Styling: ::marker, initial-letter, Tooltips & Ribbons (#18, #36)

Cascading Style Sheets have evolved dramatically in recent specifications, giving developers fine-grained control over typography and layout.

Custom ::marker bullets:
  • Zero SVG icons needed
  • Built-in Unicode glyph styling
PRO

Tooltip & Ribbon

Hover over this keyword to trigger a tooltip created with attr(data-tooltip) and ::after.

/* Custom bullet markers */
ul li::marker { content: "⚡ "; color: #008080; }

/* Magazine-style initial letter (Drop Cap) */
p::first-letter { initial-letter: 3 2; color: #008080; }
Fixed 🏠