CSS Best Practices

CSS Organization

Structure your CSS for scalability and maintainability using proven methodologies like ITCSS, BEM, and component-based architecture.

How should I organize CSS files in a project?

Order styles from general to specific: settings and variables, a reset, base element styles, layout, components, then utilities. In a component-based app, keep each component's styles next to the component (CSS Modules or Tailwind), and use @layer to control which group wins instead of fighting specificity.

@layer reset, base, layout, components, utilities;

@layer base { body { font-family: system-ui; } }
@layer components { .card { … } }
@layer utilities { .hidden { display: none; } }

ITCSS Methodology

Inverted Triangle CSS organizes styles from generic to specific, reducing conflicts and improving maintainability.

BEM Naming

Block, Element, Modifier methodology creates clear, predictable class names that prevent naming conflicts.

File Structure

Logical file organization makes your codebase easier to navigate and maintain as it grows.

Organized CSS Architecture

Code Editor

<button class="btn">Component style</button>
<button class="btn hidden">Utility wins</button>
<p>Layers decide priority, not selector specificity.</p>

Preview

ITCSS Layers

  • Settings: Variables and configuration
  • Tools: Mixins and functions
  • Generic: Reset and normalize
  • Elements: HTML elements
  • Objects: Layout patterns
  • Components: UI components
  • Utilities: Helper classes

BEM Examples

.card

Block (standalone component)

.card__title

Element (part of the block)

.card--featured

Modifier (variation of the block)

.btn__icon--large

Element with modifier

CSS Organization — step by step

1 / 4
1

Split CSS by concern

Separate your styles into files: a base/reset file, a variables file, component files, and a layout file. This prevents one giant stylesheet from becoming unmanageable.

1 of 4
🔍ITCSS Layer Order
ITCSS (Inverted Triangle CSS) organizes styles from least to most specific: Settings, Tools, Generic, Elements, Objects, Components, Utilities. Following this order prevents specificity wars and keeps overrides predictable.

🎯 Quick Check

What is the main benefit of splitting CSS into multiple files by component?

Organising CSS: summary

Key points

  • Cascade layers: styles in a later layer always win over earlier layers, regardless of specificity.
  • One file per component scales better than one giant stylesheet.
  • Keep design tokens (colours, spacing, fonts) in CSS variables in one place.

Frequently asked questions

›What are CSS cascade layers?

@layer groups rules into named layers whose priority you declare once. Later layers beat earlier ones whatever their selectors' specificity, which makes overrides predictable.