Skip to content
Tutorials Center
My learning

Topics

Lesson 121 of 139 ยท Best Practices

CSS Frameworks: Bootstrap and Tailwind Concepts

Understand how component frameworks like Bootstrap and utility-first frameworks like Tailwind work under the hood by building tiny versions of both yourself.

The overview in Preprocessors and Frameworks told you what Bootstrap and Tailwind are. This lesson goes one level deeper: how do they actually work, what ideas do they share, and how do the trade-offs show up in daily work? The best way to understand a framework is to build a miniature one, so that is what you will do here.

You do not need to install anything. The framework code itself (CDN links, config files, build commands) appears as read-only samples because it needs a real project, while the live editors use plain CSS that behaves the way the frameworks' output does. Once you see that frameworks are "just CSS with a naming system", choosing and debugging them gets much easier.

What every CSS framework contains

Strip away the marketing and a framework is five things:

  1. A reset or base layer that evens out browser defaults (see Resets).
  2. Design tokens: a fixed scale of colors, spacing, font sizes and breakpoints, usually stored as custom properties or build-time variables.
  3. A layout system: a grid or container classes built on Flexbox or Grid.
  4. A naming convention: the class names you type in HTML.
  5. Extras: JavaScript for modals and dropdowns (Bootstrap), or a build tool that scans templates (Tailwind).

The two big families differ mainly in item 4. A component framework gives names to things (btn, card). A utility framework gives names to single declarations (p-4, bg-indigo-600).

flowchart LR
  T["Design tokens<br/>colors, spacing, type"] --> C["Component classes<br/>btn, card, navbar"]
  T --> U["Utility classes<br/>p-4, text-sm, flex"]
  C --> H1["HTML: few short class names"]
  U --> H2["HTML: many small class names"]

Both families start from the same tokens. They differ in how much meaning one class name carries.

Idea 1: component classes with variants

In a component framework, one base class carries the shared look, and modifier classes change it. Bootstrap's btn plus btn-primary is the classic example. The elegant modern version uses custom properties for the parts that vary, so each variant only sets a few values:

HTML
<button class="btn btn-primary">Save</button>
<button class="btn btn-outline-danger btn-sm">Delete</button>

You can build the same mechanism in a few lines of plain CSS:

A mini component framework with variants

Notice what happened: btn-outline changes the color variable before the background is reset, and btn-danger combines with it freely. This is roughly how Bootstrap 5.3 organizes its components, with --bs-btn-* variables on each component class. Because they are real custom properties, you can override them for one element without writing a new rule:

HTML
<button class="btn btn-primary" style="--bs-btn-bg: #0f766e; --bs-btn-border-color: #0f766e">
  Teal save
</button>

Idea 2: the 12-column grid and containers

Before CSS Grid existed, frameworks built page layouts out of rows and 12 columns. Bootstrap still has this system (row, col-md-6), now built on Flexbox. The column numbers say "take 6 of 12 parts of the row at the medium breakpoint":

HTML
<div class="container">
  <div class="row">
    <div class="col-12 col-md-6">Main</div>
    <div class="col-12 col-md-6">Side</div>
  </div>
</div>

In plain CSS, the same behavior is a few lines of Grid and a media query:

CSS
.row { display: grid; gap: 1rem; }
@media (width >= 768px) {
  .row { grid-template-columns: repeat(12, 1fr); }
  .col-md-6 { grid-column: span 6; }
}

That is why many teams now write layouts with native Grid even when they use a framework for components. The 12-column vocabulary is useful for communicating with designers, but it is no longer a technical necessity.

Idea 3: utility classes, one declaration each

A utility class does exactly one thing. p-4 is padding: 1rem. flex is display: flex. Responsive and state versions are generated by prefixing the name: md:p-8 means "padding 2rem, but only from the medium breakpoint up", and hover:bg-indigo-700 means "this background on hover". The framework writes those rules for you.

You can see the mechanism by writing a few by hand, including a responsive variant. The editor is wide enough for the two-column version; drag your browser window narrow (or use the device toolbar in DevTools) to see the single-column version:

A mini utility framework with a responsive variant

