Layout & Grid snippets

CSS layout snippets: grid, flexbox, centering, positioning, and responsive patterns. Every snippet includes a live demo, copyable CSS, and an LLM-ready prompt.

Widely supported

2-column grid layout

A responsive two-column layout using CSS Grid with sidebar and main content area. Copy the CSS, tweak the ratio, ship it.

no-js
Widely supported

Aspect-ratio card

Media card with consistent ratio and object-fit.

ui
Widely supported

Aspect ratio utility

Lock elements to a fixed aspect ratio — 1:1, 16:9, 4:3 and more — using the CSS aspect-ratio property. No padding hacks required. Copy and go.

no-js
Widely supported

Auto-responsive card grid

The RAM pattern: repeat(auto-fill, minmax()) for grids that adapt without media queries.

grid
Widely supported

Base layer

Register @layer base to house element-level reset and default styles at the lowest cascade priority — any component or utility automatically wins without needing !important.

no-js
New

Basic @layer setup

Declare CSS cascade layers with @layer to control specificity by order, not selector weight. Define reset, base, and components layers in one line.

no-js
New

Basic subgrid

A child grid that inherits its parent's track sizing using subgrid — perfect for aligned card grids and form layouts.

no-js
Widely supported

Brand tokens

A brand token layer — primary color, font stack, border radius, and shadow — as CSS custom properties that override the base design system for white-label or multi-brand projects.

tokensno-js
Widely supported

Breakpoint tokens

Named breakpoint reference values and modern media query range syntax — write @media (width >= 768px) instead of min-width and keep all breakpoints in one place.

tokensno-js
Widely supported

Button reset

Strip a button of all browser default styles to get a clean, unstyled element ready for custom CSS. Uses appearance: none, all: unset, or individual property resets.

no-js
Widely supported

Card row with flexbox

A horizontal row of cards using flexbox with wrapping. Equal-height cards that reflow gracefully on narrow viewports.

no-js
Widely supported

Center horizontally with margin: auto

Center any block element horizontally using margin-inline: auto. The simplest CSS centering technique — no flexbox or grid required. Copy and go.

no-js
Widely supported

Center in the Viewport

Center an element in the full viewport using position: fixed and CSS transforms — the classic modal/overlay centering technique.

no-js
Widely supported

Center inline elements with text-align

Use text-align: center on a block container to center inline and inline-block children — the original CSS centering technique, still useful today.

no-js
Widely supported

Center sticky element

Keep an element centered in the viewport while scrolling using position: sticky with top and transform — the sticky centering technique.

no-js
Widely supported

Center unknown-size element

Center an element of unknown dimensions both horizontally and vertically using CSS grid and place-items: center. Works for any content size. Copy and go.

no-js
Widely supported

Center utility

A reusable center utility using max-width and margin-inline: auto to constrain content to a readable width with consistent padding.

no-js
Widely supported

Center Vertically with Flexbox

The simplest way to center content vertically and horizontally — display: flex with align-items and justify-content.

no-js
Widely supported

Center with absolute + translate

Center any element with position: absolute and transform: translate(-50%, -50%). Works for unknown-size elements — the classic centering trick.

no-js
Widely supported

Center with container-query-aware technique

Use container queries to switch between block centering and flex centering depending on available space — a responsive centering pattern that adapts to its context.

no-js
Widely supported

Center with flexbox + margin: auto

Use margin: auto on a flex item to push it to the center — the cleanest way to center a single item inside a flex container without aligning properties.

no-js
Widely supported

Center with grid + place-items

The simplest way to center anything: two lines of CSS using display: grid and place-items: center.

no-js
Widely supported

Center with inline-block + vertical-align

Vertically center an element inside its container using inline-block display and vertical-align: middle — a pre-flexbox technique still useful for specific inline contexts.

no-js
Widely supported

Center with table-cell (legacy)

The classic pre-flexbox centering technique using display: table-cell and vertical-align: middle — still useful for supporting older email clients.

no-js
Widely supported

Center with writing-mode trick

Use writing-mode: vertical-lr and text-align: center to center elements — a lesser-known CSS trick that works without flexbox or grid.

no-js

Clearfix legacy

The classic clearfix hack for float-based layouts — a ::after pseudo-element that forces a container to wrap its floated children. Included for legacy codebases.

legacyno-js
Widely supported

