Skip to content
Tutorials Center
My learning

Topics

Lesson 116 of 139 ยท Best Practices

Preprocessors and Frameworks

Learn what Sass, Less and PostCSS add to CSS, how Tailwind and Bootstrap work, which features native CSS now covers, and how to decide what your project needs.

For most of CSS's history, the language lacked features that programmers take for granted: variables, nesting, reusable snippets, splitting code into modules. Tools grew up to fill the gaps. Preprocessors such as Sass and Less let you write an extended language that compiles to plain CSS. Post-processors such as PostCSS transform CSS after you write it. And frameworks such as Bootstrap and Tailwind hand you a ready-made set of classes so you write less CSS yourself.

Then CSS caught up. Custom properties, native nesting, cascade layers, color-mix() and container queries now cover much of what these tools were invented for. So the question today is less "which tool?" and more "what does this project still need a tool for?" This lesson explains what each tool does, shows how its key features map to native CSS, and gives you a practical way to decide. The tool code is read-only (it needs a build step), while the live examples show the native equivalents.

What a build step is

Browsers only understand CSS. A preprocessor file such as styles.scss must be compiled to styles.css before the browser can use it:

TEXT
src/styles.scss  โ”€โ”€(Sass compiler)โ”€โ”€โ–ถ  dist/styles.css  โ”€โ”€โ–ถ  <link rel="stylesheet">

In practice, a bundler such as Vite runs the compiler every time you save, so it feels instant. But it's still an extra tool to install, configure and keep updated, and the CSS you debug in DevTools is the output, not what you wrote (source maps help link the two). Every feature below is worth weighing against that cost.

Sass and SCSS

Sass is the most widely used preprocessor. It has two syntaxes: the original indented syntax (.sass, no braces or semicolons) and SCSS (.scss), which is a superset of CSS: every valid CSS file is valid SCSS. Almost everyone uses SCSS today. The official implementation is Dart Sass, available from npm as sass.

Variables

Sass variables start with $:

SCSS
$brand: #4f46e5;
$radius: 8px;

.button {
  background: $brand;
  border-radius: $radius;
}

They look like custom properties, but there's a crucial difference:

  • Sass variables are compile-time. They're replaced by their values when the CSS is built and no longer exist in the browser. You can't change them with a media query, a class or JavaScript.
  • Custom properties are runtime. They live in the browser, inherit through the DOM, and can be changed per element, per theme or per media query at any time.

That makes custom properties strictly more powerful for theming. Sass variables still have a niche: values the browser can't use inside a custom property, such as a breakpoint in a media query condition (@media (width > $tablet)).

Runtime theming that Sass variables can't do

With Sass variables, each theme would need its own compiled stylesheet. With custom properties, one class switches everything instantly.

Nesting

Sass made nesting popular, and native CSS nesting now works in every current browser. The main difference is string building: Sass can glue & to text (&__title becomes .card__title), while native CSS can't. If you rely heavily on that for BEM names, it's a real reason to keep Sass; otherwise native nesting covers the need.

Mixins and functions

A mixin is a reusable block of declarations, optionally with parameters:

SCSS
@mixin focus-ring($color: #f59e0b) {
  outline: 3px solid $color;
  outline-offset: 2px;
}

