惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

CSS Wizardry

Front-End’s Missing Metric: The TBT Window Meet Your Users Where They Are with Obs.js Better Browser Caching with No-Vary-Search font-family Doesn’t Fall Back the Way You Think What Is CSS Containment and How Can I Use It? When All You Can Do Is All or Nothing, Do Nothing Obs.js: Context-Aware Web Performance for Everyone Low- and Mid-Tier Mobile for the Real World (2025) The Fastest Site in the Tour de France Making Sense of the Performance Extensibility API Why Do We Have a Cache-Control Request Header? HTML Is Not a Programming Language… Build for the Web, Build on the Web, Build with the Web Licensing Code on CSS Wizardry A Layered Approach to Speculation Rules Designing (and Evolving) a New Web Performance Score Core Web Vitals Colours The Ultimate Contract Templates for Tech Consultants: Protect Your Business and Get Paid Optimising for High Latency Environments Cache Grab: How Much Are You Leaving on the Table? blocking=render: Why would you do that?! Correctly Configure (Pre) Connections The Three Cs: 🤝 Concatenate, 🗜️ Compress, 🗳️ Cache What Is the Maximum max-age? How to Clear Cache and Cookies on a Customer’s Device The Ultimate Low-Quality Image Placeholder Technique Core Web Vitals for Search Engine Optimisation: What Do We Need to Know? The HTTP/1-liness of HTTP/2 In Defence of DOM­Content­Loaded Site-Speed Topography Remapped Why Not document.write()? Speeding Up Async Snippets Critical CSS? Not So Fast! Measure What You Impact, Not What You Influence Optimising Largest Contentful Paint Measuring Web Performance in Mobile Safari Site-Speed Topography Speed Up Google Fonts Real-World Effectiveness of Brotli Performance Budgets, Pragmatically Lazy Pre-Browsing with Prefetch Making Cloud.typography Fast(er) Time to First Byte: What It Is and How to Improve It Self-Host Your Static Assets Tips for Technical Interviews Cache-Control for Civilians Bandwidth or Latency: When to Optimise for Which ITCSS × Skillshare What If? CSS and Network Performance The Three Types of Performance Testing Getting to Know a Legacy Codebase Image Inconsistencies: How and When Browsers Download Images Identifying, Auditing, and Discussing Third Parties My Digital Music Setup Measuring the Hard-to-Measure Finding Dead CSS The Fallacies of Distributed Computing (Applied to Front-End Performance) Ten Years Old Relative Requirements Airplanes and Ashtrays Performance and Resilience: Stress-Testing Third Parties Refactoring Tunnels Little Things I Like to Do with Git Writing Tidy Code Configuring Git and Vim Base64 Encoding & Performance, Part 2: Gathering Data Base64 Encoding & Performance, Part 1: What’s Up with Base64? Code Smells in CSS Revisited Typography for Developers Moving CSS Wizardry onto HTTPS and HTTP/2 Ack for CSS Developers A New Year, a New Focus Preparing Vim for Apple’s Touch Bar Choosing the Correct Average CSS Shorthand Syntax Considered an Anti-Pattern CSS Wizardry Newsletter Nesting Your BEM? Improving Perceived Performance with Multiple Background Images Continue Normalising Your CSS Pure CSS Content Filter Pragmatic, Practical, and Progressive Theming with Custom Properties Refactoring CSS: The Three I’s Speaker’s Checklist: Before and After Your Talk Improving Your CSS with Parker The Importance of !important: Forcing Immutability in CSS Mixins Better for Performance Managing Typography on Large Apps White October Events Workshop Partnership BEMIT: Taking the BEM Naming Convention a Step Further Travelling Like You Want to, When You Have To Contextual Styling: UI Components, Nesting, and Implementation Detail Subtleties with Self-Chained Classes Cyclomatic Complexity: Logic in CSS Immutable CSS More Transparent UI Code with Namespaces When to use @extend; when to use a mixin The Specificity Graph CSS Wizardry Ltd.: Year 1 in review CSS Guidelines 2.0.0
Can CSS Be Too Modular?
Harry Roberts · 2015-03-08 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

I frequently get asked questions along the lines of Can CSS be too modular? I recently got asked about this in an email, which I’ve published below:


Harry,

I wanted to first thank you for creating inuitcss which I point my students to in my Object-Oriented CSS class I teach. I do have a philosophical question. From experience, I noticed that my websites have become abstraction heavy, and more often than not code was being duplicated. It is really because abstractions are owned by an object.

For example, I created a simple object called .box with abstractions to change the padding. Later I created an object for alert messages, inline labels, and inputs and noticed that the sizing abstractions were the same. Since then, I moved away from creating abstractions unless they need to target a child element or are SPECIFIC to that object, and began working with atoms.

So:

.box          { }
.box--xs      { padding: 6px; }
.box--s       { padding: 12px; }
.box--m       { padding: 24px; }
.box--l       { padding: 36px; }
.box--xl      { padding: 48px; }

.alert        { }
.alert--xs    { padding: 6px; }
.alert--s     { padding: 12px; }
.alert--m     { padding: 24px; }
.alert--l     { padding: 36px; }
.alert--xl    { padding: 48px; }

.label        { }
.label--xs    { padding: 6px; }
.label--s     { padding: 12px; }
.label--m     { padding: 24px; }
.label--l     { padding: 36px; }
.label--xl    { padding: 48px; }

became an atom called .pa (padding-all):

.pa--xs       { padding: 6px; }
.pa--s        { padding: 12px; }
.pa--m        { padding: 24px; }
.pa-l         { padding: 36px; }
.pa--xl       { padding: 48px; }

