Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / CSS Debugging

2,080 people viewed this

CSS Debugging

Section 34: CSS Debugging and Developer Tools

1. Open browser Developer Tools

In Chrome or another Chromium-based browser, right-click an element and choose Inspect. You can also use F12 or Ctrl+Shift+I on Windows in common configurations.

2. Inspect styles

  • Elements panel: inspect the HTML structure and edit DOM styles temporarily.
  • Styles panel: see matching declarations and rules that are overridden.
  • Computed styles: inspect the final computed values for an element.
  • Console: review errors and warnings.
  • Device toolbar: test responsive layouts at different viewport sizes.

3. Diagnose specificity and cascade

p { color: black; }
.notice p { color: green; }
#main .notice p { color: blue; }

If all three rules match the same paragraph and no other cascade factors intervene, the ID-containing selector has greater specificity. The Styles panel shows which declarations are overridden.

4. Debug layout problems

.container {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}
img {
  max-width: 100%;
  height: auto;
}

When content overflows, inspect the element's box model, width, padding, margins, grid tracks, flex sizing, and long unbreakable content.

Runnable debugging example

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Debug This CSS</title>
<style>
p { color: black; }
.notice p { color: green; }
#main .notice p { color: blue; }
.container { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.box { padding: 20px; background: #dbeafe; overflow-wrap: anywhere; }
@media (max-width: 600px) { .container { grid-template-columns: 1fr; } }
</style>
</head>
<body>
<main id="main">
  <section class="notice">
    <p>Inspect this paragraph to see which color wins.</p>
  </section>
  <div class="container">
    <div class="box">First card</div>
    <div class="box">AnExtraLongUnbrokenWordThatCouldCauseOverflow</div>
  </div>
</main>
</body>
</html>

A systematic debugging checklist

  1. Confirm the correct stylesheet is loaded and has no network errors.
  2. Check the element's class names and whether the selector matches.
  3. Inspect overridden declarations, specificity, layers, and source order.
  4. Check inherited values and computed styles.
  5. Inspect box dimensions, padding, margin, overflow, and layout mode.
  6. Test the problem at different viewport sizes.
  7. Change declarations temporarily in DevTools, then update the real source file.

Common mistakes

  • Changing random CSS properties without finding the root cause.
  • Assuming the last declaration always wins even when specificity or importance differs.
  • Fixing a temporary DevTools change without updating the source file.
  • Testing only at one screen width.

Practice exercise

Use Developer Tools to inspect the example above. Identify the winning paragraph color, find the grid layout rules, and test the layout at a viewport narrower than 600px.

Knowledge check: 10 MCQs

  1. What does Inspect help you do? Answer: Examine an element's HTML and styles. Explanation: It opens the element inspector.
  2. Where can you see overridden declarations? Answer: The Styles panel. Explanation: Overridden rules are shown with visual indicators.
  3. What are computed styles? Answer: The resolved values applied to an element. Explanation: They reflect the result of the cascade and value computation.
  4. Does the last declaration always win? Answer: No. Explanation: Importance, origin, layers, specificity, and source order all matter.
  5. Which panel helps test responsive viewport sizes? Answer: Device toolbar or responsive design mode. Explanation: It allows testing different viewport dimensions.
  6. What should you check when a stylesheet is missing? Answer: Its URL, loading status, and console/network errors. Explanation: The browser must load the stylesheet successfully.
  7. Why inspect the box model? Answer: To understand element dimensions and spacing. Explanation: Padding, borders, margins, and sizing affect layout.
  8. Are DevTools edits automatically permanent? Answer: Not usually. Explanation: Temporary edits do not necessarily update project files.
  9. What is a good first step when a selector fails? Answer: Verify that the selector matches the intended element. Explanation: A rule cannot apply if it does not match.
  10. Why test multiple viewport widths? Answer: To catch responsive layout problems. Explanation: A layout that works on desktop may fail on mobile.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars