Section 31: CSS Architecture and Organization
CSS architecture is the way you organize stylesheets, components, shared rules, and design decisions so a project remains maintainable as it grows.
1. Organize stylesheets by responsibility
project/
index.html
css/
reset.css
tokens.css
base.css
layout.css
components/
button.css
card.css
pages/
dashboard.css
utilities.cssThis is an example structure, not a mandatory standard. Small projects may use fewer files. Use the structure that matches the team's size and build process.
- reset.css: optional normalization of browser defaults.
- tokens.css: shared colors, spacing, typography, and other design tokens.
- base.css: document-wide defaults for body, headings, links, and form controls.
- layout.css: page shells, grids, navigation, and reusable layout patterns.
- components: button, card, alert, modal, and other reusable UI parts.
- pages: styles unique to a specific page or route.
- utilities.css: small single-purpose classes such as visually-hidden.
2. Build reusable components
/* components/card.css */
.card {
padding: 1.25rem;
border: 1px solid #dbe2ea;
border-radius: .75rem;
background: white;
}
.card__title {
margin-block: 0 .5rem;
font-size: 1.25rem;
}
.card--featured {
border-color: #2563eb;
}3. Load styles in a clear order
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/tokens.css">
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/layout.css">
<link rel="stylesheet" href="css/components/card.css">
<link rel="stylesheet" href="css/utilities.css">Order matters when declarations have equal cascade priority. Cascade layers, covered in Section 33, can provide a more explicit strategy for managing precedence.
4. Runnable example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Organized CSS</title>
<style>
:root {
--color-primary: #2563eb;
--color-border: #dbe2ea;
--space-card: 1.25rem;
}
body { font-family: Arial, sans-serif; margin: 0; padding: 2rem; background: #f8fafc; }
.card { max-width: 420px; padding: var(--space-card); border: 1px solid var(--color-border); border-radius: .75rem; background: white; }
.card__title { margin-top: 0; color: var(--color-primary); }
.card__button { padding: .65rem 1rem; border: 0; border-radius: .4rem; background: var(--color-primary); color: white; cursor: pointer; }
</style>
</head>
<body>
<article class="card">
<h2 class="card__title">Reusable card</h2>
<p>A component can be reused on several pages.</p>
<button class="card__button">View details</button>
</article>
</body>
</html>Common mistakes
- Putting every rule into one enormous stylesheet without sections or conventions.
- Repeating identical values instead of using shared design tokens.
- Writing broad selectors such as div or article for component-specific styling.
- Using !important repeatedly to compensate for unclear cascade rules.
- Creating too many tiny files without considering the project's tooling or maintenance needs.
Practice exercise
Design a simple admin dashboard with a header, sidebar, statistic cards, and a primary button. Separate shared component styles from page layout styles.
Knowledge check: 10 MCQs
- What is CSS architecture? Answer: A strategy for organizing styles. Explanation: It helps CSS remain understandable and maintainable.
- What belongs in a component stylesheet? Answer: Styles for a reusable UI component. Explanation: Component styles should have a clear responsibility.
- What are design tokens? Answer: Shared values such as colors and spacing. Explanation: They help keep a design consistent.
- Why separate page-specific styles? Answer: To reduce unrelated style coupling. Explanation: Page changes are less likely to affect unrelated components.
- Does the browser require this exact folder structure? Answer: No. Explanation: Folder structure is a development convention.
- Can stylesheet order affect results? Answer: Yes. Explanation: Source order can break ties in the cascade.
- Why avoid repeated CSS values? Answer: Shared tokens make updates easier. Explanation: One token can update many components.
- Should every project have hundreds of CSS files? Answer: No. Explanation: Structure should fit the project.
- What is a component? Answer: A reusable interface unit. Explanation: Examples include cards, buttons, and alerts.
- What should you prefer over repeated !important declarations? Answer: Clear selector and cascade organization. Explanation: Predictable rules are easier to debug.