On this page11 sections
Lecture 056: Organizing & Debugging CSS
| Category | 10: Tools & Workflow |
| Lecture | 056 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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
/* 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:
.card → the block
.card__title → an element inside it
.card--featured → a variation of itExample
<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 see | It means |
|---|---|
| A declaration | Another rule won (higher specificity, or later) |
| A yellow ⚠ warning icon | Invalid property or value (often a typo) |
| A greyed-out declaration | It has no effect here (e.g. width on an inline element) |
| The rule isn't listed at all | The 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:
<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
- Is the CSS file actually loaded? (Network panel; check the path.)
- Does the selector match? (Check the spelling,
.vs#, and capitals.) - Is a typo or missing
;breaking the declaration? (Look for the ⚠ icon.) - Is another rule winning? (Look for the crossed-out declaration.)
- Does the property apply here? (e.g.
widthon an inline<span>,z-indexwithoutposition.) - 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
- Name the five sections of the suggested stylesheet order.
- Write a BEM class for the button inside a
searchblock. - What does a crossed-out declaration in DevTools mean?
- What does a yellow warning icon next to a declaration mean?
- Why use
outlineinstead ofborderfor visual debugging? - A style works in DevTools but not after saving the file. What should you check?
Practice Answers
- Settings, base, layout, components, utilities.
.search__button- Another rule overrode it.
- The property or value is invalid, usually a typo.
- Outlines don't take up space, so they don't change the layout.
- 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.