Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 5 of 6 · Lab · 25 min · revision 3

Lab: Debug a Broken Stylesheet in DevTools

Take a recipes page with five seeded CSS bugs — a specificity clash, collapsed margins, clipped content, a stacking fault and a wrong unit — diagnose each one in the DevTools Styles and Computed panels, and state its cause in one sentence.

Contentsand lesson path

What you will learn

  • Predict which declaration wins using origin, cascade layers, specificity, source order and inheritance, and resolve conflicts without !important.
  • Size and space elements with the box model (content, padding, border, margin, box-sizing), normal flow and block vs inline display.
  • Diagnose a styling bug with the DevTools Styles and Computed panels and state its cause.

Prerequisites: Semantic HTML

01. Goal and setup

A colleague hands you a small recipes page and five complaints: the card heading is too small, the space between cards is not what the stylesheet says, the "save" options vanish, the NEW badge covers the menu, and the footnote is unreadably tiny. All five are real CSS bugs of the kinds you will meet weekly, and each has a one-sentence cause.

Your job is to diagnose before changing anything: for each bug, predict, inspect in DevTools, name the cause, and only then fix it. You finish by proving each fix with a console check. You need Node 22 (for npx serve) and any current browser. Versions this lab was last run with: Node 22.23.2, serve 14.2.8 (pinned in the command below), Chromium 156 via Playwright 1.64.1.

Terminal
mkdir recipes-lab && cd recipes-lab
touch index.html styles.css
npx [email protected] -l 4173 .
Expected output (non-TTY)
 INFO  Accepting connections at http://localhost:4173

Open http://localhost:4173 and DevTools (F12, or right-click an element and choose Inspect). Paste each step's snippet into the DevTools console — every snippet below is self-contained, so pasting one at a time works on a fresh page. If you prefer no server, opening index.html directly from disk also works for this lab. Whichever you choose, keep the <link rel="stylesheet" href="styles.css"> line in the head: without it the page renders unstyled and none of the measurements below match.

HTML · index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Recipes</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <aside id="sidebar">
    <article class="recipe-card">
      <h2 class="title">Lemon drizzle</h2>
      <button class="save" type="button" aria-expanded="false">Save</button>
      <span class="flag" id="flag">NEW</span>
      <nav class="menu" id="menu" aria-label="Save options">
        <a href="#breakfast">Add to breakfast list</a>
        <a href="#baking">Add to baking list</a>
        <a href="#shared">Share with the club</a>
        <a href="#plan">Add to the meal plan</a>
      </nav>
    </article>
    <article class="recipe-card next-card">
      <h2 class="title">Sourdough</h2>
    </article>
  </aside>
  <p class="small-print">
    Prices checked weekly.
    <span class="note">Offer open to members only.</span>
  </p>
</body>
</html>
CSS · styles.css (broken — do not fix yet)
/* base */
* { box-sizing: border-box; margin: 0; }
body {
  font-family: system-ui, sans-serif;
  line-height: 1.5;
  padding: 24px;
}

/* components */
#sidebar .recipe-card .title { font-size: 1rem; }
.recipe-card {
  background: white;
  padding: 16px;
  position: relative;
  max-width: 320px;
  margin-bottom: 24px;
  overflow: hidden;
}
.next-card { margin-top: 32px; }
.recipe-card .title { font-size: 1.5rem; }
.flag {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 64px;
  text-align: center;
  background: gold;
  padding: 8px 0;
  z-index: 1;
}
.menu {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 280px;
  background: white;
  border: 1px solid gray;
  padding: 8px;
  z-index: 0;
}
.menu a { display: block; padding: 4px 0; }
.small-print { font-size: 0.8em; }
.small-print .note { font-size: 0.8em; }

02. Step 1: the heading that would not grow

Complaint: "the card headings are small, like body text, though the stylesheet clearly says 1.5rem for .recipe-card .title." Predict first: which rules match the first heading, and what does each say? Then select the heading in the Elements panel and read the Styles panel from the top: both title rules are listed, the 1.5rem one is struck through, and the 1rem one is not. Struck-through means matched but lost. Hover the winning rule to see its file and line: it is the #sidebar .recipe-card .title rule, and one column of its specificity explains everything. Confirm with the computed value:

DevTools console
const t = document.querySelector('.recipe-card .title');
console.log('title font-size = ' + getComputedStyle(t).fontSize);
Expected output
title font-size = 16px

Cause: the id in #sidebar .recipe-card .title gives that rule (1,2,0) against (0,2,0), and one id outranks any number of classes. Fix: delete the #sidebar rule — the components rule is the single source of truth. Re-run the snippet: title font-size = 24px.

03. Step 2: the gap that ignores arithmetic

Complaint: "the cards have margin-bottom: 24px and margin-top: 32px, yet the gap is not 56 pixels." In the Elements panel, select the gap between the two cards and watch the box model diagram: the top card shows a margin-bottom bar and the second card a margin-top bar, and they overlap in the diagram — the browser draws collapsed margins as one space. Measure to remove all doubt:

DevTools console
const cards = document.querySelectorAll('.recipe-card');
const a = cards[0].getBoundingClientRect();
const b = cards[1].getBoundingClientRect();
console.log('gap = ' + (b.top - a.bottom).toFixed(1) + 'px');
Expected output (notes in parentheses are not printed)
gap = 32.0px   (not 56px)

