CSS Frameworks Overview
In this page:
What are CSS Frameworks?
CSS Frameworks are pre-written libraries of CSS classes, grids, and components designed to speed up development times and establish consistent styling across your project.
Example: What are CSS Frameworks?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<style>
.container { background: #e7f1ff; border: 2px solid #0d6efd; padding: 1rem; }
</style>
</head>
<body>
<div class="container">Pre-written classes, grids, and components</div>
</body>
</html>
Utility-First Architecture
Utility-first frameworks (like Tailwind CSS) provide libraries of tiny, single-purpose classes (like p-4 for padding or text-center for centering) that allow you to style elements directly inside your HTML markup.
Example: Utility-First Architecture
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { font-family: sans-serif; }
</style>
</head>
<body>
<div class="p-4 text-center bg-blue-100 border-2 border-blue-500">Tiny single-purpose classes</div>
</body>
</html>
Component-Driven Architecture
Component-driven frameworks (like Bootstrap or Bulma) provide pre-written classes for complete UI components (like navigation bars, cards, modals, or forms), allowing you to build standard pages in minutes.
Example: Component-Driven Architecture
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<style>
body { padding: 1rem; }
</style>
</head>
<body>
<nav class="navbar navbar-light bg-light border">Pre-written navbar component</nav>
</body>
</html>
Customizing Default Theme Configurations
To keep your website from looking identical to others, most frameworks allow you to customize their default theme configurations (like colors, fonts, or breakpoints) using design variables.
Example: Customizing Default Theme Configurations
<style>
:root {
--bs-primary: #ff5733;
}
</style>
<button class="btn btn-primary">Themed button</button>
Optimizing and Purging Unused CSS
Because CSS frameworks provide libraries of thousands of classes, their file sizes can be very large. To keep your website fast and optimized, you should use optimization tools (like PurgeCSS) to scan your HTML and automatically strip out any unused classes before publishing.
Example: Optimizing and Purging Unused CSS
<style>
.btn { background: royalblue; color: white; padding: 10px 20px; border: 0; }
/* .legacy-banner { ... } is never used in the HTML: PurgeCSS would strip it */
/* PurgeCSS scans your HTML and strips out any unused classes
from the compiled framework CSS before you publish */
</style>
<button class="btn">Used class kept</button>
- Using CSS frameworks without customizing their colors and layouts, making your site look generic.
- Failing to purge unused classes from your final files, slowing down page load times with bloated stylesheets.
- Attempting to override framework component classes without using sufficiently specific CSS selectors.
- CSS Frameworks provide pre-written libraries of style classes, grids, and UI components to speed up development times.
- Choose utility-first frameworks (like Tailwind) for custom designs, and component-driven (like Bootstrap) for fast, standard layouts.
- Customize theme variables to match your brand identity, and purge unused classes before publishing to keep your stylesheets lightweight and fast.
Compiled framework stylesheets are supported natively by all modern web browsers.
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep