There's no shortage of opinions about how CSS should be written. Tailwind or BEM? CSS Modules or CSS-in-JS? Sass, or is native CSS good enough now? The debate can get framed as a choice where you pick one and then have to defend it.
In practice, these approaches aren't mutually exclusive. Most real projects combine several of them. A typical codebase might have a global stylesheet for resets† and design tokens, a component framework for layout, CSS Modules for the odd tricky component, and a few inline styles for values computed at runtime. A long-lived enterprise site might run Sass with BEM for its core components and gradually adopt native nesting and cascade layers as browser support allows.
So the useful question isn't "which approach is best?" but "which approach suits this part of this project?" This guide walks through the common options, the trade-offs, and where each one fits.
Before getting stuck in, let's look at SSR, cached HTML and SPAs
One factor you need to take into account before any CSS comparisons, as it can decide the matter on large sites: where your HTML lives, and how long it's cached.
Server-rendered sites with cached HTML
Many large sites (CMS-driven sites, e-commerce, publishing, AEM - classic, Drupal, WordPress, the list goes on) render HTML on the server and cache it aggressively at the CDN level (Akamai, Cloudflare, Fastly). These pages may be cached indefinitely, until the content is updated.
On these sites, there's a big difference in cost between two kinds of change:
- Changing a CSS file is cheap. You deploy one file and purge one URL, and the change is live in seconds.
- Changing the HTML is expensive. If a shared component like the main menu appears on thousands of pages, changing one class name in its markup means invalidating or regenerating thousands of cached pages. That can mean a slow, risky cache flush, a spike in origin traffic.
So on cached server-rendered sites, approaches where styling decisions live in the CSS and class names stay stable (BEM, Suit CSS, semantic or component names) have a real operational advantage. Approaches where styling decisions live in the markup (utility classes, inline styles, generated class names written into SSR output) turn a visual tweak into a site-wide HTML change.
Single-page applications (SPAs). In a client-rendered SPA, the markup ships inside the JavaScript bundle. The HTML document is a near-empty shell, and the bundle is versioned and replaced on every deploy. Changing a class name in a component is just part of the next build. The caching cost described above largely disappears, which is one reason utility-first and CSS-in-JS approaches thrive in SPA-heavy ecosystems.
Hybrid frameworks (Next, Nuxt, SvelteKit, Astro). These sit in between. A statically generated or server-rendered page cached at the edge behaves like the first case; a client-side navigation behaves like the second. If your pages are cached for long periods, assume the server-rendered rules apply.
Keep this in mind as you read each section. The same approach can be the right call for a SPA and the wrong one for a 50,000-page cached marketing site.
Contents
- Vanilla CSS
- BEM and naming methodologies
- Preprocessors: Sass and Less
- CSS Modules
- Scoped component styles
- Utility-first CSS
- Runtime CSS-in-JS
- Zero-runtime CSS-in-JS
- Inline styles
- Shadow DOM and Web Components
- Component frameworks
- Wrapping up
Vanilla CSS
Plain stylesheets, written by hand and linked from the page. Every selector is global, and the cascade and specificity decide which rule wins.
Vanilla CSS has changed a lot in recent years. Features that once required a preprocessor or a JavaScript library are now native: nesting, custom properties (CSS variables) for theming and design tokens, cascade layers (@layer) for controlling specificity between resets, frameworks and your own code, @scope for limiting where selectors apply, container queries for component-level responsiveness, and :has() for parent selection. For many projects, the gap between "plain CSS" and a CSS preprocessing tool is now quite small.
/* styles.css */
h1 {
color: red;
}
<link rel="stylesheet" href="styles.css">
<h1>Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
A great fit for small sites, prototypes, static pages and documentation, and as the foundation layer of any project (resets, tokens, base typography). It's also a strong choice for cached server-rendered sites, since class names stay stable and restyling ships in the CSS file alone.
One caching caveat: if you use fingerprinted filenames (styles.a1b2c3.css) for cache-busting, the new filename is written into the HTML, so every CSS release forces an HTML purge anyway. On heavily cached sites, consider a stable CSS URL with a shorter TTL, or a targeted CDN purge of just the stylesheet.
On its own, vanilla CSS scales poorly without conventions. That's why it's usually paired with a methodology such as BEM, which is next.
BEM and naming methodologies
BEM (Block, Element, Modifier) isn't a tool; it's a naming convention layered on top of plain CSS or Sass. A block is a standalone component (.card), an element is a part of it (.card__title), and a modifier is a variation (.card--featured). Related methodologies include SMACSS, OOCSS, SUIT CSS and ITCSS (which is more about file organisation and layering).
The goal is to simulate component scoping through discipline: class names are unique by convention, every selector is a single class (so specificity stays flat and predictable), and you can tell from the HTML alone which CSS applies.
.page-title--alert {
color: red;
}
<h1 class="page-title page-title--alert">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
The best fit for large server-rendered sites with heavily cached HTML: CMS-driven builds, AEM component libraries, e-commerce and publishing. Because class names are a stable contract, restyling a menu used on thousands of pages is a single CSS deploy rather than a site-wide cache flush. Markup and styling can also be owned by different teams (for example, CMS authors and back-end developers producing HTML, front-end developers owning CSS) with the class names as the agreed interface.
BEM pairs naturally with Sass, where the &__element and &--modifier syntax keeps source files tidy. It's less valuable in component frameworks with built-in scoping, where the tool already solves the problem BEM exists for.
Preprocessors: Sass and Less
A preprocessor is a CSS superset compiled to plain CSS at build time. Sass (usually in its SCSS syntax) is by far the most popular today; Less is still found in older codebases and Bootstrap 3-era projects.
Preprocessors add variables, nesting, mixins (reusable chunks of declarations), functions and maths, loops for generating utility classes, and partials with imports for splitting styles across files. Modern Sass uses a module system (@use and @forward) that replaces the old global @import.
Some of these features are now native in CSS (nesting, variables), but Sass still offers things CSS doesn't: mixins, loops, maps, and compile-time logic for generating design-system output.
$title-colours: (
red: #c00,
green: #080,
blue: #00c,
);
@each $name, $value in $title-colours {
.title-#{$name} {
color: $value;
}
}
<h1 class="title-red">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
A good fit for large traditional sites, design systems and enterprise codebases, particularly in combination with BEM. Caching behaviour is the same as vanilla CSS: the output is a stylesheet, so refactors and variable changes don't touch the cached HTML as long as the output class names stay stable.
For new projects, it's worth asking whether native CSS now covers what you need. If you're only using variables and nesting, it probably does. If you're generating spacing scales, colour palettes or utility classes from maps and loops, Sass still earns its place.
CSS Modules
CSS Modules are ordinary .css (or .scss) files that a bundler such as Vite or webpack processes so that every class name is locally scoped by default. You import the file into a JavaScript component and get back an object mapping your class names to generated unique names. Two components can both define .title without colliding.
You still write standard CSS. There's no new syntax to learn beyond composes for sharing styles and :global for opting out of scoping.
/* title.module.css */
.title {
color: red;
}
import s from './title.module.css';
<h1 className={s.title}>Hello</h1>
<!-- Rendered HTML -->
<h1 class="title_x7f3a">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
A strong fit for React and Next apps that want scoping without adopting a new paradigm. It's also a good migration path from a global Sass codebase, since the CSS barely changes.
Caching: in webpack and Next.js, generated class names are derived from the file path and class name, not the CSS content, so editing styles leaves cached HTML valid. Vite’s default also hashes the file’s content, so any edit renames its classes. Set css.modules.generateScopedName to a path-based pattern if that matters to you. In every setup, renaming a class, moving a file or changing the hash configuration renames that class in every cached page. In an SPA that's irrelevant; in statically generated or SSR output behind long-lived caches, treat file moves and renames as HTML-affecting changes.
Scoped component styles
Vue, Svelte and Angular all let you write CSS directly inside a component file, and the framework compiles it so the styles apply only to that component. Vue uses <style scoped>, Svelte scopes styles by default, and Angular uses view encapsulation.
Under the hood, the compiler adds a unique attribute or class to the component's elements (for example, data-v-4f2a1c in Vue) and rewrites your selectors to match it. You write ordinary CSS, co-located with the markup that uses it.
<!-- Vue single-file component -->
<template>
<h1>Hello</h1>
</template>
<style scoped>
h1 {
color: red;
}
</style>
<!-- Rendered output -->
<h1 data-v-4f2a1c>Hello</h1>
<style>
h1[data-v-4f2a1c] { color: red; }
</style>
| Pros | Cons |
|---|---|
|
|
Summary
The natural default for Vue/Nuxt, Svelte/SvelteKit and Angular projects. It solves scoping and dead-code problems with very little ceremony.
Caching: the scope identifiers are written into the rendered HTML, and in some frameworks they can change when the component file changes (Vue, for example, includes the file's contents in its production hash). On a client-rendered SPA this doesn't matter. On a Nuxt or SvelteKit site serving long-cached SSR or statically generated pages, editing a shared component may mean regenerating every page that uses it. Worth checking how your framework generates its scope IDs before relying on CSS-only deploys.
Utility-first CSS
Utility-first frameworks such as Tailwind CSS and UnoCSS provide a large set of small, single-purpose classes (text-red-600, p-4, flex, md:grid-cols-3), and you compose them directly in your markup. You rarely write custom CSS at all.
At build time, the tool scans your templates and generates only the classes you actually use, so the production stylesheet is typically small. Design tokens (colours, spacing, breakpoints) are defined once in configuration, which keeps the UI consistent. Tailwind v4 moved that configuration into CSS itself using @theme.
<h1 class="text-red-600">Hello</h1>
/* Generated CSS */
.text-red-600 {
color: rgb(220 38 38);
}
| Pros | Cons |
|---|---|
|
|
Summary
An excellent fit for product UIs, SPAs, startups, rapid prototyping, and teams working from an established design system. In component-based apps, the "noisy markup" complaint is softened because the classes live inside a reusable component rather than being repeated across pages.
Caching: this is the most expensive approach for large cached server-rendered sites. Styling lives in the markup, so changing how the menu looks means changing the classes in the HTML, which invalidates every cached page that contains the menu. The rebuilt CSS also needs to deploy in step with the HTML, because newly used classes won't exist in the old stylesheet. In an SPA, where the markup ships in the JS bundle, this is a non-issue.
Accessibility note: because styling is applied per element, it's easy to forget focus styles, prefers-reduced-motion variants and forced-colours support. Bake them into your shared components rather than relying on developers remembering the right utility each time.
Reset note: Tailwind ships with Preflight, a fairly aggressive base reset that removes heading sizes, list styles and margins from every element. That's convenient in a fully componentised app, but it catches people out with CMS or Markdown content, which then needs the typography plugin (prose) to get readable defaults back. See A note on CSS resets for the alternative of resetting only what you style.
Runtime CSS-in-JS
Libraries such as styled-components and Emotion let you write CSS inside JavaScript, usually as tagged template literals or style objects. The library generates a unique class name, injects the CSS into the page at runtime, and attaches the class to your component.
Because styles are JavaScript, they have full access to props, state and theme objects. This made CSS-in-JS very popular in the React ecosystem in the late 2010s. The trade-off is that style generation happens in the browser (or on the server during SSR), which adds runtime cost and complicates server rendering. React Server Components don't support this model well, and styled-components entered maintenance mode in 2025.
const H1 = styled.h1`
color: red;
`;
<H1>Hello</H1>
<!-- Rendered HTML and injected CSS -->
<h1 class="sc-a1b2c3">Hello</h1>
<style>
.sc-a1b2c3 { color: red; }
</style>
| Pros | Cons |
|---|---|
|
|
Summary
Best suited to existing React SPAs that already use it. For new projects, zero-runtime alternatives (next section) or CSS Modules are generally a better choice.
Caching: with SSR, the generated class names and critical CSS are inlined into each page's HTML, so any style change alters the cached HTML site-wide. In a client-rendered SPA this is acceptable; for large cached server-rendered sites it's a poor fit.
Zero-runtime CSS-in-JS
Tools such as vanilla-extract, Linaria, Panda CSS and Meta's StyleX keep the developer experience of writing styles in TypeScript or JavaScript, but extract static CSS at build time. Nothing is generated in the browser.
The biggest benefit is type safety: design tokens become typed objects, so a typo in a colour name is a compile error rather than a silent bug. Dynamic styling is handled through CSS custom properties rather than generating new CSS at runtime.
There are two flavours worth distinguishing. Scoped-class tools (vanilla-extract, Linaria) produce one class per style block, much like CSS Modules. Atomic tools (StyleX, and Panda in atomic mode) produce one class per declaration, much like Tailwind.
// title.css.ts (vanilla-extract)
import { style } from '@vanilla-extract/css';
export const title = style({ color: 'red' });
<h1 className={title}>Hello</h1>
<!-- Extracted CSS and rendered HTML -->
<style>
.title_1x2y3z { color: red; }
</style>
<h1 class="title_1x2y3z">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
A good fit for typed design systems, large React or Next apps, and performance-sensitive builds where you want CSS-in-JS ergonomics without the runtime.
Caching depends on the flavour. Scoped-class tools generally keep class names stable when you edit style values, so changes ship in the CSS file. Atomic tools behave like Tailwind: changing a value changes the class names in the markup, so cached HTML must be regenerated.
Inline styles
Styles applied directly to an element through the style attribute in HTML, or the style prop in a framework. There's no stylesheet and no selector; the declaration applies only to that element.
<h1 style="color: red;">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
Inline styles have a few legitimate uses:
For anything shared, avoid them. On cached sites, every visual change is an HTML change, so a tweak to a shared component means invalidating every page that contains it.
Shadow DOM and Web Components
Web Components are native browser custom elements. When a component uses a shadow root, its internal markup and styles are encapsulated: page selectors can’t reach elements inside it, and component styles can’t leak out. It’s the only approach on this list with browser-enforced selector isolation in both directions.
The boundary isn't completely sealed, though. Inherited properties such as color and font-family flow in from the host element, so components pick up the page's base typography unless they reset it with :host { all: initial; }. Custom properties pass through the same way, which makes them the main theming mechanism, and ::part() lets a component expose specific internal elements for external styling. Slotted content stays in the page's DOM, so page styles apply to it as normal. Libraries such as Lit make authoring Web Components much more ergonomic.
class MyTitle extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = `
<style>
:host { display: block; }
h1 { color: red; }
</style>
<h1><slot></slot></h1>
`;
}
}
customElements.define('my-title', MyTitle);
<my-title>Hello</my-title>
| Pros | Cons |
|---|---|
|
|
Summary
A good fit for embeddable widgets, components that must work across several frameworks, and design systems shared between teams using different stacks. It's less suited as the main styling approach for a single-framework application.
Caching: with declarative shadow DOM (server-rendered), styles are often inlined into each page, so changes require regenerating the cached HTML. Linking an external stylesheet from inside the shadow root avoids this.
Accessibility note: ARIA attributes that reference IDs (aria-labelledby, aria-describedby) can't cross the shadow boundary, so labelling relationships between page content and component internals need careful design.
Component frameworks
Prebuilt CSS frameworks such as Bootstrap, Bulma and Foundation ship a complete stylesheet of components (buttons, cards, navbars, modals) and utility classes. You apply them by adding classes to your markup and customise them through variables or overrides.
/* Provided by Bootstrap */
.text-danger {
color: var(--bs-danger) !important;
}
<link rel="stylesheet" href="bootstrap.min.css">
<h1 class="text-danger">Hello</h1>
| Pros | Cons |
|---|---|
|
|
Summary
A good fit for admin panels, internal tools, MVPs and projects where speed matters more than a distinctive design.
Caching: theme tweaks through variables or overrides only change the CSS file, which is cheap. Major-version upgrades that rename classes (for example, Bootstrap 4's ml-* becoming ms-* in Bootstrap 5) touch the markup and therefore every cached page. Plan upgrades accordingly.
Accessibility note: framework components give you a reasonable starting point, but "accessible baselines" isn't the same as accessible. Customisations, colour overrides and how components are assembled still need testing.
Wrapping up
There's no single right way to write CSS, and most mature projects end up with a combination. A few principles help when choosing:
- Start with where your HTML lives. On large server-rendered sites with cached HTML, prefer approaches where restyling changes only the CSS file and class names stay stable. In SPAs, that constraint mostly disappears, and component-scoped or utility-first approaches become more attractive.
- Use the platform first. Native CSS now covers nesting, variables, layers, scoping and container queries. Add tooling for what it genuinely adds, not out of habit.
- Keep browser defaults unless you're replacing them. Reset only the elements you're actually going to style, rather than stripping everything globally and hoping every element gets restyled.
- Match the approach to the layer. Global CSS for a light base and tokens, a scoped or utility approach for components, inline custom properties for runtime values. Mixing approaches deliberately is fine; mixing them accidentally is how codebases get messy.
-
Bake accessibility into the shared pieces. Whatever the approach, focus styles,
prefers-reduced-motion, forced-colours support and sufficient contrast should live in shared components and tokens, so they aren't left to each developer to remember.
The best choice is the one your team can apply consistently, that suits how your pages are rendered and cached, and that you'll still be comfortable maintaining in five years.
† A note on CSS resets
Since the foundation layer often starts with a reset, I'll come clean and say I'm not a fan of aggressive CSS resets. Blanket rules like * { margin: 0; padding: 0; }, or full resets that strip list bullets, heading sizes and form control styling from every element, meaning anything without specific styles renders as unformatted text, often without any space or gaps.
I'd much rather have the browser's default styles than no styles at all! Browser defaults are readable, familiar and carry useful visual cues: headings look like headings, lists have bullets, links are underlined, buttons look like buttons, hover and focus outlines are visible. That matters most for content you don't fully control, such as rich text from a CMS editor, Markdown output or third-party embeds.
There are accessibility considerations. Removing list styling with list-style: none can cause Safari/VoiceOver to stop announcing the element as a list, and resets that include outline: none remove the focus indicator keyboard users rely on.
So my recommendation is to only reset the elements you are going to style, and add the base styles you want in the same location so the reset and the default value are easy to find.
Top comments (0)