.button:focus-visible { @include focus-ring; }
.link:focus-visible   { @include focus-ring(#2563eb); }

The compiled CSS simply contains those two declarations in each rule. Sass also has functions that compute values, such as math.div(16px, 2) or color.scale($brand, $lightness: -20%) for a darker shade.

Native CSS covers much of this differently:

  • Shared declarations: a class you add in HTML, or a selector list (.button:focus-visible, .link:focus-visible { ... }), with a custom property for the part that varies.
  • Math: calc(), clamp(), min(), max() and other CSS functions run in the browser and can mix units (calc(100% - 2rem)), which Sass never could.
  • Color shades: color-mix() and relative colors (see Modern Color) derive hover and border shades from one base color at runtime.
Native replacements for Sass color functions

Native mixins and functions are coming. The CSS Working Group is designing @mixin and @function rules, and Chromium browsers have already shipped custom @function and an if() function in 2025. They aren't in all browsers yet, so treat them as something to watch rather than to rely on.

Partials, modules and loops

Sass lets you split code into partials, files whose names start with an underscore (_buttons.scss), and combine them into one output file with @use:

SCSS
// main.scss
@use "tokens";
@use "base";
@use "components/buttons";

The output is a single CSS file, with no extra network requests. (Sass's old @import rule is deprecated in favor of @use and @forward, which give each file its own namespace.) In plain CSS, @import makes the browser fetch files one after another, which is slow, as CSS Performance explains. So combining files is still a job for a build tool, but any bundler can do it without Sass.

Sass also has control flow: @if, @each and @for. They're handy for generating repetitive rules, such as a set of spacing utilities:

SCSS
$spaces: (1: 0.25rem, 2: 0.5rem, 4: 1rem, 8: 2rem);

@each $name, $size in $spaces {
  .mt-#{$name} { margin-top: $size; }
}
// โ†’ .mt-1 { margin-top: 0.25rem; } .mt-2 { ... } ...

Native CSS has no loops, and probably never will: generating rules is a build-time task.

Less

Less is an older preprocessor with similar ideas and slightly different syntax: variables start with @, and any class can be used as a mixin:

LESS
@brand: #4f46e5;

.rounded() { border-radius: 8px; }

.card {
  .rounded();
  border: 1px solid fade(@brand, 30%);
}

Less powered early versions of Bootstrap and still appears in older projects, but Sass has long been the more popular choice, and few new projects pick either one for features native CSS already has.

Less in depth

If you inherit a Less codebase, these are the features you will meet. Every output below was produced with the real Less 4 compiler (lessc).

Variables and interpolation. Variables are declared with @ and are lazy: Less uses the last definition in the scope, even if it appears after the use. To put a variable inside a selector or a property name, wrap it in @{...}:

LESS
@brand: #4f46e5;
@selector: card;
@prop: color;

.@{selector}-title { @{prop}: darken(@brand, 10%); }
CSS
.card-title { color: #291fd9; }

Mixins with parameters. Any class (or ID) can be called like a mixin, and parameters can have defaults. Use ; between arguments when a value itself contains commas:

LESS
.rounded(@r: 8px) { border-radius: @r; }
.shadow(@y: 2px; @blur: 6px) { box-shadow: 0 @y @blur rgba(0, 0, 0, 0.2); }

.card {
  .rounded(12px);
  .shadow(4px; 10px);
}
CSS
.card { border-radius: 12px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); }

Nesting and &. Nesting works as in Sass. & stands for the parent selector, so &:hover and &-dark (which builds .card-dark) both work.

Operations and functions. Arithmetic works on numbers, units and colors. Parentheses are needed for division in current versions, so write (@gap / 2). Built-in functions such as darken() and fade() handle color work:

LESS
@gap: 8px;
.card {
  padding: @gap * 2;                     // 16px
  margin: (@gap / 2) 0;                  // 4px 0
  border: 1px solid fade(#4f46e5, 30%);  // rgba(79, 70, 229, 0.3)
}

Guards. A mixin can carry a condition with when, so Less picks the matching version. A guard is Less's answer to @if:

LESS
.theme(@mode) when (@mode = dark)  { background: #111; color: #eee; }
.theme(@mode) when (@mode = light) { background: #fff; color: #111; }

.card-dark  { .theme(dark); }
.card-light { .theme(light); }

A guarded mixin can also call itself with a smaller number, which is how Less does loops:

LESS
.make-cols(@n) when (@n > 0) {
  .col-@{n} { width: (@n * 100% / 4); }
  .make-cols(@n - 1);
}
.make-cols(4);

This generates .col-4 (100%), .col-3 (75%), .col-2 (50%) and .col-1 (25%).

Extend. &:extend(.btn-base) does not copy declarations. It adds the new selector to the existing rule, so the compiled CSS is smaller than the same mixin used twice:

CSS
.btn-base,
.btn-primary { padding: 8px; cursor: pointer; }
.btn-primary { background: #4f46e5; }

Imports. @import "colors"; pulls in colors.less (the extension is optional), and its variables become available to the importing file.

Compiling. You have three options. The command line (npx lessc styles.less styles.css) is the usual build step. Bundlers have a Less loader. The old browser-side less.js compiles in the page at load time, which is fine for experiments but should not ship to production, because every visitor pays for the compile on every visit.

Much of the above has a native counterpart today: variables are custom properties, nesting is native nesting, and fade() or darken() are covered by color-mix() and relative colors in Modern Colors. Mixins, guards and loops are what native CSS still lacks.

PostCSS: transforming CSS with plugins

PostCSS is a different kind of tool. It reads ordinary CSS, turns it into a tree, runs plugins over it and writes CSS back out. On its own it does nothing; the plugins do the work. Popular ones:

  • Autoprefixer adds vendor prefixes (such as -webkit-) based on the browsers you say you support, so you write standard properties only.
  • postcss-preset-env converts newer CSS into code older browsers understand, where that's possible.
  • cssnano minifies the output.
JavaScript
// postcss.config.js
export default {
  plugins: {
    "postcss-preset-env": {},
    cssnano: {},
  },
};

The browser targets usually come from a shared Browserslist setting (for example "> 0.5%, last 2 versions, not dead"). Many bundlers now use Lightning CSS, a very fast tool that does prefixing, syntax lowering and minification in one step. Either way, the idea is the same: write modern, standard CSS and let the tool handle compatibility.

PostCSS works happily alongside Sass, or instead of it. Many teams have dropped Sass entirely and kept only a PostCSS or Lightning CSS step for prefixes and minification.

Frameworks

A CSS framework gives you prebuilt styles so you can build interfaces without writing much CSS. The two dominant approaches are very different.

Component frameworks: Bootstrap

Bootstrap (and similar libraries) provides finished components: buttons, cards, navbars, modals, a grid system, plus some utilities. You build pages by adding its class names:

HTML
<button class="btn btn-primary">Save</button>

<div class="card">
  <div class="card-body">
    <h5 class="card-title">Plan</h5>
    <p class="card-text">Everything you need to start.</p>
  </div>
</div>

Strengths: very fast to get a consistent, accessible-enough interface; well documented; customizable through Sass variables and, in recent versions, CSS custom properties. Great for admin panels, prototypes and teams without a designer.

Weaknesses: sites built with defaults all look alike; you ship CSS for components you don't use unless you trim the build; and deep customization means fighting the framework's own specificity.

Utility-first frameworks: Tailwind CSS

Tailwind CSS takes the utility-class idea to its conclusion. It has no buttons or cards, only thousands of small single-purpose classes, and you compose designs in your HTML:

HTML
<button class="rounded-lg bg-indigo-600 px-4 py-2 font-semibold text-white
               hover:bg-indigo-700 focus-visible:outline-3 focus-visible:outline-amber-500">
  Save
</button>

The build tool scans your templates and generates CSS only for the classes you actually use, so the output stays small. Variants such as hover:, md: (a breakpoint) and dark: compile to the matching pseudo-classes and media queries. Recent versions (v4) are configured in CSS itself with an @theme block of custom properties, and build on cascade layers and other modern features.

The utility-first idea, hand-written

Strengths: you rarely name things or switch files; the CSS stops growing as the project grows; consistent spacing and color scales; deleting markup deletes its styles.

Weaknesses: long class lists that some people find hard to read; a build step is required; repeated patterns need extracting into template components (or @apply) to avoid copy-paste; and you still need to understand CSS, because every utility is just a CSS declaration.

Other approaches in brief

  • Classless frameworks style plain HTML elements directly, with no classes at all. Nice for documents and quick prototypes.
  • CSS-in-JS and CSS Modules keep styles next to JavaScript components and scope class names automatically. Native @scope and cascade layers now offer some of the same isolation without JavaScript.

Choosing for your project

There's no universal answer, but these questions help:

  1. What does native CSS already cover? Variables, nesting, layers, color mixing and container queries need no tools. Start there.
  2. Do you need a build step anyway? If your project already uses a bundler, adding PostCSS or Sass costs little. If it's a static site with one stylesheet, plain CSS may be all you need.
  3. Who maintains it? A team that knows Bootstrap will move faster with it. A team with a strong design system may prefer Tailwind or hand-written components.
  4. How custom is the design? Highly branded designs often fight component frameworks; utility frameworks or your own CSS fit better.
  5. Can you leave later? Utility classes and framework components spread through every template, so switching away is expensive. Native CSS never goes out of date.

A sensible modern default for many projects: plain CSS with custom properties, nesting and cascade layers, plus a bundler that combines files, adds prefixes and minifies. Add Sass for loops or string-built class names, a component framework when speed matters more than uniqueness, or Tailwind when your team likes working in markup.

Whatever you choose, the fundamentals in this course still apply. Every tool outputs CSS, every bug is eventually a CSS bug, and DevTools always shows the real cascade. Learning CSS well makes you better with every framework.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the key difference between a Sass variable and a CSS custom property?

    Sass variables vanish when the CSS is compiled. Custom properties inherit, respond to classes and media queries, and can be changed by JavaScript.
  2. 2. Which Sass feature has no native CSS equivalent today?

    Native CSS has nesting, custom properties and color-mix(), but no way to generate rules in a loop. That remains a build-time job.
  3. 3. What does Autoprefixer, a PostCSS plugin, do?

    You write standard properties, and Autoprefixer adds -webkit- or other prefixes only where your target browsers need them.
  4. 4. How does Tailwind keep its output CSS small?

    The build step finds every class in your files and outputs just those utilities and variants.
  5. 5. Which is a common drawback of component frameworks like Bootstrap?

    Bootstrap is customizable through variables, but heavily branded designs often end up overriding many of its styles.

Summary

  • Preprocessors (Sass/SCSS, Less) compile an extended language into CSS; PostCSS and Lightning CSS transform standard CSS with plugins; frameworks provide ready-made classes. All of them need a build step.
  • Sass variables are compile-time; custom properties are runtime and better for theming. Use Sass variables mainly where custom properties can't go, such as media query conditions.
  • Native CSS now covers nesting, math, color shades (color-mix(), relative colors) and layering; Sass still adds loops, string-built selectors, mixins and module bundling.
  • PostCSS with Autoprefixer or postcss-preset-env (or Lightning CSS) lets you write standard CSS and handles prefixes, older syntax and minification.
  • Bootstrap gives finished components for speed and consistency; Tailwind gives utilities composed in HTML, generated only for the classes you use.
  • Start with native CSS and add tools only for needs it can't meet; every tool still outputs CSS, so the fundamentals always matter.

Next: Resets and Base Styles, where you smooth out browser defaults and set up a solid foundation for every page.