CSS Naming and Organization
๐ Covered in this chapter:
Naming Conventions ยท BEM ยท Utility Classes ยท Component Classes ยท Layout ยท State Classes ยท Folder Structure ยท Reset ยท Base ยท Components ยท Utilities ยท Themes ยท Refactoring
Welcome to Chapter 40: CSS Naming and Organization โ part of the CSS Complete Roadmap. This lesson covers all subtopics with clear explanations, code examples, and best practices used in professional web development.
๐ What You Will Learn
- Professional CSS naming conventions and BEM methodology
- Organizing CSS into reset, base, layout, components, utilities, themes
- Recommended folder structure for small and large projects
- Avoiding global CSS conflicts and specificity wars
- Refactoring messy CSS into maintainable architecture
๐ก Why This Matters
Small projects lo single CSS file chalu. But 10+ pages, multiple developers, component library unte โ naming and organization lekunda CSS spaghetti avuthundi. Proper architecture save hours of debugging and makes onboarding new developers easy.
1CSS Naming Conventions
| Type | Pattern | Example |
|---|---|---|
| Component | .block or .block__element | .card, .card__title |
| State | .block--modifier | .btn--primary, .nav__link--active |
| Layout | .l- or .layout- | .l-container, .layout-sidebar |
| Utility | .u- or single purpose | .u-text-center, .mt-4 |
CSS โ BEM Exampleโถ Try in Editor
.card { }
.card__header { }
.card__body { }
.card__footer { }
.card--featured { border-color: #2563eb; }
.card__title--large { font-size: 1.5rem; }
2Recommended Folder Structure
styles/ โโโ reset.css โ browser defaults normalize โโโ variables.css โ CSS custom properties / design tokens โโโ base.css โ typography, body, links โโโ layout.css โ grid, container, page structure โโโ components/ โ โโโ button.css โ โโโ card.css โ โโโ modal.css โ โโโ navbar.css โ โโโ form.css โโโ utilities.css โ spacing, text, display helpers โโโ themes.css โ light/dark theme overrides
CSS โ main.css importsโถ Try in Editor
@import 'reset.css';
@import 'variables.css';
@import 'base.css';
@import 'layout.css';
@import 'components/button.css';
@import 'components/card.css';
@import 'components/modal.css';
@import 'utilities.css';
@import 'themes.css';
3Reset, Base & Component Layers
CSS โ reset.cssโถ Try in Editor
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
img, picture, video { max-width: 100%; display: block; }
input, button, textarea, select { font: inherit; }
CSS โ base.cssโถ Try in Editor
body {
font-family: var(--font-sans);
line-height: 1.6;
color: var(--text-primary);
background: var(--bg-primary);
}
a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }
CSS โ components/button.cssโถ Try in Editor
.btn { /* component styles only */ }
.btn--primary { }
.btn--secondary { }
Rule: Components should not set page-level layout. Layout classes handle grid/flex page structure. Components handle self-contained UI pieces.
4Avoiding Global Conflicts & Refactoring
- Namespace components:
.oc-btnor BEM blocks prevent collisions - Avoid element selectors in components:
.card pโ.card__text - CSS Layers:
@layer reset, base, components, utilities; - Refactoring steps: Audit โ group by component โ extract variables โ add utilities โ delete dead CSS
โ ๏ธ Common Mistakes
- Generic class names: .box, .red, .big โ meaningless and conflicting
- Deep nesting in CSS files matching HTML structure โ hard to maintain
- !important to fix specificity wars instead of fixing architecture
- Utility classes for everything โ HTML becomes unreadable
- No CSS variables โ colors duplicated 50 times across files
โ Mini Quiz
Q1 BEM lo .card__title--highlighted lo __ and -- meaning enti?
__ (double underscore) = element inside block. -- (double hyphen) = modifier/state variant.
Q2 reset.css vs base.css difference enti?
reset.css removes browser inconsistencies. base.css sets project defaults (fonts, colors, link styles).
โ
Quick Recap
- BEM: Block__Element--Modifier naming prevents conflicts
- Folder structure: reset โ variables โ base โ layout โ components โ utilities โ themes
- Components self-contained โ no page layout inside component CSS
- CSS custom properties in variables.css โ single source of truth
- Refactor incrementally โ one component at a time