Sub topic 6
Layered architectures: ITCSS, SMACSS and OOCSS
This chapter assumes you can read selectors and calculate specificity. It also assumes you know what BEM class names look like and how a stylesheet is split across files. It builds on the file organisation chapter and leads into @layer, which enforces in the browser what these architectures only enforce by convention.
Why layers help
A folder tree tells you where a file lives. It doesn't tell you what a rule is allowed to do. Two files in css/ can both be named buttons.css, and one may reset every <button> while the other styles a single checkout form. A layered architecture adds a rule about reach and strength. Some code is broad and weak, such as a reset. Some code is narrow and strong, such as a one-off override. The architecture puts the broad, weak code first and the narrow, strong code last.
Source order then works for you. Later rules win ties, so if specificity rises as you move down the files, you almost never need !important or a longer selector.
Three well-known approaches cover this ground:
- OOCSS is a way of writing individual rules.
- SMACSS is a way of categorising rules.
- ITCSS is a way of ordering the whole stylesheet.
They overlap, and many teams use all three together.
OOCSS: separate structure from skin
OOCSS (Object-Oriented CSS) was described by Nicole Sullivan. It has two main principles.
- Separate structure from skin. Structure is layout and box behaviour: display, spacing, alignment. Skin is visual decoration: colours, borders, shadows. Put them in different classes so you can combine them.
- Separate container from content. A component should not depend on where it is placed. Avoid selectors like
.sidebar .button. Style the button as a button, and let the sidebar decide where the button goes.
/* Structure: a reusable layout object */
.media {
display: flex;
gap: 1rem;
align-items: flex-start;
}
.media__figure {
flex: none;
}
.media__body {
flex: 1;
}
/* Skin: purely visual, usable on any object */
.skin-bordered {
border: 1px solid #ddd;
border-radius: 4px;
}<div class="media skin-bordered">
<img class="media__figure" src="avatar.png" alt="" width="48" height="48">
<p class="media__body">Ada commented on your pull request.</p>
</div>The same .media object can appear in a comment list, a notification and a search result. Each place chooses its own skin. The cost is that markup carries more classes, and you need discipline to keep objects free of decoration.
SMACSS: five categories of rule
SMACSS (Scalable and Modular Architecture for CSS) was written by Jonathan Snook. It sorts every rule into one of five categories.
| Category | Contents | Typical selector | Typical prefix |
|---|---|---|---|
| Base | Defaults for bare elements | `a`, `body`, `h1` | none |
| Layout | Major page regions | `.l-sidebar` | `l-` |
| Module | Reusable components | `.tabs`, `.card` | none |
| State | Temporary condition of a module | `.is-hidden` | `is-` |
| Theme | Alternative visual treatments | `.theme-dark .card` | `theme-` |
/* Base */
a {
color: #0b5fff;
}
/* Layout */
.l-sidebar {
width: 16rem;
}
/* Module */
.tabs__panel {
padding: 1rem;
}
/* State: scoped to the module it modifies */
.tabs__panel.is-hidden {
display: none;
}The prefixes carry meaning. When you see .l- you know the rule positions a region, and when you see .is- you know JavaScript toggles it. The state rule combines two classes, so its specificity is 0,2,0. That is deliberately higher than the module rule 0,1,0, so a state always beats the module's default without !important.
SMACSS says little about file order or specificity growth across the whole codebase. ITCSS addresses that.
ITCSS: the inverted triangle
ITCSS (Inverted Triangle CSS) was proposed by Harry Roberts. It orders the stylesheet in layers. Going down the layers, each one has three properties:
- Its selectors are more specific.
- Its rules reach fewer elements.
- Its rules are less reusable.
The shape is an inverted triangle. The top is wide and weak, and the bottom is narrow and strong.
The seven layers
- Settings. Global variables such as colours and spacing. In modern CSS these are custom properties. They are covered in the design tokens chapter.
- Tools. Sass mixins and functions. Neither layer 1 nor layer 2 should produce CSS output of its own in a Sass setup. If you use only plain CSS, you can skip Tools.
- Generic. Resets and normalisation. These are the widest selectors in the codebase, often
*or::before. - Elements. Styles for unclassed HTML tags:
h1,a,table. Specificity is0,0,1. - Objects. Undecorated layout patterns, such as the OOCSS
.mediaor a grid wrapper. They use class selectors only. - Components. Designed UI pieces, such as
.cardor.nav. This is where most of your BEM lives. - Trumps. Utilities and overrides, such as
.u-hidden. These are allowed to be heavy-handed, because they are the last word.
A working layout
css/
main.css
settings/
tokens.css
generic/
reset.css
elements/
page.css
objects/
layout.css
components/
card.css
nav.css
trumps/
spacing.css/* main.css: the import order is the architecture */
@import "settings/tokens.css";
@import "generic/reset.css";
@import "elements/page.css";
@import "objects/layout.css";
@import "components/card.css";
@import "components/nav.css";
@import "trumps/spacing.css";Plain CSS @import rules must come before all other statements. Browsers fetch imported files one after another, which is slow. In production a bundler inlines them. The build chapter covers that.
A file can only depend on files above it. components/card.css may read a token from settings/tokens.css. It may not rely on anything in trumps/.
Comparing the three
| Question | OOCSS | SMACSS | ITCSS |
|---|---|---|---|
| What does it govern? | How one rule or class is written | How rules are categorised | How the whole stylesheet is ordered |
| Main tool | Structure/skin split | Five categories and prefixes | Seven layers sorted by specificity |
| Handles source order? | No | Weakly | Yes, explicitly |
| Fits well with BEM? | Yes | Yes | Yes, in Objects and Components |
Combining them in practice
Most teams don't choose one. A common mix looks like this:
- ITCSS decides the folder order and the import order.
- BEM names the classes inside Objects and Components.
- OOCSS guides what goes in Objects (structure) and what stays in Components (skin).
- SMACSS contributes the
is-andl-prefixes, if the team wants them.
Layer names are not sacred. Many teams merge Settings and Tools, or drop Elements into Generic, or add a vendor/ layer between Generic and Elements for third-party CSS. Keep the principle: order layers by increasing specificity and decreasing reach.
Common errors
- Components reaching into each other. A rule like
.nav .cardinnav.cssmakes the card's appearance depend on its parent. Give the card a modifier, such as.card--compact, or let a layout object position it. - Putting an ID or long chain in a lower layer, then fighting it. One high-specificity selector early in the file forces every later rule to match or beat it. Lint against this. The Stylelint chapter shows the rules.
- Using Trumps as a first resort. If
u-classes outnumber component classes in your markup, the Components layer is not doing its job. See the utility classes chapter for trade-offs. - Treating the layers as folders only. The order is what matters. If files are alphabetised by a bundler glob,
components/loads beforegeneric/and the architecture is silently broken. Always import in explicit order. - Mixing Objects and Components. If a "layout" class also sets colours, it is a component. Move it, or the skin leaks into every place the layout is reused.
Where this leads
ITCSS orders code by convention, using the file sequence. Cascade layers, covered next, let you declare that order in the CSS itself. In that chapter you will map each ITCSS layer onto an @layer name. Specificity then stops mattering across layers, and only specificity inside a layer is left to manage.
Exercise
A teammate adds .sidebar .card { padding: 0; } to components/nav.css so that cards in the sidebar are compact. Which two architectural problems does this create, and how would you fix it using the ideas in this chapter?
Show answer
First, it breaks OOCSS container independence, because the card's look now depends on where it sits, and it hides a card rule in the wrong file. Second, it raises specificity from 0,1,0 to 0,2,0 inside a layer where components should be equal, so future overrides need to be stronger. Fix it by adding a modifier in components/card.css, such as .card--compact { padding: 0; }. Apply the modifier in the sidebar markup. The specificity stays at 0,1,0, and a rule placed later in the file still wins.
Exercise
You are given the import order components/card.css, generic/reset.css, trumps/spacing.css. The reset contains * { margin: 0; }, and .card sets margin: 1rem. What margin does a .card element get, and why is it a sign the architecture is broken?
Show answer
The card gets margin: 0. Both rules apply and the reset loads later, but specificity decides first. .card is 0,1,0 and * is 0,0,0, so .card wins and the margin is 1rem. The broken part is that this works only because of specificity. A reset of equal specificity, such as .card, * { margin: 0 }, would win by source order. Generic code must come first so that it can never beat component code, whatever its specificity.