Sub topic 6
Layered architectures: ITCSS, SMACSS and OOCSS
This chapter assumes you know selectors, specificity and source order. It also assumes you have seen BEM naming and a basic folder layout from earlier chapters. Here you will learn three named architectures that decide where each rule belongs and in what order the files load.
Why an architecture on top of naming
BEM tells you what to call a class. It does not tell you which file the class lives in or what loads first. Without that, a generic reset can end up after a component, and a late file can override rules by accident. Layered architectures fix this by sorting rules by reach and specificity, then loading them in that order.
ITCSS: order by reach and specificity
ITCSS (Inverted Triangle CSS) arranges code in layers. The layers run from far-reaching, low-specificity rules to narrow, high-specificity rules. The triangle is "inverted" because the widest impact sits at the top.
| Layer | Contents | Example |
|---|---|---|
| Settings | Variables and tokens, no output | `--color-accent` |
| Tools | Mixins and functions, no output | Sass mixins |
| Generic | Resets and box-sizing | `* { box-sizing: border-box; }` |
| Elements | Bare HTML elements | `h1`, `a` |
| Objects | Layout patterns with no decoration | `.o-container` |
| Components | Designed UI pieces | `.card` |
| Utilities | Single-purpose overrides | `.u-hidden` |
Each layer may override the ones above it, so you rarely need !important or long selectors.
A typical entry file imports the layers in order:
@import "settings/tokens.css";
@import "generic/reset.css";
@import "elements/typography.css";
@import "objects/container.css";
@import "components/card.css";
@import "utilities/visibility.css";Order in this file is the architecture. Moving components above elements would let element styles override component styles.
SMACSS: categorise by purpose
SMACSS (Scalable and Modular Architecture for CSS) sorts rules into five categories:
- Base: defaults for bare elements, such as
aandbody. - Layout: major page regions, usually prefixed
l-, such as.l-sidebar. - Module: reusable components, such as
.card. - State: temporary conditions, prefixed
is-orhas-, such as.is-open. - Theme: visual variations such as colour schemes.
State rules are the distinctive part. A name like .is-open tells readers that JavaScript toggles it, so it should not hold structural styles.
OOCSS: separate structure from skin
OOCSS (Object-Oriented CSS) is a set of two principles rather than a folder scheme.
- Separate structure from skin. Layout and sizing go in one class. Colours, borders and shadows go in another.
- Separate container from content. A component should look the same wherever you place it, so avoid selectors like
.sidebar .button.
.media {
display: flex;
gap: 1rem;
}
.skin-bordered {
border: 1px solid #ddd;
border-radius: 4px;
}<div class="media skin-bordered">
<img src="avatar.png" alt="">
<p>Reusable layout, optional skin.</p>
</div>The cost is more classes in the markup. The benefit is that each class is small and reusable.
Choosing and combining
The three ideas fit together. ITCSS controls load order, SMACSS provides category names such as state, and OOCSS keeps individual classes reusable. Many teams use ITCSS folders, BEM names and OOCSS-style separation together.
Exercise
You find .is-open { padding: 2rem; } in a SMACSS project, and it breaks a card's spacing when the state is not set. Which SMACSS principle does the rule violate, and where should the padding go?
Show answer
State rules should describe only a temporary condition, not core structure. Move the padding into the module (for example .card), and keep .is-open for the changes that apply only while open.
Exercise
In an ITCSS stylesheet, why must the Utilities layer load last?
Show answer
Utilities are single-purpose overrides, such as .u-hidden. Loading them last lets them win over component and object rules through source order, without needing !important or higher specificity.