Helper-KitCheatsheets

CSS Cheatsheet

CSS selectors, flexbox, grid, positioning, units, media queries, variables and animations, with copy-ready examples.

CSS Cheatsheet · Helper-Kit

Everything here works in all current browsers unless a card says otherwise.

59 entries

Selectors

Element, class, ID

Select by tag name, class or id.

p { }
.card { }
#header { }

Combine selectors

Both classes on one element, or a list of selectors sharing the same rules.

.btn.primary { }
h1, h2, h3 { }

Descendant and child

Any level inside, or direct children only.

.nav a { }
.nav > li { }

Siblings

The element right after, or any later sibling.

h2 + p { }
h2 ~ p { }

Attribute

Has an attribute, equals, starts with, ends with, contains.

[disabled] { }
input[type="email"] { }
a[href^="https"] { }
a[href$=".pdf"] { }
[class*="icon-"] { }

State

Hover, keyboard focus, and form states.

a:hover { }
button:focus-visible { }
input:disabled { }
input:checked { }
input:invalid { }

Position in a list

First, last, every other, or the first 3.

li:first-child { }
li:last-child { }
tr:nth-child(even) { }
li:nth-child(-n + 3) { }

Not, is, has

Exclude matches, group selectors, or select a parent by what it contains.

.btn:not(.primary) { }
:is(h1, h2, h3) a { }
.card:has(img) { }

:has() works in all current browsers since late 2023.

Pseudo-elements

Add content before or after, or style the first line or placeholder.

.required::after { content: " *"; }
p::first-line { }
input::placeholder { }

Box model

Include padding in the width

Makes width and height include padding and border. Most sites set this first.

*, *::before, *::after {
  box-sizing: border-box;
}

Margin and padding

One value for all sides; two for top/bottom and left/right; four go top, right, bottom, left.

.box {
  margin: 16px;
  padding: 8px 16px;
  margin: 0 auto; /* centre a block */
}

Border and rounded corners

Width, style and colour; radius rounds the corners.

.card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

Outline

Like a border but takes no space. Good for focus styles.

button:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

Overflow

What happens when content is too big: hide, scroll, or scroll only when needed.

.panel { overflow: auto; }
.clip { overflow: hidden; }

Display

How an element takes part in layout.

display: block | inline | inline-block | flex | grid | none;

Flexbox

Row of items

Lay children out in a row with a gap between them.

.row {
  display: flex;
  gap: 12px;
}

Centre anything

Centre children horizontally and vertically.

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

Direction and wrapping

Stack in a column, or let items wrap onto new lines.

.list {
  display: flex;
  flex-direction: column;
}
.tags {
  display: flex;
  flex-wrap: wrap;
}

Main-axis alignment

Spread items along the row (or column).

justify-content: flex-start | center | flex-end | space-between | space-around | space-evenly;

Cross-axis alignment

Line items up across the row.

align-items: stretch | flex-start | center | flex-end | baseline;

Grow and shrink

Let one item fill the remaining space, or keep a fixed size.

.main { flex: 1; }
.sidebar { flex: 0 0 250px; }

Push an item to the end

An auto margin takes all spare space.

.nav .logout { margin-left: auto; }

Grid

Equal columns

Three columns of equal width with a gap.

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Responsive cards without media queries

As many columns as fit, each at least 220px wide.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

Span columns or rows

Make an item cover more than one cell.

.wide { grid-column: span 2; }
.full { grid-column: 1 / -1; }
.tall { grid-row: span 2; }

Named areas

Draw the layout with names.

.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }

Centre with grid

The shortest way to centre one child.

.center {
  display: grid;
  place-items: center;
}

Positioning

Position values

relative = nudge from normal place; absolute = place inside nearest positioned parent; fixed = stick to the screen; sticky = stick while scrolling.

position: static | relative | absolute | fixed | sticky;

Overlay inside a parent

Cover the whole parent box.

.parent { position: relative; }
.overlay {
  position: absolute;
  inset: 0;
}

Sticky header

Stays at the top while the page scrolls.

.header {
  position: sticky;
  top: 0;
  z-index: 10;
}

Sticky stops working if a parent has overflow: hidden.

Stacking order

Higher z-index sits on top. Works on positioned elements and on children of flex or grid containers.

.modal { position: fixed; z-index: 1000; }

Typography

Font

Family with fallbacks, size, weight and line spacing.

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.6;
}

Alignment and transform

Align, change case, underline.

text-align: left | center | right | justify;
text-transform: uppercase | lowercase | capitalize;
text-decoration: underline | none;

Cut long text with …

One line with an ellipsis.

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Limit to a number of lines

Show 2 lines and cut the rest with an ellipsis.

.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

Break long words

Stop long URLs from overflowing their box.

.content { overflow-wrap: anywhere; }

Fluid font size

Grows with the screen but never below 1.5rem or above 3rem.

h1 { font-size: clamp(1.5rem, 4vw, 3rem); }

Colours & backgrounds

Colour formats