Cause: adjacent vertical margins of blocks in normal flow collapse to the larger, so 24 and 32 produce one 32-pixel gap. Fix: let one margin own the space — delete margin-bottom: 24px from .recipe-card and keep .next-card's margin-top. The gap stays 32.0px, but it now comes from exactly one place, and no future rule can fight the second margin.

04. Step 3: the menu that vanishes below the card

Complaint: "the save options are cut off — links spilling past the card are clipped." The .menu is absolutely positioned inside the card and extends past the card's bottom edge. Select the card in the Elements panel: the Computed panel shows overflow: hidden. Hover the .menu node in the Elements panel: the page highlight shows its full box running below the card, while only the part inside the card is painted. Hit-testing tells the truth about what remains clickable:

DevTools console
const card =
  document.querySelector('.recipe-card').getBoundingClientRect();
const probe =
  document.elementFromPoint(card.left + 140, card.bottom + 12);
console.log('below the card: ' +
  (probe ? probe.tagName.toLowerCase() : 'nothing'));
Expected output (notes in parentheses are not printed)
below the card: aside   (not the menu link it should be)

Cause: overflow: hidden on the card clips everything inside it that spills past the padding box — including absolutely positioned children — and clipped areas stop receiving clicks. Fix: delete overflow: hidden from .recipe-card (it was cargo-culted from a rounded-corner trick). Re-run the snippet: below the card: a. If you ever do need clipping on inner content, ensure the overflow clipping boundary does not sit on an ancestor of the popup menu (or use modern top-layer popovers).

05. Step 4: the badge that paints over the menu

Complaint: "when the save options open, the NEW badge sits on top of the first link." Both .flag and .menu are positioned, so painting order decides. Check each rule for z-index: the badge has z-index: 1, the menu z-index: 0. In the Layers panel (or by thinking it through), the badge paints above the menu. Confirm with a hit test on a point inside the badge:

DevTools console
const r = document.getElementById('flag').getBoundingClientRect();
const hit = document.elementFromPoint(r.left + 4, r.top + 4);
console.log('over the badge: ' + (hit ? hit.tagName.toLowerCase() +
  (hit.className ? '.' + hit.className : '') : 'nothing'));
Expected output (notes in parentheses are not printed)
over the badge: span.flag   (the menu link underneath cannot be clicked)

Cause: positioned elements compare z-index within their stacking context; the badge's 1 lifts it above the menu's 0. Fix: delete both z-index declarations. With z-index: auto on both, later DOM order paints on top — and the menu comes after the badge in the markup, so the menu wins without a single magic number. Re-run the snippet: over the badge: a.

06. Step 5: the footnote that shrank twice

Complaint: "the member-only note is tiny." Select the note in the Elements panel and read the Computed panel: font-size: 10.24px — smaller than either 0.8em should produce from a 16px root. Expand the computed font-size row: it points to .small-print .note, and above the panel's "Inherited from" sections you can see the chain. The Styles panel shows both em rules. The unit is the bug:

DevTools console
const n = document.querySelector('.small-print .note');
console.log('note font-size = ' + getComputedStyle(n).fontSize);
Expected output (notes in parentheses are not printed)
note font-size = 10.24px   (0.8 x 0.8 x 16px)

Cause: em in font-size is relative to the parent, so the two rules compound: 0.8 × 0.8 × 16px = 10.24px. Fix: size the note in rem — .small-print .note { font-size: 0.8rem; } — so it anchors to the root and stays 12.8px wherever it is nested. Re-run the snippet: note font-size = 12.8px. Ten pixels of text is also an accessibility concern: tiny text compounding across nested elements is a classic pitfall.

07. If something does not behave

08. Check your understanding

Practice check · 3 questions

Reading DevTools like a stylist

Each situation comes from this lab. Name what the panel is telling you.

Q1. In the Styles panel, a declaration on your element is shown struck through with no warning icon. What does that mean?
Q2. You change `16px` to `24px` in the Styles panel and the page updates instantly — but reloading brings the old value back. Why?
Q3. If the `.small-print .note` rule were removed, DevTools Computed panel would show `font-size: 12.8px` on the note, yet no rule targets `.note`. What is the explanation?
Practice only: not graded, not saved, not evidence.

09. Apply it

Now break your own page. Take the article stylesheet you built across this skill and seed it with three deliberate bugs: a specificity clash, one wrong unit, and one clipping or stacking fault. Hand it to another learner — or to yourself tomorrow — with only the symptoms written down, and diagnose with the same discipline: predict, inspect, state the cause in one sentence, fix, then prove the fix with a console check. Writing the symptom list is the exercise: "the badge covers the menu" forces a cause; "the CSS is broken" forces nothing. Close the lab by re-reading your styles.css top to bottom and confirming every rule has one obvious reason to exist.

10. Review prompt

Without looking back, list the five bugs in this lab and their one-sentence causes — the id column, the collapsed pair, the clip, the z-index pair, the compounded em. For each, say which DevTools surface exposed it: the Styles panel, the Computed panel, the box model diagram, or a console hit test. Then write the general rule you would put on your team's wall about diagnosing before fixing.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check