Sub topic 5
Organising files and folders
This chapter assumes you know CSS selectors and specificity, can link stylesheets to HTML, and can run npm scripts. It builds on naming conventions and BEM from the previous chapters. Layered architectures and tooling come later.
Why folder structure matters
A folder structure tells a new teammate where a rule lives and what else it can affect. Without one, developers append rules to the end of a single file. The file grows, source order becomes accidental, and nobody dares delete anything.
Good organisation answers two questions:
- Where does this style belong?
- What can it break if I change it?
Group by reach, then by component
Global rules, such as resets and tokens, affect every page. Component rules should affect only one block. Putting both in the same folder hides that difference. A common layout separates them:
styles/
main.css
settings/
tokens.css
base/
reset.css
typography.css
layout/
grid.css
page.css
components/
card.css
button.css
nav.css
utilities/
spacing.cssEach folder has a different reach:
| Folder | Contains | Reach | Typical selectors |
|---|---|---|---|
| settings | Custom properties | Whole site, no output | `:root` |
| base | Resets, element defaults | Whole site | Element selectors |
| layout | Page structure | Many pages | Single classes |
| components | One block per file | One component | One BEM block |
| utilities | Single-purpose helpers | Anywhere | Single classes |
Folders are ordered from the widest reach to the narrowest. Later folders can override earlier ones, which is why order matters.
One file per block
Give each BEM block its own file, named after the block. Keep the block, its elements and its modifiers together:
/* components/card.css */
.card {
padding: 1rem;
border: 1px solid #ddd;
}
.card__title {
font-size: 1.25rem;
}
.card--featured {
border-color: gold;
}A developer who sees class="card card--featured" can open components/card.css without searching. When a component is deleted, its file is deleted too. That makes dead CSS much easier to find.
Avoid a misc.css or common.css file. These names say nothing about reach, so they collect unrelated rules.
Assemble files in one entry point
A single entry file lists every partial in the intended order. This is the only place where source order is decided:
/* main.css */
@import "settings/tokens.css";
@import "base/reset.css";
@import "base/typography.css";
@import "layout/grid.css";
@import "layout/page.css";
@import "components/card.css";
@import "components/button.css";
@import "components/nav.css";
@import "utilities/spacing.css";The order of the imports matches the folder order in the table. In the browser, each @import costs an extra request. A bundler can inline them, which the build chapter covers.
Common mistakes
- Mixing component and global rules. A rule such as
.card pincard.cssstyles every paragraph in the card. Prefer a class like.card__body. - Importing in the wrong order. If a utility is imported before a component, the component can win when it should not.
- Folders organised by page. Pages share components, so a
home/folder often duplicates rules. Organise by reach and component, and add page files only for truly page-specific layout. - Deep nesting. More than two levels makes imports hard to read. Split a folder only when it holds more than about a dozen files.
Check your understanding
Exercise
A developer adds .nav a { color: red; } to base/typography.css because it is "just link colour". Which two problems does this cause, and where should the rule go?
Show answer
It puts a component-specific rule in a global folder, so a reader of nav.css will not find it. It also gives the rule higher specificity than a plain element rule, which later base or component files may need to override. It belongs in components/nav.css, ideally as a class such as .nav__link.
Exercise
Why do the folders in main.css go from settings to utilities rather than alphabetically?
Show answer
Source order decides which rule wins when specificity is equal. Wide-reach rules must come first so narrower rules can override them, and utilities come last so they can override components.