CSSPrintable

CSS Cheat Sheet

Selectors, the box model, Flexbox, Grid, responsive design, modern CSS and animation. The CSS you use every day, on one page.

Back to the CSS tutorial

Selectors

SyntaxWhat it selectsLesson
pEvery <p>Selectors
.cardElements with class cardSelectors
#logoThe element with id logoSelectors
nav aa anywhere inside navCombinators
ul > liDirect children onlyCombinators
h2 + pThe p right after an h2Combinators
[type="email"] [href$=".pdf"]By attribute valueCombinators
.card:has(img)Elements that contain somethingModern selectors
:is(h1, h2) :where(...)Group selectorsModern selectors
a:hover :focus-visibleElement statesPseudo-classes
li:nth-child(2n)Every even itemPseudo-classes
p::first-line ::beforeParts of elementsPseudo-elements

Colour & text

SyntaxWhat it doesLesson
color: #1e3a8aText colourColors
rgb(0 0 0 / 50%)Colour with transparencyColors
hsl(150 70% 40%)Hue, saturation, lightnessColors
font-family: Georgia, serifFont stackFonts
font: 700 1.5rem/1.2 sans-serifFont shorthandFonts
text-align: centerAlign textText
line-height: 1.6Space between linesText
text-transform: uppercaseChange caseText
text-overflow: ellipsis"Long tex…" (with nowrap + hidden)Overflow

Units

SyntaxMeansLesson
pxFixed pixelsUnits
rem× root font size (16px)Units
em× current font sizeUnits
%Of the parentUnits
vw / vh1% of viewport width / heightUnits
chWidth of "0"Units
clamp(1rem, 4vw, 2rem)Fluid between limitsUnits

Box model

SyntaxWhat it doesLesson
box-sizing: border-boxWidth includes padding & borderBox model
padding: 10px 20pxSpace inside (top/bottom, left/right)Margin & padding
margin: 0 autoCentre a block horizontallyMargin & padding
border: 2px solid #000Border shorthandBorders
border-radius: 50%Circle (on a square)Borders
max-width: 600pxNever wider thanBox model
overflow: autoScroll when content is too bigBox model

Display & position

SyntaxWhat it doesLesson
display: block / inline / inline-blockBox behaviourDisplay
display: noneRemove from pageDisplay
position: relativeOffset from normal placePosition
position: absolutePlace inside nearest positioned parentPosition
position: fixedStuck to the screenPosition
position: sticky; top: 0Sticks when scrolled toPosition
z-index: 10Stacking orderPosition

Flexbox

SyntaxWhat it doesLesson
display: flexMake a flex containerFlexbox
flex-direction: columnStack verticallyFlexbox
justify-content: space-betweenSpread along the main axisFlexbox
align-items: centerAlign on the cross axisFlexbox
gap: 1remSpace between itemsFlexbox
flex-wrap: wrapAllow multiple linesFlexbox
flex: 1Item grows to fill spaceFlexbox

Grid

SyntaxWhat it doesLesson
display: gridMake a grid containerGrid
grid-template-columns: 1fr 2frColumn sizesGrid
repeat(3, 1fr)Three equal columnsGrid
repeat(auto-fit, minmax(200px, 1fr))Responsive cards, no media queryGrid
grid-column: 1 / -1Span every columnGrid
grid-template-areasName layout areasGrid
place-items: centerCentre in both directionsGrid

Responsive & variables

SyntaxWhat it doesLesson
@media (min-width: 768px) { }Styles for wider screensMedia queries
@media (prefers-color-scheme: dark)Dark modeMedia queries
--brand: #17a865;Define a variableVariables
color: var(--brand)Use a variableVariables
!importantForce a rule (avoid!)Inheritance
@container (min-width: 400px)Styles based on the containerContainer queries
clamp(1rem, 0.8rem + 2vw, 2rem)Fluid size with limitscalc() & clamp()
light-dark(#fff, #111)Light or dark colourDark mode

Effects & motion

SyntaxWhat it doesLesson
box-shadow: 0 4px 12px #0003Shadow around a boxShadows & gradients
linear-gradient(red, blue)Gradient backgroundShadows & gradients
transition: transform 0.2sAnimate changes smoothlyTransitions
transform: translateY(-4px)Move without affecting layoutTransforms
transform: rotate(45deg) scale(1.2)Rotate and scaleTransforms
@keyframes spin { to { … } }Define an animationAnimations
animation: spin 1s linear infiniteRun itAnimations

Sizing & centering

SyntaxWhat it doesLesson
max-width: 600pxNever wider than 600pxWidth & height
box-sizing: border-boxWidth includes padding and borderWidth & height
aspect-ratio: 16 / 9Keep a shapeWidth & height
overflow-x: autoScroll sideways when neededOverflow
margin-inline: autoCentre a blockCentering
display: grid; place-items: centerCentre both waysCentering
float: left clear: bothWrap text around an imageFloat

Responsive patterns

SyntaxWhat it doesLesson
img { max-width: 100%; height: auto; }Fluid imagesResponsive images
repeat(auto-fit, minmax(240px, 1fr))Card grid, no media queryResponsive layouts
width: min(100% - 32px, 1100px)Centred container with guttersResponsive design
max-width: 65chReadable line lengthResponsive typography
[aria-expanded="true"] + .menuOpen a hamburger menuResponsive navbar
scroll-snap-type: x mandatorySwipeable carouselScroll snap
object-fit: coverCrop an image to its boxImages

How to use this CSS cheat sheet

This CSS cheat sheet collects the properties and selectors you'll use most, in the same order as the course: selectors, colours, text and fonts, the box model, display and position, flexbox, grid, responsive design and animation. Each section links to the lesson that explains it with live examples you can edit.

Keep it open while you build a page. When you can't remember whether it's justify-content or align-items, or how to write a media query, a quick look here is faster than a search. Type a word into the filter box to jump straight to it, or print the sheet and keep it beside your keyboard.

CSS gotchas worth remembering

  • Specificity beats order. A rule with an id selector wins over a later rule with only classes. If a style won't apply, check specificity in the browser's developer tools before reaching for !important.
  • Width doesn't include padding by default. Add *, *::before, *::after { box-sizing: border-box; } so padding and borders stay inside the width you set.
  • Vertical margins collapse. Two stacked margins of 20px and 30px give a 30px gap, not 50px. Flexbox and grid gaps don't collapse.
  • justify-content follows the main axis. In a row it works horizontally; in a column it works vertically. align-items always works on the other axis.
  • position: absolute needs a positioned parent. Give the parent position: relative, otherwise the element positions itself against the page.

The CSS lessons behind this cheat sheet