Cluster utility

A cluster layout utility using flexbox wrap and gap for horizontal groupings of tags, buttons, and icons.

no-js
Widely supported

Component layer

Declare @layer components to contain reusable UI patterns. Component styles override the base layer automatically, and utility classes in a higher layer still win without specificity hacks.

no-js
Widely supported

Component tokens

Scope component-level design tokens with CSS custom properties. Build variants without extra classes by overriding variables at the component boundary.

no-js
Widely supported

Cover utility

A cover layout utility that vertically centers a principal element within a minimum-height container.

no-js
Widely supported

Dark mode tokens

Implement dark mode with CSS custom properties and prefers-color-scheme — redefine semantic tokens in the dark media query, add a data-theme toggle for user override.

tokensdark-modeno-js
New

Dashboard widget with CQ

A dashboard stat widget that adapts its layout from compact (icon + number stacked) to expanded (icon left, stats right) using CSS container queries.

no-js
Widely supported

Debug grid overlay

A CSS-only layout grid overlay using a fixed position element with repeating-linear-gradient columns — toggle with a body class to visualize alignment during development.

debugno-js
Widely supported

Duotone image with background-blend

Create a duotone image effect using CSS background-blend-mode — replace the image's colors with two custom hues using only CSS, no image editing required.

colorno-js
Widely supported

Background blend layers

Stack multiple background layers with blend modes to create rich, complex backgrounds using only CSS — no images needed.

colorno-js
Widely supported

Color-burn effect

Apply the color-burn blend mode to intensify contrast and saturation, creating rich, saturated color blends between overlapping elements.

colorno-js
Widely supported

Exclusion blend mode

Create high-contrast, inverted color effects using the exclusion blend mode — unique color inversion when overlapping elements blend.

colorno-js
Widely supported

Luminosity blend

Combine the luminance of one layer with the hue and saturation of another using the luminosity blend mode — preserve lightness while changing colors.

colorno-js
Widely supported

Duration and motion tokens

CSS custom property animation tokens — duration scale from instant to slow, easing curve tokens, and a prefers-reduced-motion override that sets all durations to 0.

tokensanimationno-js
New

Fade-in on scroll

Elements fade in and slide up as they enter the viewport using CSS scroll-driven animations. No JavaScript, no Intersection Observer.

animationno-js
Widely supported

Flow utility

A flow utility class that adds consistent vertical spacing between child elements using the lobotomized owl selector.

no-js
Widely supported

Form reset

Reset form element styles so inputs, selects, textareas, and buttons inherit your font and color — eliminating cross-browser styling inconsistencies.

no-js
New

Framework override layer

Use CSS @layer to override framework styles cleanly — no !important required. Your override layer always wins because it's declared last.

no-js
Widely supported

Full-bleed grid layout

A CSS Grid layout where content stays centered but full-bleed elements span the entire viewport width.

no-js
Widely supported

Full-bleed utility

Break an element out of a constrained content area to span the full viewport width using the CSS full-bleed technique with 100vw and calc(). Copy and go.

no-js
Widely supported

Full-height section with dvh

Use 100dvh for mobile-friendly full-viewport sections that account for browser chrome. The modern replacement for 100vh.

no-js
Widely supported

Holy grail layout

The classic holy grail layout with header, two sidebars, content, and footer using CSS Grid template areas.

grid
Widely supported

Image reset

Three-line image reset that fixes the inline-block gap, prevents overflow, and makes responsive images the default — display block, max-width 100%, height auto.

resetno-js
Widely supported

Layer ordering strategy

Define cascade layer order with @layer to control CSS priority across resets, components, themes, and utilities.

no-js
Widely supported

Line clamp

Truncate text to a fixed number of lines with an ellipsis using -webkit-line-clamp. Works in all modern browsers. Copy and go.

no-jstypography
Widely supported

List reset

Remove default list styling (bullets, numbers, padding, margin) with a reusable utility class — semantic HTML preserved, visual reset applied only where needed.

resetno-js
Widely supported

Media object

The classic media object pattern with image on the left and text on the right using flexbox and object-fit.

Widely supported

Minimal CSS reset

A minimal modern CSS reset — box-sizing border-box, margin collapse fix, and sensible body defaults in under 20 lines. No browser quirks, no opinionated base styles.

resetno-js
Widely supported