Now it could be applied to any element and is not bound to any particular object.

I read a fantastic article on Smashing Magazine that goes over this technique. Yahoo has implemeneted it. Atoms really are low-level utility classes that cache well. I think the only reason to keep abstractions are:

  1. They target a specific object, which atoms can not do
  2. Too many atoms would be needed to make up the abstraction (like your .btn class)
  3. An abstraction is SPECIFIC to an object. Atoms are not.

This is no way replaces modular design, but rather compliments. Everyone uses helper classes, (inuitcss, Bootstrap, SUIT) but creating an atom pool seems to be the next evolution in creating scalable frameworks.

Here is the article (the authors naming conventions for font-sizes are awful it would be better to do .fz-s, .fz-m, .fz-l in case they change):

smashingmagazine.com/…/challenging-css-best-practices-atomic-approach/

I was wondering how you felt about this? Is my thinking on this wrong? When I tell my students about this, I always warn them first I have only seen a handful of websites (besides my own) that use it.

Thanks,
[Name]


To which my response was:


Hey!

Thanks for getting in touch. This is a really interesting one, and something I came up against in my own work a couple of years ago.

The problem, I think, is that with any new tool or technique we learn, we try and take it to its fullest extreme. We then see how that led us slightly astray, and then we begin to correct. Almost any dev problem/solution I’ve ever seen looks like a pendulum: one extreme; extreme overcorrection; finding the sweet spot.

With that in mind, I think that old-style CSS (no classes, very convoluted selectors, etc.) was one peak of the pendulum, and something like Atomic CSS (or anything that begins to replicate/emulate inline styles) is the peak on the opposite side: vast overcorrection to take us as far from the other option as possible. I think we need to find the sweet spot.

With Atomic CSS (this is a pseudo-code example; it doesn’t use any actual Atomic CSS names) we might have really, really, really DRY CSS, but some DOM like this:

<div class="d-bk  p-md  bg-w">
    <a href="#" class="p-sm  fs-m  d-bk  c-gn">Log in</a>
</div>

I know very little about this HTML. Sure, I can tell you which declarations are acting on it (it’s pretty much inline styles) but I couldn’t tell you:

  • Where does the ‘scope’ of this section of DOM start and stop?
  • Are the div and a part of the same UI component?
  • Could the div or a exist without one another?
  • Are any of the classes thematically related?
  • Do these classes appear side by side purely coincidentally?
  • Do we need all classes to remain intact if we were to reuse this HTML?
  • Do all classes when combined create a specific UI component?
  • Are any of the classes optional at this point?
  • If one of those classes changes in this bit of DOM, will they need to be changed in every similar piece of DOM?

Sure, we have tiny CSS, but our HTML has completely lost its ability to be read, or to self-document. This is too extreme.

We could get around this by having some sort of meta templating language that sits above the HTML, so something like (again, pseudo-code):

<% ui-component('promo-button') %>

…might spit out a series of Atomic classes that, when combined, create that piece of UI, but then we’re just designing more complexity into the system in order to solve a problem that we invented in the first place, which seems quite counterproductive.

(Relatedly, we could wrap up the Atomic classes in our Sass, e.g.:

.btn-promo {
  @extend .p-sm;
  @extend .fs-m;
  @extend .d-bk;
  @extend .c-gn;
}

…but this brings the same readability problems as before, as well as being complete overkill—exclusively @extending single declarations will lead to a very unusual project, as well as very tightly coupled dependencies.)

…more often than not code was being duplicated.

Back on the subject of DRYness. To quote myself from this article: Repetition in a compiled system is not a bad thing: repetition in source is a bad thing.

That is to say, having padding: 6px; appear in your CSS 50 times is no big deal. You will feel no performance impact. Gzip will crunch the bejeezus out of it.

However, manually typing out padding: 6px; 50 times in your Sass (or LESS, or whatever) is a problem. DRYness is the pursuit of a Single Source of Truth, meaning that any key data exists (and therefore needs to be changed) only once in the codebase. This ensures consistency (updated values are picked up throughout the entire project) and, more importantly, ease of maintenance. DRY is about repetition in source, not repetition-in-general.

To this end, I would just ensure that the values we’re interested in (6px, 12px, 24px, etc.) are only ever written once and then are simply recycled throughout the project. This is just as DRY (in terms of the Single Source of Truth) but also allows for more readability and context/knowledge in the HTML.

Here’s a very crude proof of concept: sassmeister.com/…/87b2da6cbd80d3f1f993. We can see from this that we only actually have one number involved at all, so we have achieved the Single Source of Truth, and we can just reuse it and its spawned size-variants very easily.

And now our HTML is more like this:

<div class="box  box--promo">
    <a href="#" class="btn  btn--small  btn--positive">Log in</a>
</div>

This tells us a heck of a lot about what’s going on in the DOM, it tells us that these classes are thematically related; it tells us which bits are optional, and it tells us what are candidates for change (i.e. anything with a -- prefix).

There is absolutely no reason you can’t have utility classes—I even do this myself—but they should be the exception rather than the rule. Avoid building entire sections of UI out of them.

Abstracting classes out as far as Atomic CSS (or any other similar approach—this isn’t a criticism of Atomic CSS specifically) suggests will sacrifice (at the very least) context, readability, and maintainability, in favour or smaller filesizes. I feel this is overkill, especially considering that gzip would negate any difference in filesize anyway. We need to find that sweet spot, and I feel Atomic CSS (and paradigms like it) overshoot the mark.

Wow, that was a very long answer. I hope it helps.

Best,
Harry