On this page11 sections
  1. Learning Objectives
  2. 1. One Order, Every Time
  3. 2. Naming Classes: BEM
  4. 3. Comments and Small Files
  5. 4. Debugging With DevTools
  6. 5. Quick Visual Debugging
  7. 6. When a Style Doesn't Work: a Checklist
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 056: Organizing & Debugging CSS

Category10: Tools & Workflow
Lecture056 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

A stylesheet that starts at 50 lines can reach 5,000. Without a plan it turns into a mess where nobody dares delete anything. This lecture covers how to organise CSS so it stays readable, and how to find out quickly why a style isn't working.


Learning Objectives

  • Organise a stylesheet into clear sections
  • Name classes consistently (for example with BEM)
  • Debug CSS with DevTools
  • Follow a checklist when a style doesn't apply

1. One Order, Every Time

css
/* 1. Settings: variables */
:root { --brand: #1971c2; --radius: 8px; }

/* 2. Reset / base: elements */
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }

/* 3. Layout: page structure */
.container { max-width: 1100px; margin-inline: auto; }

/* 4. Components: reusable pieces */
.card { … }
.button { … }

/* 5. Utilities: small helpers */
.text-center { text-align: center; }

Cascade layers (Lecture 027) can enforce this order: @layer settings, base, layout, components, utilities;


2. Naming Classes: BEM

Block, Element, Modifier:

output
.card                 → the block
.card__title          → an element inside it
.card--featured       → a variation of it

Example

html
<style>
  .card { border: 1px solid #dee2e6; border-radius: 10px; padding: 14px; margin-bottom: 10px; font-family: system-ui; }
  .card__title { margin: 0 0 6px; }
  .card__text { margin: 0; color: #495057; }
  .card--featured { border-color: #f59f00; background: #fff9db; }
</style>

<div class="card"><h3 class="card__title">Normal card</h3><p class="card__text">Plain.</p></div>
<div class="card card--featured"><h3 class="card__title">Featured card</h3><p class="card__text">Highlighted with a modifier.</p></div>

Names say what things are (.card__title), not how they look (.big-blue-text).


3. Comments and Small Files

  • A short comment heads each section; explain why, not what.
  • On bigger projects, split CSS into files (base.css, layout.css, components/card.css) and combine them when building the site.

4. Debugging With DevTools

Right-click → Inspect, then look at the Styles panel:

You seeIt means
A declaration crossed outAnother rule won (higher specificity, or later)
A yellow ⚠ warning iconInvalid property or value (often a typo)
A greyed-out declarationIt has no effect here (e.g. width on an inline element)
The rule isn't listed at allThe selector doesn't match the element

Other useful tools:

  • Computed tab: the final value of every property, and where it came from.
  • The box model diagram: margin, border, padding and content sizes.
  • Grid and Flex badges: overlays showing the lines and gaps.
  • Tick or untick declarations to switch them on and off live.

5. Quick Visual Debugging

Example: an outline on everything shows every box at once:

html
<style>
  .debug * { outline: 1px solid rgb(255 0 0 / 0.5); }
</style>

<div class="debug">
  <header><h2>Title</h2><nav><a href="#">Home</a> <a href="#">About</a></nav></header>
  <main><p>Spot which box is causing unexpected space.</p></main>
</div>

Outlines don't change the layout, unlike borders.


6. When a Style Doesn't Work: a Checklist

  1. Is the CSS file actually loaded? (Network panel; check the path.)
  2. Does the selector match? (Check the spelling, . vs #, and capitals.)
  3. Is a typo or missing ; breaking the declaration? (Look for the ⚠ icon.)
  4. Is another rule winning? (Look for the crossed-out declaration.)
  5. Does the property apply here? (e.g. width on an inline <span>, z-index without position.)
  6. Is the browser showing an old cached copy? (Hard refresh: Ctrl+Shift+R / ⌘⇧R.)

Common Beginner Mistakes

1. Adding !important instead of finding the cause. DevTools shows the real reason in seconds.

2. Class names that describe looks. .red-text becomes wrong the day the design turns blue.

3. Never deleting old CSS. Unused rules pile up and slow everyone down.


Practice Questions

  1. Name the five sections of the suggested stylesheet order.
  2. Write a BEM class for the button inside a search block.
  3. What does a crossed-out declaration in DevTools mean?
  4. What does a yellow warning icon next to a declaration mean?
  5. Why use outline instead of border for visual debugging?
  6. A style works in DevTools but not after saving the file. What should you check?

Practice Answers

  1. Settings, base, layout, components, utilities.
  2. .search__button
  3. Another rule overrode it.
  4. The property or value is invalid, usually a typo.
  5. Outlines don't take up space, so they don't change the layout.
  6. That the file was saved and actually linked, and do a hard refresh to clear the cache.

Key Takeaways

  • Keep one order: settings → base → layout → components → utilities.
  • Name classes by meaning; BEM keeps names predictable.
  • DevTools shows which rule won, which are invalid, and the box model.
  • Work through the checklist before reaching for !important.