Tailwind's real class names look like md:grid-cols-2. The : in a class name must be escaped in CSS (.md\:grid-cols-2), which is a detail the framework handles for you. The editor above used md-cols-2 to keep the hand-written CSS readable.

How Tailwind keeps the CSS small

Tailwind would produce a gigantic stylesheet if it shipped every possible utility. Instead its build tool scans your HTML, templates and component files for class names, and generates CSS only for the ones it finds. In Tailwind v4 you configure it in CSS, and your tokens are custom properties:

CSS
/* app.css, processed by the Tailwind build step */
@import "tailwindcss";

@theme {
  --color-brand: #4f46e5;
  --font-display: "Fraunces", serif;
  --breakpoint-3xl: 120rem;
}

With that theme in place, classes such as bg-brand, font-display and 3xl:p-8 become available. Two practical consequences follow. First, class names must appear in your source as complete strings: a class built by joining pieces ("bg-" + color) cannot be found by the scanner. Second, the final CSS grows with the number of different utilities you use, not with the size of the project.

Reuse: where each family hurts

Every approach has a repeated-work problem:

  • Component frameworks make reuse easy (btn) but customization hard. When the design needs something slightly different, you override the framework's rules and end up fighting specificity. Bootstrap's own advice is to set its variables, not to write stronger selectors.
  • Utility frameworks make customization easy but reuse awkward. If the same 10-class button appears 40 times, a fix means editing 40 places, unless the markup is a template component (a partial, a Blade or React component) so the long class list lives in one file.

The fix for utilities is almost never a special CSS feature. It is your templating language. That is also why utility-first works best in component-based projects and less well in hand-copied static HTML.

Tailwind also has @apply, which pastes utilities into a CSS rule: .btn { @apply px-4 py-2 rounded-lg; }. It works, but its authors recommend using it sparingly, because it recreates the naming and specificity concerns utilities were meant to avoid.

Other frameworks you will meet

Bootstrap and Tailwind are the two giants, but you will run into smaller names in older projects, in company design systems and in tutorials. They all fit the two families you just learned, so you can place each one quickly:

Framework Family What makes it recognizable
Bulma Component CSS only, no JavaScript. Modifier classes read like words: button is-primary is-large, and a flexbox columns / column grid. Newer versions are built on custom properties.
Foundation Component Created by the agency ZURB as a more customizable rival to Bootstrap. Ships JavaScript plugins and a flexbox grid, written in Sass. Less common in new projects than it once was.
Pure.css Component Very small (a few kilobytes) and split into modules: grids, forms, buttons, menus, tables. You include only what you need.
Primer Both GitHub's design system. It has component classes (btn, Box) and many small utilities (mt-3, d-flex), plus React components for the same design.
Tachyons Utility "Functional CSS" before Tailwind made it popular. Tiny, short class names built on a fixed scale: pa3 (padding step 3), f4 (font size 4), bg-light-blue.
Materialize Component An implementation of Google's Material Design: cards with elevation, ripple effects, floating labels, plus JavaScript for modals and tabs, installed as CSS and JS files or built from Sass. The original project has been quiet for years and community forks exist, so check before adopting it.
Semantic UI (and its community fork Fomantic UI) Component Class names read like English phrases: ui large primary button, ui three column grid. It ships a lot of components and a jQuery-based behavior layer. The community fork is the actively maintained one.
Spectre.css Component A small, flexbox-based framework with a tidy set of elements, components and utilities, and no JavaScript. Its development has slowed, so it suits small, stable projects.
Milligram Classless-leaning A minimalist kit (around 2 KB compressed) that styles typography, forms, tables, buttons and a simple row / column grid. Good when you want a clean default look and almost no framework.
Blaze UI Component A modular, pure-CSS toolkit whose class names carry a prefix for their role (components, objects, utilities), in the spirit of ITCSS, so you can see what a class is for from its name.
Onsen UI Component Aimed at hybrid mobile apps rather than websites: custom elements such as <ons-page> and <ons-button> that adopt an iOS or Material look automatically, usable with plain JavaScript, Angular, React or Vue, commonly packaged with Cordova.

A useful way to compare them is by how a class name is built. Component kits name the thing and its variant, utility kits name one declaration on a scale. The editor below uses plain CSS written in each style (these are stand-ins that imitate the naming, not the real frameworks) to style the same button:

The same button in three naming styles

There is one more style worth knowing: the classless kit, used by Milligram-like and Pico-like libraries. You write ordinary semantic HTML and the stylesheet targets elements, so there is nothing to memorize. The price is that you get one look, with limited room for variants:

A classless mini framework

The markup has no framework classes at all, only the wrapper that scopes the example. That is the appeal and the limit: a page of articles, forms and tables looks finished in minutes, but a design with many different button styles needs classes anyway.

Do not pick a framework by name recognition. Check three things: whether it is still maintained, whether it fits your build setup (some need Sass, some need none), and whether you can customize it through variables or tokens instead of overrides.

Where utility-first frameworks struggle

Tailwind is popular for good reasons, but it is not free of costs. Know them before you adopt it:

  • Long class lists. One element can carry fifteen classes. That is manageable with template components and painful in hand-copied HTML.
  • A learning curve at the start. You must learn the scale (p-4, gap-6), the variant prefixes (md:, hover:, dark:) and where to look things up. New learners often find it harder than copying a Bootstrap component.
  • A build step. The framework scans your templates to generate only the classes you use, so a project needs its tooling configured (or a ready-made compiled stylesheet, which loses the main benefit).
  • No ready-made behavior. There is no modal, dropdown or carousel script; you write it or add a separate component library.
  • Repetition and drift. Without discipline, the same combination appears in many places in slightly different forms. Extracting components, or using @apply in moderation, is how teams solve this.
  • Markup tied to styling. Changing a visual detail means editing HTML, which is a downside when design and content are handled by different people.

None of these is a reason to avoid it. They are reasons to decide on purpose.

Trade-offs side by side

Question Component framework (Bootstrap) Utility framework (Tailwind)
Speed for a standard UI Very fast, ready-made parts Fast once you know the names
HTML readability Short class lists Long class lists
Unique, branded design Needs overrides Natural fit
JavaScript parts (modal, dropdown) Included Not included; bring your own
CSS size Large unless trimmed Small, grows with distinct utilities
Learning curve Learn components and their markup Learn the scale and variant names
Needs a build step Optional (CDN works) Yes (or a compiled copy)
Leaving later Replace markup patterns Replace every class

Neither column is "better". Look at your constraints: a small admin panel with a deadline suits Bootstrap; a custom marketing site built from template components suits Tailwind; a small static site may need neither.

Reading the framework as plain CSS

Whenever a framework class puzzles you, open DevTools (see Browser DevTools) and look at the Styles panel. You will always find ordinary declarations, and you can read them with everything you learned in this course. A few habits make life easier:

  • Check specificity and cascade layers when your own CSS does not win. Tailwind v4 puts its output in layers, so unlayered CSS of your own beats it.
  • Prefer overriding variables to overriding rules.
  • Do not load a whole framework for one component. Unused CSS slows down performance.
  • Keep your own tokens. If your brand color is a custom property, you can wire it into either framework's theme.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between a component framework and a utility framework?

    btn carries a whole look, while p-4 is just padding: 1rem. That difference drives most of the trade-offs.
  2. 2. Why does Tailwind's scanner miss a class that your code builds by joining two strings?

    The scanner does not run your code. It matches complete strings, so partial names never appear in the generated CSS.
  3. 3. In the mini component framework, why do variants such as btn-danger only set custom properties?

    Variants change variables like --btn-bg, and the base rule reads them. This keeps specificity low and variants composable.
  4. 4. Your team repeats a 10-class button in 40 places with a utility framework. What is the usual fix?

    Reuse in utility-first projects comes from your template or component system, not from CSS.

Summary

  • A framework is a reset, a set of design tokens, a layout system, a naming convention and sometimes JavaScript.
  • Component frameworks name whole parts; variants work best when they only change custom properties.
  • Utility frameworks name single declarations; responsive and state prefixes are generated media queries and pseudo-classes.
  • Tailwind generates only the utilities it finds as complete strings in your source files.
  • Utilities need template components for reuse; component frameworks need variable overrides for customization.
  • Everything a framework outputs is ordinary CSS, so DevTools, specificity and layers still explain every result.

Next: Sass in Practice.