Modern CSS reset

A minimal modern CSS reset that removes inconsistent browser defaults, sets box-sizing, inherits fonts on form elements, and prevents media overflow.

no-js
Widely supported

Typography reset

A side-by-side comparison showing browser default typography vs. a modern CSS reset with box-sizing, font-family, line-height, and margins.

layoutno-js
Widely supported

Multi-brand tokens

A CSS custom property architecture for multi-brand design systems. Switch themes with a single data attribute. Copy and go.

no-js
Widely supported

Multiply overlay effect

Use mix-blend-mode: multiply to layer a brand color over a grayscale photo — the image shows through while being tinted to any hue, with no image editing required.

colorno-js
New

Nav collapse by container width

A navigation bar that collapses its links into a scrollable row or a compact stack based on the container width, using only CSS container queries.

no-js
New

Nested container queries

Use container queries inside other container queries to create components that respond independently to their own local context at every nesting level.

no-js
Widely supported

Object-fit utility

Utility classes for object-fit values (cover, contain, fill, none) to control how images fill their containers.

no-js
Widely supported

Page override layer

The @layer page layer sits at the top of the cascade — ideal for one-off layout adjustments on specific pages without creating specificity wars or scattering !important.

no-js
Widely supported

Radius Token Scale

CSS custom property border-radius scale — --radius-none through --radius-full for consistent rounding across a design system.

no-js
Widely supported

Reset in a layer

Place your CSS reset inside a cascade layer so component styles always override reset defaults without specificity wars.

no-js
New

Responsive table with container queries

A data table that collapses into a card-stack layout at narrow container widths using CSS container queries, keeping data readable at any size.

no-js
Widely supported

Responsive table

A table that stacks into card-like rows on small screens using container queries and data-label attributes.

ui
Widely supported

Text on dark image

Make text legible over any photo using a linear-gradient scrim — a CSS-only technique that darkens the image locally without covering the entire background.

colorno-js
Widely supported

Scroll reset

Smooth scroll behavior, scroll-margin-top for fixed header offset, and overscroll-behavior to prevent the pull-to-refresh bounce on mobile web apps.

resetno-js
Widely supported

Semantic tokens

Semantic CSS custom properties that describe purpose rather than value — --color-surface, --color-on-surface, --color-border — making dark mode and theming a one-token swap.

tokensno-js
Widely supported

Shadow Tokens

A CSS custom property shadow scale using layered box-shadows for consistent depth in design systems.

no-js
New

Sidebar-aware layout

A layout that uses CSS container queries to reflow its content based on the container width, not the viewport — perfect for reusable components.

no-js
Widely supported

Sidebar utility

A sidebar layout utility using CSS flexbox that stacks at narrow widths and sits beside content at wide widths.

no-js
Widely supported

Spacing Scale Tokens

A CSS custom property spacing scale using a 1.25 ratio for consistent design system padding and gaps.

no-js
Widely supported

Spacing scale

A numeric spacing scale as CSS custom properties — --space-1 through --space-24 using a 4px base unit — giving consistent rhythm across all margins, padding, and gaps.

tokensno-js
Widely supported

Split navigation

A flexbox navigation bar with logo on the left and links pushed to the right. Responsive, accessible, zero JavaScript.

uino-js
Widely supported

Stack utility

A vertical stack layout utility using flexbox and gap for consistent spacing between child elements. One class, zero layout math.

no-js
Widely supported

Stat card with container queries

A stats card that adapts from a compact vertical stack to a horizontal row using container queries — no media queries needed.

container-queriesno-js
Widely supported

Sticky footer with flex

Push the footer to the bottom of the viewport when content is short using flexbox. Three lines of CSS, no hacks.

no-js
Widely supported

Sticky footer

A sticky footer that stays at the bottom of the viewport even with short content.

Widely supported

Subgrid layout

Aligned grid children with CSS subgrid — no fixed heights.

Widely supported

Switcher utility

A switcher layout utility that flips between horizontal and vertical arrangement based on container width.

no-js
Widely supported

Table reset

Normalize HTML table layout with border-collapse, table-layout fixed, and consistent cell padding — removes double borders and makes column widths predictable.

resetno-js
Widely supported

Theme layer

Use a dedicated @layer for theme overrides to swap design tokens without touching component specificity.

no-js
Widely supported

Theming System

