Table of Contents
CSS Globals: A Quick Overview
BEM: A Concise Explanation
Utility Classes: The Essentials
Title
A Practical Example: A Card Gallery
Home Web Front-end CSS Tutorial Building a Scalable CSS Architecture With BEM and Utility Classes

Building a Scalable CSS Architecture With BEM and Utility Classes

Apr 07, 2025 am 10:14 AM

Building a Scalable CSS Architecture With BEM and Utility Classes

Managing extensive CSS projects presents significant challenges. Over time, various approaches have emerged to streamline large-scale CSS development. The common goals remain: efficiency (minimizing design time, maximizing coding time) and consistency (ensuring uniform coding practices across the team).

For over a year and a half, I've contributed to CodyFrame, a component library and front-end framework boasting 220 components. These aren't isolated modules; they're reusable patterns, often integrated to build complex layouts. This project's complexities necessitated a scalable CSS architecture, combining global CSS, BEM, and utility classes.

Let's explore this approach.

CSS Globals: A Quick Overview

Global CSS files house rules applicable across all components (e.g., spacing, typography, colors). Tokens maintain design consistency and reduce component CSS size.

Here's a typography global example:

/* Typography | Global */
:root {
  /* body font size */
  --text-base-size: 1em;

  /* type scale */
  --text-scale-ratio: 1.2;
  --text-xs: calc((--text-base-size / var(--text-scale-ratio)) / var(--text-scale-ratio));
  --text-sm: calc(var(--text-xs) * var(--text-scale-ratio));
  --text-md: calc(var(--text-sm) * var(--text-scale-ratio) * var(--text-scale-ratio));
  --text-lg: calc(var(--text-md) * var(--text-scale-ratio));
  --text-xl: calc(var(--text-lg) * var(--text-scale-ratio));
  --text-xxl: calc(var(--text-xl) * var(--text-scale-ratio));
}

@media (min-width: 64rem) { /* responsive decision applied to all text elements */
  :root {
    --text-base-size: 1.25em;
    --text-scale-ratio: 1.25;
  }
}

h1, .text-xxl  { font-size: var(--text-xxl, 2.074em); }
h2, .text-xl   { font-size: var(--text-xl, 1.728em); }
h3, .text-lg   { font-size: var(--text-lg, 1.44em); }
h4, .text-md   { font-size: var(--text-md, 1.2em); }
.text-base     { font-size: var(--text-base-size); }
small, .text-sm { font-size: var(--text-sm, 0.833em); }
.text-xs       { font-size: var(--text-xs, 0.694em); }
Copy after login

BEM: A Concise Explanation

BEM (Blocks, Elements, Modifiers) is a naming convention for reusable components.

Example:

<a href="https://www.php.cn/link/5982407c8b651cdf1ecdc48937c14cbe"></a>
<nav>
  <ul>
    <li><a href="https://www.php.cn/link/5982407c8b651cdf1ecdc48937c14cbe">Homepage</a></li>
    <li><a href="https://www.php.cn/link/5982407c8b651cdf1ecdc48937c14cbe">About</a></li>
    <li><a href="https://www.php.cn/link/5982407c8b651cdf1ecdc48937c14cbe">Contact</a></li>
  </ul>
</nav>
Copy after login
  • Block: A reusable component.
  • Element: A child of a block (e.g., .block__element).
  • Modifier: A variation of a block/element (e.g., .block--modifier, .block__element--modifier).

Utility Classes: The Essentials

Utility classes perform single functions. For instance:

.padding-sm { padding: 0.75em; }
.padding-md { padding: 1.25em; }
.padding-lg { padding: 2em; }
Copy after login

Entire components can be built using utility classes:

<h1 id="Title">Title</h1>
<p>Lorem ipsum...</p>
Copy after login

Utility classes can leverage global CSS variables:

/* Spacing | Global */
:root {
  --space-unit: 1em;
  --space-xs: calc(0.5 * var(--space-unit));
  --space-sm: calc(0.75 * var(--space-unit));
  --space-md: calc(1.25 * var(--space-unit));
  --space-lg: calc(2 * var(--space-unit));
  --space-xl: calc(3.25 * var(--space-unit));
}

/* responsive rule affecting all spacing variables */
@media (min-width: 64rem) {
  :root {
    --space-unit: 1.25em; /* this responsive decision affects all margins and paddings */
  }
}

/* margin and padding util classes - apply spacing variables */
.margin-xs { margin: var(--space-xs); }
.margin-sm { margin: var(--space-sm); }
.margin-md { margin: var(--space-md); }
.margin-lg { margin: var(--space-lg); }
.margin-xl { margin: var(--space-xl); }

.padding-xs { padding: var(--space-xs); }
.padding-sm { padding: var(--space-sm); }
.padding-md { padding: var(--space-md); }
.padding-lg { padding: var(--space-lg); }
.padding-xl { padding: var(--space-xl); }
Copy after login

Let's create a card gallery, first using only BEM, then incorporating globals and utility classes to highlight their benefits. The final result will demonstrate the power of this combined approach.

(The remainder of the article continues with the detailed example, comparing BEM-only, BEM Globals, and BEM Globals Utility Classes approaches, and concluding with file structure recommendations.)

The above is the detailed content of Building a Scalable CSS Architecture With BEM and Utility Classes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

The Three Types of Code The Three Types of Code Apr 11, 2025 pm 12:02 PM

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

See all articles