Web cheatsheet
The HTML, CSS and JavaScript you reach for most, from page structure and layout to the DOM, events and fetch.
HTML structure
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>My page</title></head>
<body>...</body>Every page starts like this. Only body shows up.
<h1>Title</h1> <h2>Section</h2> <p>Text</p>Headings run h1 (one per page) to h6; p is a paragraph.
<a href="/shop/">Shop</a>
<img src="cat.jpg" alt="A cat on a skateboard">Links need href. Images need src and alt text.
2 more: semantic tags, lists
<header> <nav> <main> <section> <footer>Say what each part is. Helps screen readers and search.
<ul><li>One</li><li>Two</li></ul>A bulleted list. Use ol for a numbered one.
Forms
<form id="signup">
<label for="user">Username</label>
<input id="user" name="user" required>
</form>for matches id, so clicking the label focuses the box.
<input type="email"> <input type="number" min="1" max="10">type sets the phone keyboard and built-in checks.
2 more: dropdowns, submit buttons
<select id="team"><option>Red</option><option>Blue</option></select>A dropdown. Read the choice with select.value.
<button type="submit">Join</button>Sends the form, which reloads the page unless you stop it.
CSS selectors
p { } .card { } #score { }Element name, class (dot) and id (hash).
nav a { } .card > h2 { }Space: anywhere inside. >: direct child only.
2 more: hover and focus states, combining selectors
button:hover { } input:focus { } li:nth-child(2) { }Pick elements in a state or position.
.btn.primary { } h1, h2 { }No space: both classes on one element. Comma: several selectors.
Box model
padding: 16px; border: 2px solid #333; margin: 24px;Space inside the border, the border, then space outside.
* { box-sizing: border-box; }Width includes padding and border. Put it first.
2 more: two-value margins, centring
margin: 10px 20px;Two values: top and bottom first, then left and right.
margin: 0 auto; max-width: 640px;Centres a block and caps its width on big screens.
Flexbox and grid
.row { display: flex; gap: 12px; }Children sit in a row with space between them.
justify-content: space-between; align-items: center;Spread items along the row; line them up across it.
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }As many columns of 160px or wider as fit, filled in order.
2 more: columns and wrapping, narrow screens
flex-direction: column; flex-wrap: wrap;Stack items instead, or let them wrap onto the next line.
@media (max-width: 600px) { .grid { grid-template-columns: 1fr; } }On narrow screens, switch to one column.
Values and variables
const name = "Zara";
let score = 0;const names never change; let names can.
`Level ${level}: ${score} pts`A template literal puts values into text.
score === 100; score !== 0;Always compare with three equals signs.
2 more: typeof, text and numbers
typeof 42; typeof "42"; typeof true;Gives "number", "string" and "boolean".
"5" + 3; Number("5") + 3;The first gives "53" (glued), the second 8.
Arrays
const songs = ["Neon Rain", "Late Bus"];
songs[0]; songs.length; songs.push("Paper Planes");Index from 0, count, add to the end.
for (const s of songs) { console.log(s); }Loop over every value.
const loud = songs.map((s) => s.toUpperCase());
const short = songs.filter((s) => s.length < 9);map changes a copy; filter keeps what passes.
3 more: searching, reduce, sorting
songs.includes("Late Bus"); songs.indexOf("Late Bus");Is it there? Where is it? (-1 means not found.)
const total = scores.reduce((sum, n) => sum + n, 0);Boil an array down to one value.
players.sort((a, b) => b.points - a.points);Sort by a number, highest first. sort changes the array.
Locked
DOM and events
Code 6 rows
Async and fetch
Code 6 rows
Modules
Code 4 rows