A CSS custom property theming system — switch between light, dark, and brand themes by toggling a data-theme attribute on any element.

no-js
Widely supported

Type scale tokens

Font size tokens from --text-xs to --text-6xl using a Major Third ratio (1.25×), plus fluid heading tokens using clamp() for responsive typography without breakpoints.

tokensno-js
Widely supported

Utility layer

Place utility classes in the highest cascade layer so they always override component styles without !important.

no-js
Widely supported

Vendor layer

Wrap third-party CSS in @layer vendor so your own styles always win — no !important, no specificity battles with library class names.

no-js
Widely supported

Visually hidden utility

A CSS utility class that hides content visually while keeping it accessible to screen readers. The sr-only pattern every project needs.

a11yno-js
Widely supported

z-index token scale

Create a z-index token scale (base, dropdown, modal, toast) to avoid "random 9999". A small, documented system prevents stacking-context chaos.

no-js
Widely supported

Typography reset

A side-by-side comparison showing browser default typography vs. a modern CSS reset with box-sizing, font-family, line-height, and margins.

typographyno-js
Widely supported

Color-burn effect

Intensify contrast and saturation with CSS mix-blend-mode: color-burn — create rich, vivid overlays that punch up colors without image editing.

colorno-js
Widely supported

Exclusion blend mode

Use mix-blend-mode: exclusion to create a high-contrast, inverted color effect — overlapping elements produce a unique color inversion that shifts hues and intensifies contrast.

colorno-js
Widely supported

Luminosity blend

Use mix-blend-mode: luminosity to preserve lightness while changing color relationships — the hue and saturation come from the blend layer while luminance stays intact.

colorno-js
Widely supported

Background blend layers

Stack multiple background layers with blend modes to create complex, layered effects — combine gradients, images, and solid colors with CSS background-blend-mode.

colorno-js
Widely supported

Text over image with blend

Place text over images with CSS blend modes for stunning overlays — combine mix-blend-mode with text-shadow for readable, visually striking captions.

colorno-js
Widely supported

Decorative blend effect

Create decorative overlays using CSS blend modes — add artistic flourishes and visual interest without images or JavaScript.

colorno-js
Widely supported

Filter chain effect

Stack multiple CSS filters (blur, brightness, contrast, hue-rotate) to create complex visual effects — chain filters for unique styling without images.

colorno-js
Widely supported

The ultimate centering decision tree

A decision tree for choosing the right centering technique — flexbox, grid, absolute positioning, or transform. Know when to use each method.

layoutno-js
Widely supported

Typography adapts to container

Use container queries to make typography respond to its container — fluid fonts that scale based on available space, not just viewport width.

layoutno-js
Widely supported

Spacing adapts to container

Use container queries to make spacing adapt to available space — gap, padding, and margins that respond to container size for truly fluid layouts.

layoutno-js
Widely supported

Image layout adapts to container

Make image layouts respond to container size using container queries — switch between grid, flex, and stacked layouts based on available space.

layoutno-js
Widely supported

Icon + text adapts to container

Use container queries to adapt icon + text layouts — switch between horizontal and vertical arrangements based on available space.

layoutno-js
Widely supported

Media card with CQ

Create responsive media cards using container queries — image and content layouts that adapt to container size, not just viewport.

layoutno-js
Widely supported

Profile card with CQ

Build profile cards with container queries — avatar, name, and bio layouts that adapt to available space for reusable components.

layoutno-js
Widely supported

Pricing card with CQ

Create responsive pricing cards with container queries — feature lists, prices, and CTAs that adapt to container size.

layoutno-js
Widely supported

Feature card with CQ

Build feature cards using container queries — icons, headings, and descriptions that reflow based on available space.

layoutno-js
Widely supported

Testimonial card with CQ

Use container queries for testimonial cards — quotes, avatars, and names that adapt to container width for reusable components.

layoutno-js
Widely supported

CTA block with CQ

Create CTA blocks with container queries — headings, text, and buttons that adapt layout based on container size.

layoutno-js
Widely supported

Hero section with CQ

Build hero sections with container queries — headlines, subheads, and CTAs that adapt to container width for flexible layouts.

layoutno-js
Widely supported

Layout shift prevention with CQ

Prevent layout shifts using container queries — stable dimensions and responsive containers that adapt without reflow.

layoutno-js