CSS Tutorial
20 Topics- CSS Tutorial Overview
- CSS Introduction
- CSS Fundamentals
- CSS Selectors
- CSS Colors & Backgrounds
- CSS Text & Fonts
- CSS Box Model
- CSS Display & Positioning
- CSS Flexbox
- CSS Grid
- CSS Sizing & Layout
- CSS Lists & Tables
- CSS Transitions & Animations
- CSS Responsive Design
- CSS Advanced Styling
- CSS Functions
- CSS for Accessibility & Print
- CSS Architecture & Methodologies
- CSS Preprocessors & Tools
- CSS Modern & Experimental CSS
CSS Architecture, Methodologies & Scaling
Conquer global namespace collisions, eliminate specificity wars, and engineer maintainable design systems using BEM, OOCSS, SMACSS, and CSS Modules.
1. The Three Crises of Large-Scale CSS
As web projects grow from small prototypes into enterprise codebases with dozens of developers, unarchitected CSS rapidly deteriorates into three critical bottlenecks:
A. Global Namespace Pollution
All standard CSS selectors share a single global scope. A selector like .title or .card in a header stylesheet inadvertently mutates components across the entire application.
B. Specificity Inflation
When developers cannot override an inherited style, they resort to deeper selectors (body div.page .card h3), ID selectors, or !important. This creates cascading specificity wars.
C. Dead Code Accumulation
Because nobody knows which pages depend on an old selector, teams are terrified to delete CSS rules. Stylesheets perpetually grow in file size, slowing download times.
2. BEM: Block, Element, Modifier
Developed at Yandex, BEM is the most widely adopted CSS naming methodology in modern software engineering. It enforces a flat specificity hierarchy:
| Component | Separator | Definition | Example |
|---|---|---|---|
| Block | None | Standalone, reusable entity with standalone semantic meaning. | .card, .navbar, .btn |
| Element | Double Underscore __ | Part of a block that has no standalone meaning outside the block. | .card__title, .navbar__item, .btn__icon |
| Modifier | Double Hyphen -- | A flag that changes appearance, state, or behavior. | .btn--primary, .card--highlighted, .is-active |
CSS Example
/* Strict BEM implementation: Flat (0, 0, 1, 0) Specificity */
.product-card {
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 16px;
background: #ffffff;
max-width: 320px;
}
.product-card__title {
font-size: 1.25rem;
font-weight: 700;
color: #1e293b;
margin-top: 0;
}
.product-card__desc {
color: #64748b;
font-size: 0.95rem;
line-height: 1.5;
}
.product-card--featured {
border-color: #3b82f6;
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.15);
}3. OOCSS: Object-Oriented CSS
Proposed by Nicole Sullivan, OOCSS applies software engineering principles of abstraction and code reuse to styling through two fundamental rules:
- Separate Structure from Skin: Keep structural layout properties (width, padding, margins, display) in base classes, and visual themes (colors, borders, gradients) in skin classes.
- Separate Container from Content: Never style an element based on its parent container (e.g. avoid
.sidebar h3). Instead, give the heading a reusable class (.heading-subtle) so it looks identical wherever it is placed.
The Classic Media Object Pattern:
CSS Example
/* The OOCSS Media Object: Structure */
.media-object {
display: flex;
align-items: flex-start;
gap: 16px;
padding: 16px;
background: #f8fafc;
border-radius: 8px;
}
.media-object-badge {
width: 48px;
height: 48px;
border-radius: 50%;
background: #3b82f6;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
flex-shrink: 0;
}
.media-object-body {
flex: 1;
}4. SMACSS & Utility-First Approaches
SMACSS (Scalable and Modular Architecture for CSS) categorizes rules into five distinct buckets:
- Base: Element defaults (
html,body,h1-h6,a) with no classes. - Layout: Major page regions prefixed with
l-(e.g..l-header,.l-grid). - Module: Self-contained components (e.g.
.card,.modal). - State: Dynamic visual states prefixed with
is-orhas-(e.g..is-active,.is-collapsed). - Theme: Visual styling overrides for multi-tenant or dark mode apps.
Utility-First CSS (Tailwind):
Instead of writing semantic CSS classes, utility-first CSS composes single-purpose atomic classes directly in markup (e.g. flex p-4 bg-white rounded-lg shadow-md).
Advantages: Eliminates CSS authoring entirely; zero specificity wars; production bundle size plateaus because no new CSS is written for new components.
5. Frequently Asked Questions (FAQ)
.card--active) and relationships (Elements like .card__header) within the module file..card { &__title { color: red; } }. While this compiles to the flat selector .card__title, nesting actual descendant rules like .card { .card__title { ... } } increases specificity to (0,0,2,0) and defeats BEM's primary goal of flat specificity.style="..." attributes cannot use media queries (no responsive design), cannot use pseudo-classes (like :hover or :focus), have maximum specificity (1,0,0,0), and cannot be cached. Utility-first classes are standard CSS classes that support media queries, pseudo-states, and design tokens cleanly.Explore Related Tools
Hand-picked utilities and calculators related to this tool.
