Element, class, ID
Select by tag name, class or id.
p { }
.card { }
#header { }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
Select by tag name, class or id.
p { }
.card { }
#header { }Both classes on one element, or a list of selectors sharing the same rules.
.btn.primary { }
h1, h2, h3 { }Any level inside, or direct children only.
.nav a { }
.nav > li { }The element right after, or any later sibling.
h2 + p { }
h2 ~ p { }Has an attribute, equals, starts with, ends with, contains.
[disabled] { }
input[type="email"] { }
a[href^="https"] { }
a[href$=".pdf"] { }
[class*="icon-"] { }Hover, keyboard focus, and form states.
a:hover { }
button:focus-visible { }
input:disabled { }
input:checked { }
input:invalid { }First, last, every other, or the first 3.
li:first-child { }
li:last-child { }
tr:nth-child(even) { }
li:nth-child(-n + 3) { }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.
Add content before or after, or style the first line or placeholder.
.required::after { content: " *"; }
p::first-line { }
input::placeholder { }Makes width and height include padding and border. Most sites set this first.
*, *::before, *::after {
box-sizing: border-box;
}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 */
}Width, style and colour; radius rounds the corners.
.card {
border: 1px solid #e5e7eb;
border-radius: 8px;
}Like a border but takes no space. Good for focus styles.
button:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}What happens when content is too big: hide, scroll, or scroll only when needed.
.panel { overflow: auto; }
.clip { overflow: hidden; }How an element takes part in layout.
display: block | inline | inline-block | flex | grid | none;Lay children out in a row with a gap between them.
.row {
display: flex;
gap: 12px;
}Centre children horizontally and vertically.
.center {
display: flex;
justify-content: center;
align-items: center;
}Stack in a column, or let items wrap onto new lines.
.list {
display: flex;
flex-direction: column;
}
.tags {
display: flex;
flex-wrap: wrap;
}Spread items along the row (or column).
justify-content: flex-start | center | flex-end | space-between | space-around | space-evenly;Line items up across the row.
align-items: stretch | flex-start | center | flex-end | baseline;Let one item fill the remaining space, or keep a fixed size.
.main { flex: 1; }
.sidebar { flex: 0 0 250px; }An auto margin takes all spare space.
.nav .logout { margin-left: auto; }Three columns of equal width with a gap.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}As many columns as fit, each at least 220px wide.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 16px;
}Make an item cover more than one cell.
.wide { grid-column: span 2; }
.full { grid-column: 1 / -1; }
.tall { grid-row: span 2; }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; }The shortest way to centre one child.
.center {
display: grid;
place-items: center;
}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;Cover the whole parent box.
.parent { position: relative; }
.overlay {
position: absolute;
inset: 0;
}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.
Higher z-index sits on top. Works on positioned elements and on children of flex or grid containers.
.modal { position: fixed; z-index: 1000; }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;
}Align, change case, underline.
text-align: left | center | right | justify;
text-transform: uppercase | lowercase | capitalize;
text-decoration: underline | none;One line with an ellipsis.
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}Show 2 lines and cut the rest with an ellipsis.
.clamp {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}Stop long URLs from overflowing their box.
.content { overflow-wrap: anywhere; }Grows with the screen but never below 1.5rem or above 3rem.
h1 { font-size: clamp(1.5rem, 4vw, 3rem); }Hex, RGB, HSL. The value after / is transparency.
color: #1f2937;
color: rgb(31 41 55 / 0.8);
color: hsl(220 26% 14%);Cover the box, centred, without repeating.
.hero {
background: url("hero.jpg") center / cover no-repeat;
}Linear and radial gradients.
background: linear-gradient(135deg, #667eea, #764ba2);
background: radial-gradient(circle, #fff, #ddd);Fade a whole element, including its children.
.faded { opacity: 0.5; }rem = root font size, em = parent font size, % = parent, vw/vh = viewport.
px rem em % vw vh dvh ch frdvh accounts for mobile browser toolbars; use it for full-height mobile layouts.
Responsive sizes without media queries.
.container {
width: min(100% - 32px, 1100px);
margin-inline: auto;
}
.sidebar { width: max(200px, 20%); }Keep a 16:9 box at any width.
.video { aspect-ratio: 16 / 9; width: 100%; }Never wider than their container; crop to fill a box.
img { max-width: 100%; height: auto; }
.thumb { width: 100px; height: 100px; object-fit: cover; }Apply rules from a screen width upwards (mobile-first).
@media (min-width: 768px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}Follow the device’s light or dark setting.
@media (prefers-color-scheme: dark) {
body { background: #0f0f0f; color: #f0f0f5; }
}Turn off animations for people who ask for less motion.
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; transition: none !important; }
}Change a component based on its own width, not the screen.
.card-wrap { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; }
}Hide things when printing.
@media print {
nav, footer, .no-print { display: none; }
}Define once, reuse everywhere. Change them for a theme.
:root {
--brand: #667eea;
--radius: 8px;
}
.btn {
background: var(--brand);
border-radius: var(--radius);
}Used when the variable is not set.
color: var(--text, #111);Mix units in a calculation.
.main { width: calc(100% - 250px); }
.gap { margin-top: calc(var(--space) * 2); }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); }Move, scale and rotate without affecting layout.
.btn:hover { transform: translateY(-2px) scale(1.02); }
.icon { transform: rotate(45deg); }Animate changes between states. List the properties instead of all.
.btn {
transition: background-color 0.2s ease, transform 0.2s ease;
}Define steps and play them.
@keyframes spin {
to { transform: rotate(360deg); }
}
.loader { animation: spin 1s linear infinite; }Blur the background behind an element, or filter the element itself.
.glass { backdrop-filter: blur(8px); }
img.gray { filter: grayscale(1); }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.
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.
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.
Put display: flex; justify-content: center; align-items: center; on the parent, or display: grid; place-items: center;.
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.
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.