CSS
Cascade, box model, Flexbox, Grid, responsive design, animations, and modern CSS architecture. · 32 lessons · click a card to expand
Cascade & Specificity
4 items01How the Cascade Resolves Conflicts
- Origin — Author styles (your stylesheet) beat user-agent defaults. Inline styles and
!importantflip the order in special ways. - Importance — Normal declarations lose to
!importantdeclarations within the same origin. - Specificity — More specific selectors win (covered in Lesson 2).
- Source order — If specificity is equal, the rule that appears later in the CSS wins.
Understanding this prevents the classic frustration of "I changed the color but nothing happened." Often a more specific rule elsewhere is winning.
/* Both match <p class="intro">, but .intro wins (higher specificity) */
p {
color: gray;
}
.intro {
color: navy;
}
/* Same specificity — later rule wins */
.card {
padding: 1rem;
}
.card {
padding: 1.5rem; /* this padding applies */
}When debugging, use DevTools Computed tab to see which rule won and which were overridden (shown with strikethrough).
<p class="intro">Hello</p>. Write two color rules as above, then add a third rule p.intro { color: crimson; } and predict the final color before refreshing.02Specificity Scored Like a Game
(inline, IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). Compare left to right; never add the numbers as plain integers.| Column | Examples | Weight |
|---|---|---|
| Inline styles | style="color: red" | 1,0,0,0 |
| IDs | #header | 0,1,0,0 |
Classes, attributes, :hover, :nth-child() | .btn, [type="text"], :focus | 0,0,1,0 |
Elements, ::before, ::after | div, p, ::before | 0,0,0,1 |
/* 0,0,1,1 — one class + one element */
nav a.active {
color: white;
}
/* 0,0,2,0 — two classes, beats nav a.active */
.sidebar .active {
color: lime;
}
/* 0,1,0,0 — one ID beats any number of classes */
#logo {
width: 120px;
}Never write selectors like #nav .menu ul li a.link just to "force" a win. High specificity makes future overrides painful and leads to !important wars.
The universal selector *, combinators (>, +, ~), and pseudo-elements like ::placeholder add zero specificity.
.card__title) beats long chained selectors and keeps the cascade predictable.#app .btn, .btn.primary, and button.btn, rank them by specificity and determine which color applies to <button class="btn primary" id="app">.03Inheritance — What Flows Down the Tree
inherit, initial, unset, and revert deliberately.Commonly inherited: color, font-family, font-size, line-height, text-align, visibility, cursor.
Not inherited: margin, padding, border, background, width, height, display, position.
body {
font-family: system-ui, sans-serif;
color: #222;
line-height: 1.6;
}
/* Links don't inherit color by default in most browsers — set explicitly */
a {
color: inherit;
}
.card {
/* Reset margin that headings might carry */
margin: 0;
}
.reset-input {
font: inherit; /* shorthand inherits all font-* properties */
color: inherit;
}Keyword values:
inherit— force inheritance even when property normally wouldn't.initial— spec default (often not the same as browser default).unset— inherit if inheritable, else initial.revert— roll back to user-agent stylesheet default.
body or :root once; components inherit a consistent base instead of re-declaring font-family everywhere.<span> inside a <div style="color: blue">. Set span { border: 1px solid currentColor; } and observe how currentColor uses the inherited text color for the border.04Specificity Wars and When !important Backfires
!important breaks normal cascade order and creates maintenance debt. Reach for it only for utility overrides or third-party CSS you cannot edit — never as a default tool.!important to a declaration makes it win over normal declarations, even with lower specificity — unless another !important with higher specificity also exists..button {
background: steelblue !important; /* wins over .sidebar .button */
}
.sidebar .button {
background: coral; /* ignored */
}
/* Utility escape hatch — common in design systems */
.u-hidden {
display: none !important;
}Problems with overusing !important:
- Every future override also needs
!important. - Specificity stops mattering, so source order becomes the only lever — chaos in large codebases.
- DevTools debugging becomes harder.
Better strategies before !important:
- Increase selector appropriateness, not length: target
.btn--dangerinstead of#page .content div .btn. - Restructure HTML to avoid fighting existing rules.
- Use cascade layers (
@layer, Lesson 28) for intentional ordering.
.is-active or [data-state="open"] modifier class keeps specificity flat and avoids !important.!important, one with an ID selector. Remove !important and add a modifier class instead; confirm both approaches work but note which is easier to override later.Box Model
3 items05Content, Padding, Border, and Margin
┌──────── margin ────────┐
│ ┌──── border ────────┐ │
│ │ ┌── padding ─────┐ │ │
│ │ │ content │ │ │
│ │ └────────────────┘ │ │
│ └────────────────────┘ │
└────────────────────────┘.box {
width: 200px; /* content width (default box-sizing) */
height: 100px;
padding: 16px; /* space inside the border */
border: 2px solid #333;
margin: 24px; /* space outside the border, between siblings */
background: #eef;
}
/* Margin collapse: vertical margins between block siblings merge
to the larger value, not the sum */
.block-a { margin-bottom: 30px; }
.block-b { margin-top: 20px; }
/* Gap between them is 30px, not 50px */width and height by default apply to the content box only — padding and border add to the total rendered size (see Lesson 6).
<div class="box"> elements with different margins and borders. Measure total height in DevTools with default vs border-box (next lesson).06box-sizing: border-box Changes Everything
box-sizing: border-box makes width/height include padding and border, which makes layout math intuitive. Set it globally.content-box means width: 300px plus padding and border expands the element beyond 300px. border-box includes padding and border inside the declared width.*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 4px solid #ccc;
/* Total outer width stays 300px with border-box */
}
/* Without border-box, total width = 300 + 40 + 8 = 348px */
.old-card {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 4px solid #ccc;
}This is especially critical in grid/flex layouts where children with percentage widths and padding would otherwise overflow.
margin: 0 on body if you want a clean slate (many reset/normalize libraries do this).padding: 20px — one with content-box, one with border-box. Observe which pair fits on one row without wrapping.07Block, Inline, and Inline-Block Display Types
display | Behavior |
|---|---|
block | New line, full width available, respects width/height/margin |
inline | Flows with text, no width/height, horizontal margin/padding only |
inline-block | Sits in text flow but accepts box dimensions |
none | Removed from layout entirely |
span.blocky {
display: block;
background: #ffd;
margin: 8px 0;
padding: 8px;
}
a.buttonish {
display: inline-block;
padding: 0.5em 1em;
background: #2563eb;
color: white;
text-decoration: none;
border-radius: 4px;
}
/* Modern alternative to inline-block for nav items: flex on parent */
nav {
display: flex;
gap: 1rem;
}Inline elements also introduce subtle spacing from whitespace in HTML between tags — flex/grid or setting font-size: 0 on the parent (hacky) can eliminate gaps between inline-blocks.
inline-block for whole-page layout anymore — flex and grid (Lessons 10–19) replace most historical inline-block patterns.<span> elements as inline, then inline-block with fixed width/height, then block. Note which properties (margin-top, width) take effect in each mode.Units & Typography
2 items08CSS Units — When to Use px, rem, em, %, and Viewport Units
rem for scalable typography and spacing; use % and fr for fluid layouts; reserve px for hairlines and precise borders; use viewport units sparingly for hero typography.:root {
font-size: 100%; /* usually 16px in browsers */
--space-md: 1rem; /* 16px if root is 16px */
}
html {
font-size: 16px; /* explicit root for rem math */
}
body {
font-size: 1rem; /* 16px — scales if user changes root */
padding: 1.5rem;
}
.hero-title {
font-size: clamp(2rem, 5vw + 1rem, 4rem);
/* fluid between 32px and 64px */
}
.container {
width: min(100% - 2rem, 1200px); /* modern centered container */
margin-inline: auto;
}
.sidebar {
width: 25%; /* relative to parent, not viewport */
}
.icon {
width: 24px; /* fixed pixel art / borders often stay px */
border: 1px solid #ccc;
}rem — relative to root font size; consistent across nested components.
em — relative to current element's font size; useful for padding proportional to text (padding: 0.5em 1em on buttons).
vw/vh — 1vw = 1% of viewport width; watch out for mobile browser chrome causing 100vh bugs (use dvh where supported).
clamp(min, preferred, max) for responsive type without dozens of media queries.html { font-size: 62.5%; } so 1rem = 10px mentally, then build spacing scale --space-1: 0.8rem through --space-6: 3.2rem.09Typography — Readable, Consistent, Accessible Type
:root {
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", ui-monospace, monospace;
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.125rem;
--text-xl: 1.5rem;
--text-2xl: 2rem;
--leading-tight: 1.25;
--leading-normal: 1.6;
--leading-loose: 1.75;
}
body {
font-family: var(--font-sans);
font-size: var(--text-base);
line-height: var(--leading-normal);
color: #1a1a1a;
}
h1, h2, h3 {
line-height: var(--leading-tight);
font-weight: 700;
text-wrap: balance; /* avoids orphaned words in headings */
}
.prose {
max-width: 65ch; /* character-based measure */
}
code, pre {
font-family: var(--font-mono);
font-size: 0.9em;
}
/* OpenType features when available */
body {
font-feature-settings: "kern" 1, "liga" 1;
-webkit-font-smoothing: antialiased;
}Pair heading sizes using a ratio (e.g., 1.25 major third): each level ~1.25× the previous. Use font-weight and letter-spacing for labels (0.05em uppercase microcopy).
.prose article with h1, paragraphs, and code. Set max-width: 65ch and toggle line-height between 1.4 and 1.8 to feel the readability difference.Flexbox Complete
5 items10Flex Container Fundamentals
display: flex on a parent turns direct children into flex items laid out along a main axis; everything else stays normal block flow..toolbar {
display: flex;
/* main axis defaults to horizontal (row) */
/* cross axis is perpendicular (vertical in row mode) */
}
.toolbar > * {
/* each direct child becomes a flex item */
}
/* Minimal centered layout — extremely common pattern */
.center-screen {
display: flex;
justify-content: center; /* main axis */
align-items: center; /* cross axis */
min-height: 100vh;
}Only direct children participate. Nested elements are unaffected unless their parent is also a flex container.
Flex items default to flex-shrink: 1 (they can shrink below content size) and flex-grow: 0 (they won't expand to fill extra space).
gap: 1rem instead of margin hacks between flex children — gap works in flex and grid in modern browsers.display: flex and gap only — no floats.11flex-direction and flex-wrap
flex-direction chooses the main axis; flex-wrap controls whether items stay on one line or wrap to new flex lines..stack {
display: flex;
flex-direction: column; /* main axis top → bottom */
gap: 1rem;
}
.row-wrap {
display: flex;
flex-direction: row; /* default: left → right in LTR */
flex-wrap: wrap; /* items wrap to next line when needed */
gap: 1rem;
}
/* Shorthand */
.flex-row-wrap {
display: flex;
flex-flow: row wrap; /* direction + wrap */
}
/* Reverse variants flip start/end of main axis */
.rtl-row {
flex-direction: row-reverse;
}When items wrap, each line is its own flex line with independent cross-axis alignment. align-content (Lesson 12) controls spacing between wrapped lines.
Without flex-wrap: wrap, shrinking items (flex-shrink: 1) may crush content rather than wrapping — set flex-wrap: wrap for responsive component rows.
flex-direction: column by default, switch to row in a media query for wider screens..tag items in a flex row with flex-wrap: wrap and fixed gap. Resize the browser and count how flex lines form.12justify-content and align-items — Axis Alignment
justify-content aligns items along the main axis; align-items aligns along the cross axis. Confusing them is the #1 flexbox mistake..card-row {
display: flex;
justify-content: space-between; /* main: spread items apart */
align-items: center; /* cross: vertically center in row mode */
min-height: 80px;
padding: 1rem;
border: 1px solid #ddd;
}
/* justify-content values */
.space-demo {
display: flex;
justify-content: flex-start; /* default */
/* flex-end | center | space-between | space-around | space-evenly */
}
/* align-items values */
.cross-demo {
display: flex;
align-items: stretch; /* default — items fill cross size */
/* flex-start | flex-end | center | baseline */
}
/* Per-item override */
.tall-item {
align-self: flex-end; /* overrides align-items for one item */
}For wrapped flex containers, align-content distributes lines along the cross axis (e.g., align-content: center when extra vertical space exists).
Memory aid: justify = main axis (think "justifying text" horizontally in a row); align = cross axis.
margin-left: auto on a flex item pushes it (and following items) to the far end — handy for "logo left, nav right" without space-between.justify-content: center, then push nav right using margin-left: auto on the nav element.13flex-grow, flex-shrink, and flex-basis
flex shorthand controls how items grow to fill space, shrink when cramped, and their starting size before free space is distributed..sidebar-layout {
display: flex;
gap: 1rem;
}
.sidebar {
flex: 0 0 240px; /* grow shrink basis — fixed 240px sidebar */
/* don't grow, don't shrink, start at 240px */
}
.main {
flex: 1 1 auto; /* grow to fill remaining space */
/* equivalent to flex: 1 in most cases */
}
.equal-columns {
display: flex;
}
.equal-columns > * {
flex: 1; /* shorthand for 1 1 0% — equal width columns */
}
/* Prevent crushing: shrink-0 on icons or labels */
.icon {
flex-shrink: 0;
}flex-basis is the hypothetical starting size before free space distribution. flex: 1 sets basis to 0%, so all items share space equally regardless of content width.
/* Explicit longhand when debugging */
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
}min-width: 0 on flex children that contain text — otherwise default min-width: auto prevents shrinking and breaks overflow ellipsis.flex: 0 0 200px and main to flex: 1. Narrow the viewport and confirm sidebar stays 200px until wrap (if you add flex-wrap).14Practical Flexbox Patterns — Holy Grail, Cards, and Forms
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-main {
flex: 1; /* absorbs extra vertical space */
}Equal-height cards in a row:
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 280px; /* at least 280px, grow equally */
display: flex;
flex-direction: column;
}
.card-body {
flex: 1; /* pushes footer/button to bottom of card */
}Inline form row:
.form-row {
display: flex;
flex-wrap: wrap;
gap: 1rem;
align-items: flex-end;
}
.form-row label {
display: flex;
flex-direction: column;
flex: 1 1 200px;
}Grid Complete
5 items15Grid Container Fundamentals
display: grid creates a two-dimensional layout of rows and columns simultaneously; direct children become grid items placed by explicit or implicit tracks..grid {
display: grid;
/* items flow row-by-row into auto-generated columns by default */
}
/* Explicit tracks */
.simple-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto 200px auto;
gap: 16px;
}
.grid-item {
/* default: spans 1 column, 1 row */
background: #e0f2fe;
padding: 1rem;
}Grid introduces powerful concepts:
- Tracks — rows and columns (explicit via template or implicit via auto-placement).
- Gap — gutters between tracks (replaces old grid hacks with margins).
- fr unit — fractional free space (like flex-grow for tracks).
.auto-fit-cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}One line produces a fully responsive card grid without media queries.
display: grid for page layouts and card matrices; use flex for toolbars and one-dimensional component internals.gap: 8px. Toggle grid-template-columns between repeat(3, 1fr) and repeat(auto-fit, minmax(100px, 1fr)) while resizing.16grid-template-columns and grid-template-rows
.layout {
display: grid;
grid-template-columns: 240px 1fr 240px; /* holy grail columns */
grid-template-rows: auto 1fr auto;
min-height: 100vh;
gap: 0;
}
.dashboard {
display: grid;
grid-template-columns: repeat(12, 1fr); /* 12-column system */
gap: 1rem;
}
.span-8 {
grid-column: span 8;
}
.span-4 {
grid-column: span 4;
}
/* minmax prevents tracks from collapsing too small */
.responsive {
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
/* Named line syntax (advanced) */
.complex {
grid-template-columns:
[sidebar-start] 200px
[sidebar-end main-start] 1fr
[main-end];
}auto-fill vs auto-fit: auto-fill keeps empty tracks; auto-fit collapses empty tracks so items stretch to fill the row.
col-span-8 = grid-column: span 8).17grid-template-areas for Semantic Layouts
grid-template-areas and assign items via grid-area for readable, refactor-friendly page layouts..site {
display: grid;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
gap: 1rem;
}
.site-header { grid-area: header; }
.site-nav { grid-area: nav; }
.site-main { grid-area: main; }
.site-aside { grid-area: aside; }
.site-footer { grid-area: footer; }
/* Responsive: stack on small screens */
@media (max-width: 768px) {
.site {
grid-template-areas:
"header"
"nav"
"main"
"aside"
"footer";
grid-template-columns: 1fr;
}
}Rules for grid-template-areas:
- Each row must have the same number of cells.
- Each named area must be rectangular (no L-shapes).
- Use
.for empty cells.
grid-column values.18Gap, Alignment, and Justification in Grid
gap sets track spacing; justify-items/align-items position items inside cells; justify-content/align-content position the whole grid when tracks don't fill the container..gallery {
display: grid;
grid-template-columns: repeat(3, 150px);
gap: 1rem 2rem; /* row-gap column-gap */
justify-content: center; /* entire grid block horizontal */
align-content: center; /* entire grid block vertical */
min-height: 400px;
}
.gallery-item {
justify-self: stretch; /* default — fill cell width */
align-self: center; /* vertical within cell */
}
/* Place single item precisely */
.featured {
grid-column: 2 / 4; /* lines 2 through 4 (spans 2 columns) */
grid-row: 1 / 3;
}Line-based placement uses grid lines (1-indexed): grid-column: 1 / 3 spans from line 1 to line 3 (two columns).
Shorthand: place-items: center (= align + justify items), place-content: center (= align + justify content).
justify-items: center with fixed-size tracks centers each item in its cell — different from centering the whole grid with justify-content.justify-content: center.19Practical Grid Patterns — Dashboard, Gallery, and Subgrid
subgrid (where supported) to align nested content with parent tracks..photos {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 4px;
}
.photos img {
width: 100%;
aspect-ratio: 1;
object-fit: cover;
display: block;
}Dashboard with dense packing:
.widgets {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1.5rem;
grid-auto-flow: dense; /* fills holes with smaller items */
}
.widget-wide {
grid-column: span 2;
}Subgrid — child grid inherits parent tracks:
.card-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3; /* title, body, footer align across cards */
}Subgrid solves equal-height card internals without flex hacks. Check caniuse.com for browser support in your target audience.
aspect-ratio pairs beautifully with grid — uniform thumbnails without fixed heights..widget-wide spanning two columns and enable grid-auto-flow: dense to see gap-filling behavior.Positioning & Stacking
2 items20static, relative, absolute, fixed, and sticky
position removes elements from normal flow (except relative) and anchors them to containing blocks; sticky hybrid sticks within its scroll container./* static — default, normal flow */
.box { position: static; }
/* relative — offset from normal position, still occupies original space */
.badge-parent {
position: relative;
}
.badge {
position: relative;
top: -8px;
left: 4px;
}
/* absolute — removed from flow, positioned vs nearest positioned ancestor */
.dropdown {
position: absolute;
top: 100%;
left: 0;
z-index: 10;
}
.icon-btn {
position: relative; /* establishes containing block for .dropdown */
}
/* fixed — relative to viewport, removed from flow */
.modal-backdrop {
position: fixed;
inset: 0; /* top/right/bottom/left: 0 */
background: rgba(0, 0, 0, 0.5);
}
/* sticky — toggles between relative and fixed */
.sticky-header {
position: sticky;
top: 0;
background: white;
z-index: 100;
}Key rules:
- Absolutely positioned elements use the nearest ancestor with
positionnotstatic(or the viewport). stickyrequires a scrolling ancestor and atop/bottomthreshold; parentoverflow: hiddenbreaks it.inset: 0is shorthand for pinning all edges.
position: relative on the direct parent of an absolutely positioned dropdown — not <body> unless intentional.position: relative and a "Sale" badge in the top-right corner using position: absolute; top: 8px; right: 8px.21z-index and Stacking Contexts
z-index only compares elements within the same stacking context; new contexts are created by position + z-index, opacity < 1, transform, filter, and more.z-index: 9999 and wonder why it still hides behind something. The issue is almost always stacking contexts — isolated layers..modal {
position: fixed;
z-index: 1000;
/* creates its own stacking context */
}
.dropdown {
position: absolute;
z-index: 50; /* only competes within parent context */
}
.sidebar {
position: relative;
z-index: 1; /* creates context — traps children inside */
}
/* Common context creators */
.context-trap {
transform: translateZ(0); /* or any transform */
opacity: 0.99;
filter: blur(0);
isolation: isolate; /* explicit new context */
}Stacking order (simplified, same context):
- Background/border of stacking context root
- Negative z-index children
- Block-level in-flow content
- Floats
- In-flow inline content
- z-index: auto / 0 positioned elements
- Positive z-index positioned elements
--z-dropdown: 100, --z-modal: 200, --z-toast: 300) instead of arbitrary large numbers.z-index: 1. Give dropdown z-index: 9999 and watch it still clip behind a sibling modal — then fix by raising sidebar's context or portal the dropdown to body.Responsive Patterns
2 items22Media Queries and Mobile-First Design
min-width media queries progressively enhance for larger viewports./* Base: mobile */
.container {
padding: 1rem;
}
.nav {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
.nav {
flex-direction: row;
justify-content: space-between;
}
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop */
@media (min-width: 1024px) {
.grid {
grid-template-columns: repeat(3, 1fr);
}
}
/* Respect user preferences */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}Common breakpoints (guidelines, not gospel): 640px, 768px, 1024px, 1280px. Prefer content-based breakpoints — resize until layout breaks, then add a query.
Modern alternatives: container queries (Lesson 27) for component-level responsiveness; clamp() for fluid spacing without queries.
min-width (mobile-first), not max-width (desktop-first), unless maintaining legacy desktop-first code.min-width queries.23Responsive Images, Fluid Type, and Container Width
max-width: 100%, srcset/sizes in HTML, object-fit, and fluid typography so content adapts without horizontal scroll or blurry images.img, video {
max-width: 100%;
height: auto;
display: block;
}
.hero-img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover; /* crop to fill box */
}
.avatar {
width: 48px;
height: 48px;
object-fit: cover;
border-radius: 50%;
}
/* Fluid type without breakpoints */
h1 {
font-size: clamp(1.75rem, 4vw, 3rem);
}
/* Prevent overflow from long URLs */
.prose {
overflow-wrap: break-word;
word-break: break-word;
}
/* Safe area for notched phones */
.footer {
padding-bottom: env(safe-area-inset-bottom);
}HTML side (pair with CSS):
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
sizes="(min-width: 768px) 50vw, 100vw"
alt="Description"
/>sizes tells the browser how wide the image renders at each viewport — critical for picking the right srcset candidate.
width and height attributes on <img> (or aspect-ratio in CSS) to prevent cumulative layout shift (CLS).max-width: 100%. Add aspect-ratio: 16/9 and object-fit: cover and compare layout stability with and without dimensions.Transitions & Animations
2 items24Transitions — Smooth Property Changes
transition animates property changes between two states over time; always specify what triggers the change (:hover, class toggle, JS)..button {
background: #2563eb;
color: white;
padding: 0.75em 1.5em;
border: none;
border-radius: 6px;
transition: background 0.2s ease, transform 0.15s ease;
}
.button:hover {
background: #1d4ed8;
transform: translateY(-2px);
}
.button:active {
transform: translateY(0);
}
.card {
transition: box-shadow 0.3s ease, opacity 0.3s ease;
}
.card:hover {
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
/* Transition only animatable properties */
.panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s ease;
}
.panel.is-open {
max-height: 500px; /* approximate — see grid-template-rows trick for smooth auto height */
}Syntax: transition: property duration timing-function delay.
Timing functions: ease, linear, ease-in-out, or cubic-bezier(0.4, 0, 0.2, 1).
transform and opacity — they are GPU-friendly and don't trigger layout reflow. Avoid transitioning width/height on large pages.translateY) and shadow transition. Toggle a class with JS to expand a panel using max-height transition.25@keyframes Animations — Loops, Choreography, and Performance
@keyframes define multi-step animations independent of user interaction; use animation shorthand for repeats, direction, and fill modes.@keyframes fade-in-up {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.toast {
animation: fade-in-up 0.4s ease both;
/* both = forwards + backwards fill mode */
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.skeleton {
background: #e5e7eb;
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
width: 24px;
height: 24px;
border: 3px solid #ccc;
border-top-color: #2563eb;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
/* Pause off-screen animations for performance */
@media (prefers-reduced-motion: reduce) {
.spinner {
animation: none;
border-top-color: #2563eb;
}
}animation properties: name, duration, timing-function, delay, iteration-count, direction, fill-mode, play-state.
Choreograph entrance with staggered delays in JS or animation-delay per child (nth-child).
forwards fill mode) prevent elements from snapping back when animation ends.prefers-reduced-motion to disable the spinner rotation.Modern CSS: Custom Props, Container Queries, @layer
3 items26Custom Properties (CSS Variables)
--token: value on :root creates reusable, cascade-aware, runtime-updatable design tokens — superior to preprocessor variables for theming.:root {
--color-bg: #ffffff;
--color-text: #111827;
--color-primary: #2563eb;
--color-primary-hover: #1d4ed8;
--radius-md: 8px;
--shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
--space-unit: 0.25rem;
}
[data-theme="dark"] {
--color-bg: #111827;
--color-text: #f9fafb;
--color-primary: #60a5fa;
--color-primary-hover: #93c5fd;
}
body {
background: var(--color-bg);
color: var(--color-text);
}
.btn-primary {
background: var(--color-primary);
border-radius: var(--radius-md);
padding: calc(var(--space-unit) * 4) calc(var(--space-unit) * 6);
}
.btn-primary:hover {
background: var(--color-primary-hover);
}
/* Local scope — overrides inherit down subtree */
.card {
--card-padding: 1.5rem;
padding: var(--card-padding);
}
.card-compact {
--card-padding: 0.75rem;
}
/* Fallback syntax */
.muted {
color: var(--color-muted, #6b7280);
}Unlike Sass variables, custom properties are live in the browser — toggle [data-theme="dark"] on <html> and everything updates. They inherit and participate in the cascade.
--color-surface-elevated), not appearance (--color-light-gray), so dark mode swaps stay semantic.:root and [data-theme="dark"]. Add a JS button that toggles the attribute and watch colors update without reloading CSS.27Container Queries — Component-Level Responsiveness
@container lets components respond to their parent container's width, not the viewport — essential for reusable cards, sidebars, and widgets..card-wrapper {
container-type: inline-size;
container-name: card; /* optional */
}
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 120px 1fr;
gap: 1rem;
}
.card-image {
aspect-ratio: 1;
}
}
@container (min-width: 600px) {
.product {
flex-direction: row;
}
}
/* Container query units */
.sidebar-widget {
container-type: inline-size;
}
.widget-title {
font-size: clamp(1rem, 4cqw, 1.5rem);
/* cqw = 1% of container width */
}Setup requirements:
- Parent declares
container-type: inline-size(orsizefor both axes). - Child styles use
@container (min-width: …)instead of@media.
This fixes the problem of a "mobile" card layout inside a wide sidebar because the viewport is desktop-sized.
Shorthand: container: card / inline-size.
clamp() — component adapts to both container and typographic bounds..card component in a narrow (250px) and wide (500px) container. Use @container (min-width: 400px) to switch from stacked to horizontal layout — no viewport media query.28@layer — Explicit Cascade Control
@layer lets you declare intentional precedence order among stylesheet groups, reducing specificity hacks when combining resets, components, and utilities./* Declare order — first layer loses, last wins (among layers) */
@layer reset, base, components, utilities;
@layer reset {
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
}
}
@layer base {
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
}
a {
color: var(--color-primary);
}
}
@layer components {
.btn {
padding: 0.5em 1em;
border-radius: 4px;
background: var(--color-primary);
color: white;
}
}
@layer utilities {
.text-center { text-align: center; }
.mt-4 { margin-top: 1rem; }
}
/* Unlayered styles beat ALL layered styles */
.critical-fix {
color: red;
}Import with layer assignment:
@import url("vendor.css") layer(vendor);
@import url("components.css") layer(components);Layers compare before specificity within a layer. Two .btn rules in different layers — the higher layer wins regardless of specificity (unless unlayered).
tokens, base, components, utilities) for predictable overrides in design systems..btn in both components (blue) and utilities (green) layers. Confirm utilities layer wins with equal specificity, then add an unlayered rule and see it trump both.Architecture: BEM, Modules, Tailwind Tradeoffs
4 items29BEM Naming — Block, Element, Modifier
Block__Element--Modifier) produces flat, readable class names with predictable specificity — one class per element, state via modifiers./* Block */
.card { }
/* Element — part of block */
.card__title { }
.card__body { }
.card__footer { }
/* Modifier — variant or state */
.card--featured {
border: 2px solid gold;
}
.card__title--large {
font-size: 1.5rem;
}
.btn {
display: inline-block;
padding: 0.5em 1em;
}
.btn--primary { background: #2563eb; color: white; }
.btn--ghost { background: transparent; border: 1px solid currentColor; }
.btn--disabled { opacity: 0.5; pointer-events: none; }HTML:
<article class="card card--featured">
<h2 class="card__title card__title--large">Featured</h2>
<div class="card__body">Content</div>
<footer class="card__footer">
<button class="btn btn--primary">Buy</button>
</footer>
</article>Rules:
- Don't nest selectors (
.card .title) — use.card__title. - Modifiers aren't standalone — combine with block/element:
btn btn--primary. - Avoid deep chains; if names get long (
search-form__input--error), the block might need splitting.
&__element nesting for authoring, as long as compiled CSS stays flat..nav ul li a.active) into BEM classes and update HTML accordingly. Count selector specificity before and after.30CSS Modules — Scoped Styles by Default
:global() escapes when needed..module.css file returns a map of scoped class names:/* Button.module.css */
.root {
composes: base from "./typography.module.css";
padding: 0.5em 1em;
border-radius: 6px;
background: var(--color-primary);
color: white;
}
.root:hover {
filter: brightness(1.1);
}
.icon {
margin-right: 0.5em;
}
/* Escape hatch for global styles */
:global(.theme-dark) .root {
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1);
}import styles from "./Button.module.css";
export function Button({ children, icon }) {
return (
<button className={styles.root}>
{icon && <span className={styles.icon}>{icon}</span>}
{children}
</button>
);
}Compiled output might become .Button_root_x7f3a — unique per file.
Benefits: colocation with components, dead-code elimination, no accidental global overrides.
Tradeoffs: no built-in theming story (use custom properties), composes behavior varies by bundler, dynamic class names need careful handling.
.primaryButton) for ergonomic JS access: styles.primaryButton.Card.module.css with .card, .title, .body. Import in a component and verify hashed class names in DevTools.31Tailwind CSS — Utility-First Tradeoffs
<article class="rounded-lg border border-gray-200 bg-white p-6 shadow-sm hover:shadow-md transition-shadow">
<h2 class="text-xl font-semibold text-gray-900 mb-2">Card Title</h2>
<p class="text-gray-600 leading-relaxed mb-4">Description text.</p>
<button class="inline-flex items-center rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700">
Action
</button>
</article>Pros:
- No naming fatigue — no debates over
.card__title--highlighted. - Design tokens enforced via config (
theme.extend.colors). - Purge/tree-shake removes unused utilities in production.
- Responsive prefixes:
md:flex,lg:grid-cols-3.
Cons:
- HTML verbosity and readability suffer in complex UIs.
- Repeating long class strings across files — extract components (React/Vue), not
@applyeverywhere. @applyin CSS files reintroduces indirection Tailwind tries to avoid.- Harder for designers to read markup; requires Tailwind IntelliSense.
When @apply makes sense (sparingly):
@layer components {
.btn-primary {
@apply inline-flex rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700;
}
}<Button variant="primary">), not large @apply blocks — keeps purge working and markup clean.32Choosing Your CSS Architecture — Decision Framework
| Factor | Lean toward |
|---|---|
| Small static site, few pages | Plain CSS + @layer + custom properties |
| Component framework (React/Vue) | CSS Modules or scoped <style> |
| Design system, many contributors | Tokens on :root + documented components |
| Rapid prototyping, solo dev | Tailwind utilities |
| Large legacy codebase | BEM refactor zones + @layer to tame cascade |
| Theming / white-label | Custom properties + [data-theme] |
| Third-party widget isolation | Shadow DOM or strict BEM prefixes |
Recommended modern stack for many apps:
@layer reset, tokens, base, components, utilities;
@layer tokens {
:root {
--color-primary: #2563eb;
--space-4: 1rem;
}
}
@layer components {
/* BEM-ish or module-composed components */
.dialog { /* ... */ }
}// Component uses CSS Module + design tokens
import styles from "./Dialog.module.css";
export function Dialog() {
return (
<div className={styles.overlay} role="dialog">
<div className={styles.panel}>...</div>
</div>
);
}Principles that outlive any tool:
- Low specificity — easy overrides without
!important. - Design tokens — single source for color, space, type.
- Colocation — styles live near the component they style.
- Document conventions — naming, layers, and file structure in README or Storybook.
Avoid mixing all paradigms unplanned (BEM + Tailwind + global SCSS nesting) — pick primary strategy per codebase region.