KodingKaro
CSS · Advanced · Advanced UI Patterns and Projects

Layered cards, overlays, and component states

Advanced components combine layout and state without making the page structure fragile.

✓ What you will learn in this topic

  • Explain the key idea: Plan component states and layering while preserving stable content structure.
  • Apply the concept: A badge remains anchored to the component, and the card accent changes according to its explicit state.
  • Avoid this common mistake:

Concept explanation

Use a positioned wrapper for a badge or overlay while keeping primary content in normal flow. Create explicit component states for hover, focus, selected, and disabled conditions. Use custom properties for state colors and keep interactive meaning in HTML attributes and text.

Check stacking contexts and overflow when an overlay is clipped or appears beneath another region. Prefer a documented layering scale over arbitrary large z-index values.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  position: relative;
  overflow: clip;
  border-radius: 1rem;
}
.demo .badge {
  position: absolute;
  inset-block-start: 0.75rem;
  inset-inline-end: 0.75rem;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  position: relative;
  overflow: clip;
  border-radius: 1rem;
}
.demo .badge {
  position: absolute;
  inset-block-start: 0.75rem;
  inset-inline-end: 0.75rem;
}

Expected result: A badge remains anchored to the component, and the card accent changes according to its explicit state.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.dashboard-card {
  --accent: #2563eb;
  border-inline-start: 0.25rem solid var(--accent);
}
.dashboard-card[data-state="warning"] {
  --accent: #b45309;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Building a complex interface with visual CSS tricks while ignoring semantics, source order, and keyboard interaction.

Best practice

Define component states and responsive behavior before polishing; keep DOM order meaningful and CSS responsibilities clear.

Practice exercise

Design a responsive dashboard panel with a summary, status indicator, data list, and actions that remain usable on mobile and desktop. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Plan component states and layering while preserving stable content structure.
  • Define component states and responsive behavior before polishing; keep DOM order meaningful and CSS responsibilities clear.

Knowledge check

Where should the primary content of an overlay card remain?

Reveal the explanation

Keep the core content in normal flow so it remains robust when content or viewport changes.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  position: relative;
  overflow: clip;
  border-radius: 1rem;
}
.demo .badge {
  position: absolute;
  inset-block-start: 0.75rem;
  inset-inline-end: 0.75rem;
}

Expected result:

A badge remains anchored to the component, and the card accent changes according to its explicit state.

Hands-on practice task

Design a responsive dashboard panel with a summary, status indicator, data list, and actions that remain usable on mobile and desktop.

In this section

Other lessons in Advanced UI Patterns and Projects

Broaden your knowledge

Related learning topics