HTML & CSS cheatsheet – syntax overview
72 key commands and patterns for quick reference.
HTML Skeleton & Text Semantic Structure Forms Tables & Lists Selectors Box Model & Units Flexbox Grid Responsive & Variables Animation & Accessibility
HTML Skeleton & Text
<!DOCTYPE html><html lang="en"> | Standards mode and document language |
<meta charset="utf-8"> | Character set (umlauts!) |
<meta name="viewport" content="width=device-width, initial-scale=1"> | Mandatory for responsive pages |
<h1> … <h6> | Headings (outline, not size) |
<p> <strong> <em> <br> | Paragraph, important, emphasized, line break |
<a href="url">Text</a> | Link (external: target="_blank" rel="noopener") |
<img src="a.png" alt="Description"> | Image (alt is mandatory) |
Semantic Structure
<header> <nav> <main> <footer> | Page regions (landmarks); only one <main> |
<article> <section> <aside> | Self-contained content, section, sidebar |
<figure><img><figcaption> | Figure with caption |
<time datetime="2024-05-01"> | Machine-readable date |
<button> <a href> | Action = button, navigation = link |
<div> <span> | Neutral containers (block / inline) |
Forms
<label for="id">Text</label> | Label, linked to the field |
<input type="email" name="mail" required> | Input field with type and required |
type="text|email|password|number|date|tel|url|checkbox|radio|file" | Important input types |
<select><option>A</option></select> | Dropdown list |
<textarea rows="4"></textarea> | Multi-line text |
minlength maxlength min max pattern | Built-in validation |
<fieldset><legend>Group</legend> | Group related fields |
autocomplete="email" | Enable browser autofill |
Tables & Lists
<table><thead><tr><th scope="col"> | Table with header row and column header |
<tbody> <tfoot> <caption> | Body, footer, title of the table |
colspan="2" rowspan="2" | Merge cells |
<ul><li></li></ul> | Bulleted list |
<ol><li></li></ol> | Numbered list |
<dl><dt>Term</dt><dd>Text</dd></dl> | Definition list (key/value) |
Selectors
p .klasse #id * | Element, class, ID, everything |
a b a > b a + b a ~ b | Descendant, child, adjacent sibling, siblings |
[type="text"] [href^="https"] | Attribute selectors (equals, starts with) |
:hover :focus-visible :checked :disabled | State pseudo-classes |
:first-child :nth-child(odd) :not(.x) | Structural pseudo-classes |
:is(h1, h2) :where(a, b) :has(img) | Grouping (:where has specificity 0), parents by content |
::before ::after ::first-letter ::marker | Pseudo-elements |
#a (1,0,0) > .a (0,1,0) > a (0,0,1) | Specificity: ID > class > element |
Box Model & Units
*, *::before, *::after { box-sizing: border-box; } | Standard reset: width incl. padding/border |
margin: 10px 20px; padding: 1rem; | Outer/inner spacing (top/bottom, left/right) |
margin: 0 auto; max-width: 60rem; | Center a block horizontally |
border: 1px solid #ccc; border-radius: 8px; | Border and rounding |
display: block | inline | inline-block | none | Display type |
px % em rem vw vh ch | Units: fixed, relative, root font size, viewport |
overflow: hidden | auto | scroll | Handling overflowing content |
position: relative | absolute | fixed | sticky | Positioning (z-index needs ≠ static) |
Flexbox
display: flex; | Flex container (children become items) |
flex-direction: row | column; | Set the main axis |
justify-content: center | space-between | flex-end; | Alignment on the main axis |
align-items: center | stretch | flex-start; | Alignment on the cross axis |
gap: 12px; flex-wrap: wrap; | Spacing, wrapping |
flex: 1; flex: 0 0 200px; | Item grows / fixed base width |
margin-left: auto; | Push an item to the end |
Grid
display: grid; grid-template-columns: repeat(3, 1fr); | Three equal-width columns |
grid-template-columns: 200px 1fr; | Fixed sidebar, flexible rest |
repeat(auto-fit, minmax(200px, 1fr)) | Automatically responsive grid |
gap: 16px; | Row and column spacing |
grid-column: span 2; | Item spanning two columns |
grid-template-areas: "kopf kopf" "menue inhalt"; | Layout as an area sketch |
place-items: center; | Center content in cells |
Responsive & Variables
@media (min-width: 768px) { … } | From 768 px up (mobile first) |
@media (prefers-color-scheme: dark) | System dark mode |
@media (prefers-reduced-motion: reduce) | Less motion requested |
@container (min-width: 400px) { … } | Depends on the container instead of the window |
:root { --farbe: #e34f26; } color: var(--farbe, red); | Define and use a custom property (with fallback) |
calc(100% - 2rem) min(100%, 60rem) clamp(1rem, 2vw, 2rem) | CSS functions for fluid sizes |
img { max-width: 100%; height: auto; } | Images scale along |
Animation & Accessibility
transition: transform .25s ease; | Smooth transition between states |
@keyframes x { to { transform: rotate(360deg); } } animation: x 1s linear infinite; | Keyframe animation |
transform: translate(10px, 0) scale(1.1) rotate(5deg); | Performant transformations |
:focus-visible { outline: 3px solid orange; } | Visible keyboard focus |
alt="" (decorative) alt="Description" | Alternative text for images |
aria-label aria-describedby aria-expanded | Additional ARIA info when HTML is not enough |
role="alert" aria-live="polite" | Announce dynamic messages |
Contrast ≥ 4.5:1 (text), ≥ 3:1 (large text, UI) | WCAG AA minimum contrasts |