CSS Cheatsheet
At a glance
| What it is | The style sheet language that controls how HTML is laid out and presented |
| Use it for | Layout (flexbox, grid), typography, colors, responsive design, animation, theming |
| Latest stable | CSS Snapshot 2026 (W3C Group Note, latest revision 2026-06-22) + Baseline for browser support - checked 2026-10-31 |
| Versioning | No "CSS4": CSS is split into modules with their own levels; the yearly snapshot lists what is stable |
| Official docs | W3C CSS specs - CSS Snapshot 2026 - MDN CSS - Baseline |
Core concepts
- Cascade: origin +
!important->@layerorder -> specificity -> source order decides which rule wins - Specificity: (id, class/attr/pseudo-class, type) -
#abeats.a.b.c;:where()is 0,:is()/:not()/:has()take their most specific argument - Inheritance: text properties (
color,font-*) inherit; box properties (margin,border) do not; force withinherit - Box model: content + padding + border + margin; use
box-sizing: border-box - Formatting contexts: block vs inline flow; flex and grid create new contexts; margins collapse only in block flow
- Stacking context:
z-indexonly compares siblings inside the same context (created byposition+z-index,opacitybelow 1,transform,isolation: isolate) - Custom properties:
--name: valuecascades and inherits; read withvar(--name, fallback) - Responsive: media queries react to the viewport, container queries to the parent size
- Logical properties:
margin-inline,padding-block,inset-inline-startfollow writing direction
Syntax & commands
/* Selectors */
.card > h2 + p ~ p {} /* child, adjacent sibling, general sibling */
a[href^="https"] {} /* attribute starts with */
li:nth-child(2n + 1) {} /* odd items */
input:focus-visible {} /* keyboard focus only */
.card:has(img) {} /* parent selector */
:is(h1, h2, h3) a {} /* group, takes highest specificity */
:where(ul, ol) { padding: 0 } /* group, zero specificity */
p::first-line, .btn::before {} /* pseudo-elements */
/* Reset basics */
*, *::before, *::after { box-sizing: border-box; }
img { max-width: 100%; height: auto; display: block; }
/* Flexbox: one dimension */
.row {
display: flex;
gap: 1rem;
justify-content: space-between; /* main axis */
align-items: center; /* cross axis */
flex-wrap: wrap;
}
.row > .grow { flex: 1 1 0; } /* grow shrink basis */
/* Grid: two dimensions */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}
.layout {
display: grid;
grid-template: "header header" auto "nav main" 1fr / 14rem 1fr;
}
.layout > header { grid-area: header; }
.center { display: grid; place-items: center; }
/* Custom properties + theming */
:root {
color-scheme: light dark;
--brand: oklch(60% 0.15 250);
--space: clamp(1rem, 2vw + 0.5rem, 2rem);
}
body { color: light-dark(#111, #eee); padding: var(--space); }
/* Media + container queries */
@media (width >= 48rem) { .sidebar { display: block; } }
@media (prefers-reduced-motion: reduce) { * { animation: none; } }
.card-list { container-type: inline-size; }
@container (width > 30rem) { .card { display: flex; } }
/* Native nesting */
.btn {
color: white;
&:hover { filter: brightness(1.1); }
.icon { margin-inline-end: 0.5rem; }
}
/* Cascade layers: later layer wins regardless of specificity */
@layer reset, base, components, utilities;
@layer components { .btn { padding: 0.5rem 1rem; } }
| Unit | Relative to |
|---|---|
px | CSS pixel (absolute) |
rem / em | root font size / parent font size |
% | parent box (property dependent) |
vw vh | viewport; svh lvh dvh = small/large/dynamic viewport (mobile toolbars) |
ch | width of "0" in the current font (good for line length) |
fr | share of free space in a grid track |
cqi cqb | container inline/block size (inside a container query) |
Common patterns
/* Readable centered column */
.prose { max-width: 65ch; margin-inline: auto; }
/* Sticky header */
header { position: sticky; top: 0; z-index: 10; }
/* Truncate text */
.one-line { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.three-lines { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
/* Aspect ratio media */
.thumb { aspect-ratio: 16 / 9; object-fit: cover; width: 100%; }
/* Fluid type */
h1 { font-size: clamp(1.75rem, 4vw + 1rem, 3rem); text-wrap: balance; }
/* Visually hidden but readable by screen readers */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* Entry animation for elements that appear (e.g. popover) */
[popover] { transition: opacity 0.2s, display 0.2s allow-discrete; opacity: 1; }
@starting-style { [popover]:popover-open { opacity: 0; } }
Gotchas
z-indexdoes nothing onposition: staticelements (except flex/grid items) and is trapped by the parent stacking context100vhon mobile includes the area under browser toolbars; prefer100dvh/100svh- Percent
heightneeds a parent with a definite height; percent padding/margin is based on the parent width - Vertical margins collapse between adjacent blocks (not in flex/grid, not with padding/border in between)
display: noneremoves from the accessibility tree;visibility: hiddenkeeps layout space; use.sr-onlyto hide visually only- Flex items default to
min-width: autoand refuse to shrink below content; addmin-width: 0for overflowing text - Unlayered styles beat every
@layerstyle;!importantreverses layer order - Transitions cannot animate
displayor to/fromautowithouttransition-behavior: allow-discrete/interpolate-size :hoversticks on touch devices; guard with@media (hover: hover)
What's new in recent releases
CSS ships per feature; these became Baseline (all major browsers) recently.
- Anchor positioning (
anchor-name,position-anchor,position-area): tether tooltips/menus to another element without JS; Baseline newly available in January 2026 (web.dev) @scope: limit selectors to a DOM subtree with an optional lower bound; Baseline newly available with Firefox 146 (web.dev)@starting-style+transition-behavior: allow-discrete: entry/exit animations for elements that toggledisplay(dialogs, popovers); Baseline since 2024light-dark(),@property(typed custom properties),text-wrap: balance: Baseline since 2024- Already safe to use: native nesting,
:has(), container queries, cascade layers,oklch()/color-mix() - Customizable select (
appearance: base-select): Chrome + Safari only for now, not Baseline