Planning CSS Architecture with BEM
The user wants to implement a CSS architecture using the BEM methodology for a large web application. I think they’re looking for guidance on folder structure, naming conventions, examples of CSS/SCSS, and possibly tool integration. I want to make it moderately detailed, focusing on scalability and maintainability. I’ll outline general goals like modularity and predictability, suggest a structure with layers such as base, layout, components, and utilities, and explain how BEM maps to each layer without overcomplicating it.Proposing CSS Architecture Structure
I’m thinking about proposing a directory structure for the CSS architecture that uses BEM methodology. It might look like this:
/src/styles
/abstracts (for variables and mixins)
/base (for resets and typography)
/layout (for grid and page layouts)
/components (housing each block)
/pages (for page-specific overrides)
/themes
/utilities
I’ll explain the BEM naming rules clearly and suggest guidelines like having one block per file, avoiding ancestor selectors and high specificity. I could also include steps for linting and documentation while maintaining flexibility for SCSS. A step-by-step implementation plan might also be beneficial.Detailing CSS Architecture with BEM
I’m planning to include modifiers and states in my discussion, highlighting the difference between state classes and JavaScript hooking. It’s crucial that I document the layout layer, utilities layer, theming support, and tooling requirements like preprocessors, bundlers, linting, and documentation. I also want to touch on migration tips for existing CSS.
For a suggested architecture, I could lay it out like this:
styles/
abstracts/
base/
layout/
components/
pages/
utilities/
The entry file, main.scss, will import everything in order, using BEM prefixes for global utilities. I need to decide on using straightforward naming conventions, balancing between semantic names and convenience.Creating SCSS Architecture with BEM
I’m thinking about demonstrating an example of SCSS, like defining buttons with modifiers such as .button--primary and handling disabled states appropriately. It's important to also incorporate JavaScript-bound classes, like .is-open and .is-active. I should highlight the use of design tokens for colors and spacing within the BEM structure, possibly using CSS custom properties.
As a recommendation, I’ll suggest choosing SCSS as the preprocessor for a large app, while also noting that plain CSS with custom properties works too. I want to frame the response with headings, bullet points, and code examples so it’s easy to follow.Outlining CSS Architecture with BEM
I need to create a practical CSS architecture guide using BEM for a large web application. I'll break it down step-by-step, starting with a component-first structure where each block has its own directory instead of a layered approach with central imports.
It might be helpful to implement a "card" block as a topical example. The HTML could look like this:
<article class="card card--featured">
<header class="card__header">
<h2 class="card__title">...</h2>
</header>
<div class="card__body">...</div>
<footer class="card__footer card__footer--align-right">...</footer>
</article>
In the CSS, I'd outline the styles accordingly. Additionally, I want to emphasize the importance of not nesting blocks inside others, promoting composition instead. Discussing theming and utilizing CSS custom properties will also be essential to cover. Below is a practical, ready‑to‑implement CSS architecture for a large web application using BEM.
I’ll cover:
- Folder/file structure
- Naming conventions (BEM)
- How to structure components (blocks)
- Layout, utilities, and states
- Tooling (linting, documentation)
You can adapt the examples to plain CSS, SCSS, Less, or CSS Modules.
1. Directory structure
A scalable structure that combines layers (base/layout/utilities) with BEM components:
src/
styles/
abstracts/ # Tokens, helpers (no CSS output if using SCSS)
_variables.scss
_mixins.scss
_functions.scss
base/ # Global styles, typography, resets
_reset.scss
_base.scss
_typography.scss
_forms.scss
layout/ # Page layout blocks
_grid.scss
_page.scss
_header.scss # block: .site-header
_footer.scss # block: .site-footer
_sidebar.scss # block: .sidebar
components/ # Reusable UI blocks
button/
_button.scss # block: .button
card/
_card.scss # block: .card
modal/
_modal.scss # block: .modal
nav/
_nav.scss # block: .nav
...
pages/ # Page-specific overrides (avoid if possible)
home.scss
dashboard.scss
utilities/ # Low-level atomic helpers
_spacing.scss
_display.scss
_visibility.scss
_zindex.scss
_text.scss
themes/ # Optional: theming
_theme-light.scss
_theme-dark.scss
main.scss # Entry file importing everything in order
main.scss example:
// 1. Abstracts (no CSS output)
@use 'abstracts/variables';
@use 'abstracts/mixins';
// 2. Base
@use 'base/reset';
@use 'base/base';
@use 'base/typography';
@use 'base/forms';
// 3. Layout
@use 'layout/grid';
@use 'layout/page';
@use 'layout/header';
@use 'layout/footer';
@use 'layout/sidebar';
// 4. Components
@use 'components/button/button';
@use 'components/card/card';
@use 'components/modal/modal';
@use 'components/nav/nav';
// 5. Utilities
@use 'utilities/spacing';
@use 'utilities/display';
@use 'utilities/visibility';
@use 'utilities/text';
// 6. Themes (optional)
@use 'themes/theme-light';
// @use 'themes/theme-dark';
Order matters: base → layout → components → utilities → themes to keep specificity low and predictable.
2. BEM naming conventions
Basic pattern:
.block {}
.block__element {}
.block--modifier {}
.block__element--modifier {}
Rules:
- Block: A standalone component that can be reused (e.g.
.card, .button, .nav).
- Element: A part of a block that has no independent meaning and is tied to its block (e.g.
.card__title).
- Modifier: A flag that changes appearance or behavior (e.g.
.card--featured, .button--large).
Examples:
.card {} /* Block */
.card__header {} /* Element */
.card__title {} /* Element */
.card--featured {} /* Block modifier */
.card__footer--align-right { /* Element modifier */
text-align: right;
}
Do:
- Use full BEM names in HTML (no descendent selectors like
.card .title in CSS).
- Keep specificity at class-level only (no IDs or tag selectors in components).
- Prefer long, explicit names to unclear short ones.
3. Implementing components (blocks)
Each reusable component = one block, in its own file/folder.
Example: card component
HTML:
<article class="card card--featured">
<header class="card__header">
<h2 class="card__title">Card title</h2>
</header>
<div class="card__body">
<p class="card__text">Card content...</p>
</div>
<footer class="card__footer card__footer--align-right">
<button class="button button--primary">Action</button>
</footer>
</article>
SCSS (components/card/_card.scss):
.card {
background-color: var(--surface-color);
border-radius: var(--radius-md);
box-shadow: var(--shadow-sm);
padding: var(--space-md);
display: flex;
flex-direction: column;
gap: var(--space-sm);
}
.card__header {
margin-bottom: var(--space-xs);
}
.card__title {
font-size: 1.25rem;
margin: 0;
}
.card__body {
flex: 1;
}
.card__footer {
display: flex;
align-items: center;
}
.card__footer--align-right {
justify-content: flex-end;
}
/* Block modifier */
.card--featured {
border: 2px solid var(--accent-color);
box-shadow: var(--shadow-md);
}
Key practices:
- Component CSS targets only
.card and its own elements.
- No layout responsibilities that belong to parents (e.g. where the card sits in a grid) — handle that in layout or with utilities.
- Use design tokens (
var(--space-md)) from abstracts/_variables.scss to keep consistent spacing/colors.
Example: button component
HTML:
<button class="button button--primary button--lg">
Save
</button>
<button class="button button--secondary button--icon-only">
<svg class="button__icon">...</svg>
</button>
SCSS (components/button/_button.scss):
.button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
border-radius: var(--radius-sm);
border: 1px solid transparent;
font: inherit;
cursor: pointer;
background: transparent;
color: inherit;
}
/* Style variants */
.button--primary {
background-color: var(--color-primary);
color: #fff;
}
.button--secondary {
background-color: var(--color-secondary);
color: #fff;
}
/* Size variants */
.button--sm {
padding: 0.25rem 0.75rem;
font-size: 0.875rem;
}
.button--lg {
padding: 0.75rem 1.5rem;
font-size: 1rem;
}
/* Icon-only variant */
.button--icon-only {
padding: 0.5rem;
width: 2.5rem;
aspect-ratio: 1 / 1;
}
/* Element */
.button__icon {
width: 1em;
height: 1em;
}
/* Disabled state */
.button[disabled],
.button--disabled {
opacity: 0.5;
cursor: not-allowed;
}
4. Layout, utilities, and states
4.1 Layout blocks
Use BEM for site-level structures as blocks under layout/.
Example: header
HTML:
<header class="site-header">
<div class="site-header__brand">...</div>
<nav class="site-header__nav nav">...</nav>
<div class="site-header__actions">...</div>
</header>
SCSS (layout/_header.scss):
.site-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: var(--space-sm) var(--space-lg);
background: var(--header-bg);
}
.site-header__brand {
display: flex;
align-items: center;
gap: var(--space-xs);
}
.site-header__nav {
flex: 1;
margin: 0 var(--space-md);
}
.site-header__actions {
display: flex;
align-items: center;
gap: var(--space-sm);
}
4.2 Utilities
Utilities are single‑purpose classes like spacing, display, etc. Keep them in utilities/.
Example (utilities/_spacing.scss):
/* Margin utilities */
.u-m-0 { margin: 0 !important; }
.u-m-xs { margin: var(--space-xs) !important; }
.u-m-sm { margin: var(--space-sm) !important; }
.u-m-md { margin: var(--space-md) !important; }
.u-mt-0 { margin-top: 0 !important; }
.u-mt-sm { margin-top: var(--space-sm) !important; }
/* ...likewise for mb, ml, mr, etc. */
/* Padding utilities */
.u-p-0 { padding: 0 !important; }
.u-p-sm { padding: var(--space-sm) !important; }
/* etc. */
Usage:
<div class="card u-mb-md">
...
</div>
4.3 State classes
Use global, non‑BEM state helpers for things like JS‑driven toggles:
is-open, is-active, is-hidden, has-error
Example:
<div class="modal modal--wide is-open">
...
</div>
.modal {
opacity: 0;
pointer-events: none;
transition: opacity 150ms ease-out;
}
.modal.is-open {
opacity: 1;
pointer-events: auto;
}
5. Theming and design tokens
Centralize design tokens in abstracts/_variables.scss and expose as CSS variables:
:root {
--color-primary: #2563eb;
--color-secondary: #10b981;
--surface-color: #ffffff;
--header-bg: #0f172a;
--space-xs: 0.25rem;
--space-sm: 0.5rem;
--space-md: 1rem;
--space-lg: 1.5rem;
--radius-sm: 4px;
--radius-md: 8px;
--shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.08);
--shadow-md: 0 4px 12px rgba(15, 23, 42, 0.12);
}
Dark theme (themes/_theme-dark.scss):
:root.theme-dark {
--surface-color: #020617;
--header-bg: #020617;
--color-primary: #38bdf8;
/* etc. */
}
Usage:
<body class="theme-dark">
...
</body>
All blocks (e.g. .card, .button) rely on these tokens, so theme changes require no changes to component CSS.
6. Tooling: linting & documentation
6.1 Stylelint with BEM rules
Use stylelint plus a BEM plugin for consistency:
stylelint-config-standard
stylelint-selector-bem-pattern (or similar)
Example .stylelintrc snippet:
{
"extends": ["stylelint-config-standard-scss"],
"rules": {
"plugin/selector-bem-pattern": {
"preset": "bem",
"implicitComponents": "src/styles/components/**/*.scss"
},
"selector-max-id": 0,
"selector-max-specificity": "0,2,0"
}
}
6.2 Living style guide / Storybook
For a large app, create a component library:
- Use Storybook (or similar) to render each block (
card, button, modal).
- Document:
- Block purpose
- Available elements
- Available modifiers
- Do/don’t examples (e.g. how not to nest blocks).
This keeps your BEM architecture visible and enforceable.
7. Implementation checklist
To implement this architecture in your project:
- Create the folder structure under
src/styles/ as above.
- Move global/reset styles into
base/.
- Identify components in your UI and create a block for each under
components/ using BEM classnames.
- Refactor selectors:
- Replace nested
.parent .child rules with explicit .block__element.
- Remove IDs and tag selectors from component CSS.
- Introduce utilities for recurring one‑off concerns (margin, display, text alignment).
- Add design tokens in
abstracts/ and refactor colors/spacings to use them.
- Add stylelint with BEM rules and fix reported issues incrementally.
- Document blocks (options and usage) somewhere visible to the team.
If you tell me your current stack (plain CSS vs SCSS, build tools, existing codebase size), I can adapt this to a more concrete, step‑by‑step migration plan or generate boilerplate files tailored to your setup.