Hex, RGB, HSL. The value after / is transparency.

color: #1f2937;
color: rgb(31 41 55 / 0.8);
color: hsl(220 26% 14%);

Background image

Cover the box, centred, without repeating.

.hero {
  background: url("hero.jpg") center / cover no-repeat;
}

Gradient

Linear and radial gradients.

background: linear-gradient(135deg, #667eea, #764ba2);
background: radial-gradient(circle, #fff, #ddd);

Transparency

Fade a whole element, including its children.

.faded { opacity: 0.5; }

Units & sizing

Common units

rem = root font size, em = parent font size, % = parent, vw/vh = viewport.

px  rem  em  %  vw  vh  dvh  ch  fr

dvh accounts for mobile browser toolbars; use it for full-height mobile layouts.

Min, max and clamp

Responsive sizes without media queries.

.container {
  width: min(100% - 32px, 1100px);
  margin-inline: auto;
}
.sidebar { width: max(200px, 20%); }

Keep a shape

Keep a 16:9 box at any width.

.video { aspect-ratio: 16 / 9; width: 100%; }

Images that fit

Never wider than their container; crop to fill a box.

img { max-width: 100%; height: auto; }
.thumb { width: 100px; height: 100px; object-fit: cover; }

Responsive

Media query

Apply rules from a screen width upwards (mobile-first).

@media (min-width: 768px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

Dark mode

Follow the device’s light or dark setting.

@media (prefers-color-scheme: dark) {
  body { background: #0f0f0f; color: #f0f0f5; }
}

Reduce motion

Turn off animations for people who ask for less motion.

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

Container query

Change a component based on its own width, not the screen.

.card-wrap { container-type: inline-size; }
@container (min-width: 400px) {
  .card { display: flex; }
}

Print styles

Hide things when printing.

@media print {
  nav, footer, .no-print { display: none; }
}

Variables

Custom properties

Define once, reuse everywhere. Change them for a theme.

:root {
  --brand: #667eea;
  --radius: 8px;
}
.btn {
  background: var(--brand);
  border-radius: var(--radius);
}

Fallback value

Used when the variable is not set.

color: var(--text, #111);

Calculate

Mix units in a calculation.

.main { width: calc(100% - 250px); }
.gap { margin-top: calc(var(--space) * 2); }

Effects & animation

Shadow

Box shadow and text shadow.

.card { box-shadow: 0 2px 8px rgb(0 0 0 / 0.1); }
h1 { text-shadow: 0 1px 2px rgb(0 0 0 / 0.3); }

Transform

Move, scale and rotate without affecting layout.

.btn:hover { transform: translateY(-2px) scale(1.02); }
.icon { transform: rotate(45deg); }

Transition

Animate changes between states. List the properties instead of all.

.btn {
  transition: background-color 0.2s ease, transform 0.2s ease;
}

Keyframe animation

Define steps and play them.

@keyframes spin {
  to { transform: rotate(360deg); }
}
.loader { animation: spin 1s linear infinite; }

Blur and filters

Blur the background behind an element, or filter the element itself.

.glass { backdrop-filter: blur(8px); }
img.gray { filter: grayscale(1); }

Smooth scrolling

Animate jumps to #anchors.

html { scroll-behavior: smooth; }

💡 Search matches titles, descriptions and the commands themselves. Copy puts the exact text on your clipboard; Print gives a clean sheet without menus or buttons. Searching happens in your browser; nothing is sent anywhere.

About This Cheatsheet

CSS controls how web pages look and how they are laid out. This cheatsheet covers selectors, the box model, flexbox, grid, positioning, typography, colours and backgrounds, units and sizing, responsive design and media queries, custom properties (variables) and effects and animation. Each card has a short, copy-ready example that you can paste straight into a stylesheet. The examples use modern CSS that works in all current browsers, such as gap, clamp(), aspect-ratio, :has() and container queries, with a note when a feature is newer. To shrink your finished stylesheet, or tidy one up, use the CSS Minifier & Beautifier.

Quick Tips

  • Set box-sizing: border-box on everything first, so widths include padding and borders.
  • Use flexbox for a row or a column of items, and grid for rows and columns together.
  • Prefer rem for font sizes so text respects the visitor's browser settings.
  • Keep visible focus styles (:focus-visible) for people who use a keyboard.
  • Put colours and spacing in custom properties so a theme change is a one-line edit.

Frequently Asked Questions

Should I use flexbox or grid?

Flexbox is best for one direction: a row of buttons or a column of cards. Grid is best when you need rows and columns at the same time, such as a page layout or a card gallery.

How do I centre something?

Put display: flex; justify-content: center; align-items: center; on the parent, or display: grid; place-items: center;.

What is the difference between margin and padding?

Padding is the space inside the border, between the border and the content. Margin is the space outside the border, between this element and its neighbours.

What is the difference between rem and em?

rem is relative to the root (html) font size, so it is the same everywhere. em is relative to the current element's font size, so it compounds when elements are nested.

Was this tool helpful?

More in this category