← Structure and style a page

HTML and CSS reference

Tags and CSS you can click to insert, with what each one does.

Print a cheat sheet (the whole course or one unit, with a picture of what each command draws)

Page structure

<h1>Heading</h1>

Example

<h1>Robotics Club</h1>
<h1> heading

The biggest heading. Use it once for the page title.

What the example draws
The biggest heading

id: h1-heading

<h2>Subheading</h2>

Example

<h1>Robotics Club</h1>
<h2>This term</h2>
<h2> subheading

A smaller heading for a section.

What the example draws
A smaller heading under the title

id: h2-subheading

<p>Some text</p>

Example

<p>We meet on Tuesdays.</p>
<p>Everyone is welcome.</p>
<p> paragraph

A paragraph of text.

What the example draws
Two paragraphs, with a gap between

id: p-paragraph

<div>
  Content
</div>
<div> box

A container that groups things together, so you can style them as one.

id: div-box

<ul>
  <li>First</li>
  <li>Second</li>
</ul>

Example

<ul>
  <li>Apples</li>
  <li>Pears</li>
  <li>Plums</li>
</ul>
<ul> list

A bulleted list. Each item is an <li>.

What the example draws
A bullet for each <li>

id: ul-list

<ol>
  <li>First</li>
  <li>Second</li>
</ol>

Example

<ol>
  <li>Plug in</li>
  <li>Switch on</li>
  <li>Press start</li>
</ol>
<ol> numbered list

A numbered list.

What the example draws
The browser numbers the items

id: ol-numbered-list

<!-- A note to yourself -->

Example

<p>You can see this.</p>
<!-- You cannot see this. -->
comment

A note that the browser ignores.

What the example draws
The comment does not show

id: comment

Content

<a href="https://example.com">Link text</a>

Example

<p>Read the <a href="https://example.com">club rules</a>.</p>
<a> link

A link. The address goes in href; the words between the tags are what people click.

What the example draws
The link text is blue and underlined

id: a-link

<img src="trophy.svg" alt="A gold trophy">

Example

<img src="medal.svg" alt="A silver medal" width="100">
<p>Second place</p>
<img> image

A picture. src is the picture file; alt describes it for people who cannot see it. There is no closing tag.

What the example draws
The picture, 100 pixels wide

id: img-image

<strong>important</strong>

Example

<p>Bring a <strong>hat</strong> tomorrow.</p>
<strong> bold

Make text stand out as important (usually bold).

What the example draws
Only the word inside is bold

id: strong-bold

<em>emphasis</em>

Example

<p>Do <em>not</em> run inside.</p>
<em> emphasis

Emphasise text (usually italic).

What the example draws
Only the word inside is italic

id: em-emphasis

<button>Click me</button>

Example

<button>Start</button>
<button>

A button.

What the example draws
A button (clicking does nothing yet)

id: button

<br>

Example

<p>Line one<br>Line two</p>
<br> line break

Start a new line.

What the example draws
A new line inside one paragraph

id: br-line-break

<hr>

Example

<p>Above</p>
<hr>
<p>Below</p>
<hr> line

Draw a line across the page.

What the example draws
A line across the page

id: hr-line

Table

<table>
  <tr>
    <th>Name</th>
    <th>Score</th>
  </tr>
  <tr>
    <td>Sam</td>
    <td>10</td>
  </tr>
</table>

Example

<table>
  <tr>
    <th>Team</th>
    <th>Points</th>
  </tr>
  <tr>
    <td>Owls</td>
    <td>12</td>
  </tr>
  <tr>
    <td>Hawks</td>
    <td>9</td>
  </tr>
</table>
<table>

A table: <tr> is a row, <th> a heading cell, <td> a normal cell.

What the example draws
Headings in bold, one row per <tr>

id: table

Page sections

<header>
  <h1>Site name</h1>
</header>

Example

<header>
  <h1>Chess club</h1>
</header>
<p>Page content</p>
<header>

The top part of a page: usually the title and the menu. Looks like a div; the name tells browsers and screen readers what it is.

What the example draws
It looks like any block: the name is the difference

id: header

<nav>
  <a href="#">Home</a>
</nav>

Example

<nav>
  <a href="#">Home</a>
  <a href="#">Games</a>
</nav>
<nav>

The main menu of links.

What the example draws
A group of menu links

id: nav

<main>
  The page's main content
</main>

Example

<main>
  <h2>This week</h2>
  <p>Practice on Wednesday.</p>
</main>
<main>

The main content of the page. One per page.

What the example draws
The main content

id: main

<section>
  <h2>Part</h2>
</section>

Example

<section>
  <h2>Results</h2>
  <p>We won 3 games.</p>
</section>
<section>

One part of the page, usually with its own heading.

What the example draws
One part of a page with its heading

id: section

<footer>
  Contact and small print
</footer>

Example

<p>Page content</p>
<footer>
  <p>Contact: chess@example.com</p>
</footer>
<footer>

The bottom part of a page: contact details, small print.

What the example draws
The bottom of the page

id: footer

<header>
  <h1>Site name</h1>
  <nav>
    <ul>
      <li><a href="https://example.com">Home</a></li>
    </ul>
  </nav>
</header>
<main>
  <section>
    <h2>Section title</h2>
    <p>Some text.</p>
  </section>
</main>
<footer>
  <p>Contact us</p>
</footer>

Example

<header>
  <h1>Chess club</h1>
  <nav>
    <ul>
      <li><a href="https://example.com">Home</a></li>
      <li><a href="https://example.com/rules">Rules</a></li>
    </ul>
  </nav>
</header>
<main>
  <section>
    <h2>When we meet</h2>
    <p>Wednesdays at lunch.</p>
  </section>
</main>
<footer>
  <p>Contact the teacher in charge.</p>
</footer>
page parts

The parts of a page, named: header, nav, main with its sections, and footer. They look like plain blocks; the names are for screen readers and search engines.

What the example draws
Each part is a plain block: the tag names are the difference

id: page-parts

<a href="#results">Link text</a>

<h2 id="results">Section title</h2>

Example

<nav>
  <ul>
    <li><a href="#results">Results</a></li>
    <li><a href="#join">Join</a></li>
  </ul>
</nav>
<h2 id="results">Results</h2>
<p>The Owls won.</p>
<h2 id="join">Join</h2>
<p>Come on Wednesday.</p>
jump link

A link to a place on the same page. id names the place; href starts with # and the same name.

What the example draws
Clicking a link scrolls the page to its heading

id: jump-link

Forms

<form>
  fields
  <button>Send</button>
</form>

Example

<form>
  <label for="town">Town</label>
  <input id="town" type="text">
  <button>Go</button>
</form>
<form>

Holds the fields and the button that sends them. On its own, a form sends nowhere: code behind it does the work.

What the example draws
A field and a button

id: form

<label for="name">Name</label>
<input id="name" type="text">

Example

<label for="age">Age</label>
<input id="age" type="number">
label and input

An input is a box to type in. Its label says what to type; for and id must match, so clicking the label puts the cursor in the box.

What the example draws
A labelled box for a number

id: label-and-input

<select id="house">
  <option>Owls</option>
  <option>Hawks</option>
</select>

Example

<label for="size">Size</label>
<select id="size">
  <option>Small</option>
  <option>Large</option>
</select>
<select> drop-down

A drop-down list: one option to choose.

What the example draws
A drop-down showing its first option

id: select-drop-down

CSS

<style>
  h1 {
    color: red;
  }
</style>

Example

<style>
  h2 {
    color: purple;
  }
</style>
<h2>Purple heading</h2>
<p>Normal text</p>
<style> block

Where CSS lives. Each rule is: selector { property: value; }

What the example draws
The rule styles every <h2>

id: style-block

p {
  color: blue;
}

Example

<style>
  li {
    color: teal;
  }
</style>
<ul>
  <li>One</li>
  <li>Two</li>
</ul>
rule

Style every element matching the selector.

What the example draws
Every <li> is teal

id: rule

.note {
  color: red;
}

Example

<style>
  .tip {
    font-style: italic;
  }
</style>
<p>A normal paragraph.</p>
<p class="tip">A paragraph with class="tip".</p>
class selector

Style only the elements you mark with class="note" in the HTML. The dot is only in the CSS.

What the example draws
Only the paragraph with the class changes

id: class-selector

h1, h2 {
  color: navy;
}

Example

<style>
  h1, li {
    color: purple;
  }
</style>
<h1>Title</h1>
<ul>
  <li>An item</li>
</ul>
<p>A paragraph</p>
several selectors

One rule for several kinds of element: list the selectors with commas.

What the example draws
The heading and the list item change; the paragraph does not

id: several-selectors

color: red;

Example

<style>
  em {
    color: crimson;
  }
</style>
<p>A <em>red</em> word</p>
color

The text colour.

What the example draws
Text colour

id: color

background-color: lightyellow;

Example

<style>
  h2 {
    background-color: lightyellow;
  }
</style>
<h2>Notice</h2>
background-color

The background colour.

What the example draws
The colour behind the text

id: background-color

font-size: 20px;

Example

<style>
  h2 {
    font-size: 40px;
  }
</style>
<h2>Big</h2>
<p>Normal</p>
font-size

How big the text is.

What the example draws
A bigger heading

id: font-size

font-family: sans-serif;

Example

<style>
  p {
    font-family: monospace;
  }
</style>
<p>Code font</p>
font-family

Which font to use.

What the example draws
A different font

id: font-family

text-align: center;

Example

<style>
  h2 {
    text-align: center;
  }
</style>
<h2>Centred</h2>
<p>Left</p>
text-align

Align text: left, center or right.

What the example draws
The heading moves to the middle

id: text-align

display: flex;

Example

<style>
  .row {
    display: flex;
  }
</style>
<div class="row">
  <p>One</p>
  <p>Two</p>
  <p>Three</p>
</div>
display: flex

Put it on the box that holds the items (the parent): its children line up in a row.

What the example draws
The three paragraphs sit side by side

id: display-flex

gap: 16px;

Example

<style>
  .row {
    display: flex;
    gap: 40px;
  }
</style>
<div class="row">
  <p>One</p>
  <p>Two</p>
  <p>Three</p>
</div>
gap

Space between the items in a flex row (on the parent, with display: flex).

What the example draws
40 pixels between the items

id: gap

flex-wrap: wrap;

Example

<style>
  .row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 180px;
  }
</style>
<div class="row">
  <img src="star.svg" alt="Star" width="50">
  <img src="star.svg" alt="Star" width="50">
  <img src="star.svg" alt="Star" width="50">
  <img src="star.svg" alt="Star" width="50">
</div>
flex-wrap

Let items move to the next line when the row is full.

What the example draws
The fourth star wraps to the next line

id: flex-wrap

justify-content: center;

Example

<style>
  .row {
    display: flex;
    justify-content: space-between;
    width: 220px;
    border: 1px solid gray;
  }
</style>
<div class="row">
  <p>Left</p>
  <p>Right</p>
</div>
justify-content

Where the items sit along the row: flex-start, center, flex-end or space-between.

What the example draws
space-between pushes the items to the ends

id: justify-content

list-style: none;

Example

<style>
  ol {
    list-style: none;
  }
</style>
<ol>
  <li>First</li>
  <li>Second</li>
</ol>
list-style

Remove the bullets or numbers from a list: menus are lists without bullets.

What the example draws
A list with no numbers

id: list-style

display: grid;

Example

<style>
  .grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  p {
    background-color: lavender;
  }
</style>
<div class="grid">
  <p>A</p>
  <p>B</p>
  <p>C</p>
  <p>D</p>
</div>
display: grid

Put it on the box that holds the items: its children are placed in rows and columns (with grid-template-columns).

What the example draws
Four boxes in two columns

id: display-grid

grid-template-columns: 200px 1fr;

Example

<style>
  .grid {
    display: grid;
    grid-template-columns: 80px 1fr;
    gap: 8px;
  }
  .cell {
    background-color: lightblue;
  }
</style>
<div class="grid">
  <div class="cell">80px</div>
  <div class="cell">The rest</div>
</div>
grid-template-columns

The width of each column, in order. 1fr means one share of the space left; repeat(3, 1fr) makes three equal columns.

What the example draws
A fixed column, then one that takes the rest

id: grid-template-columns

width: 200px;

Example

<style>
  div {
    width: 120px;
    background-color: lightblue;
  }
</style>
<div>A box 120 pixels wide</div>
width

How wide the content is. Padding and border are added on top of it.

What the example draws
The box is 120 pixels wide

id: width

padding: 10px;

Example

<style>
  p {
    background-color: lightblue;
    padding: 20px;
  }
</style>
<p>Room inside</p>
padding

Space inside an element, between its edge and its content.

What the example draws
Space between the edge and the text

id: padding

margin: 10px;

Example

<style>
  p {
    border: 1px solid gray;
    margin: 20px;
  }
</style>
<p>One</p>
<p>Two</p>
margin

Space outside an element, between it and its neighbours.

What the example draws
Space around each box

id: margin

border: 2px solid black;

Example

<style>
  p {
    border: 2px solid black;
  }
</style>
<p>Boxed</p>
border

A line around an element: thickness, style, colour.

What the example draws
A line around the paragraph

id: border

nav a {
  color: white;
  text-decoration: none;
}

Example

<style>
  nav a {
    color: crimson;
    text-decoration: none;
  }
</style>
<nav>
  <ul>
    <li><a href="https://example.com">Home</a></li>
    <li><a href="https://example.com/rules">Rules</a></li>
  </ul>
</nav>
<p>A <a href="https://example.com">link in the text</a> keeps its look.</p>
menu links

Style only the links inside the nav. The space means inside: nav a is every a within a nav.

What the example draws
The menu links change; the link in the text does not

id: menu-links

.card {
  width: 200px;
  padding: 16px;
  border: 2px solid navy;
  margin: 12px;
}

Example

<style>
  .card {
    width: 200px;
    padding: 16px;
    border: 2px solid navy;
    margin: 12px;
  }
</style>
<section class="card">
  <h2>Chess</h2>
  <p>Wednesdays at lunch.</p>
</section>
card box

A box with room inside (padding), a line around it (border) and space outside it (margin). Width sets the content only.

What the example draws
A card 200px wide inside, 244px with its padding and border

id: card-box

.row {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;
}

Example

<style>
  .bar {
    display: flex;
    gap: 16px;
    justify-content: space-between;
    align-items: center;
  }
</style>
<section class="bar">
  <h2>Chess club</h2>
  <p>Join us</p>
</section>
flex row

A row of items. gap is the space between them, justify-content places them along the row, align-items lines them up across it.

What the example draws
The title on the left and the text on the right, lined up across

id: flex-row

.gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
.item {
  flex: 1;
  min-width: 150px;
}

Example

<style>
  .gallery {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
  }
  .item {
    flex: 1;
    min-width: 150px;
    background-color: lightyellow;
  }
</style>
<section class="gallery">
  <p class="item">One</p>
  <p class="item">Two</p>
  <p class="item">Three</p>
  <p class="item">Four</p>
</section>
wrapping row

A row that moves items to a new line when there is no room. flex: 1 lets items share the space and min-width stops them getting too narrow.

What the example draws
Items share the row, then wrap when the preview is narrow

id: wrapping-row

<style>
  .card {
    padding: 16px;
    border: 1px solid gray;
  }
</style>
<section class="card">
  <h2>Title</h2>
  <p>Some text.</p>
</section>

Example

<style>
  .card {
    padding: 16px;
    border: 1px solid gray;
  }
</style>
<section class="card">
  <h2>Chess</h2>
  <p>Wednesdays.</p>
</section>
<section class="card">
  <h2>Art</h2>
  <p>Thursdays.</p>
</section>
card component

A chunk of HTML with a class. Copy the section for each card; one rule styles every copy.

What the example draws
Two cards, one rule

id: card-component

JavaScript (recipes)

document.querySelector('#go').addEventListener('click', function () {
  what to do
});
on click

When the element with id="go" is clicked, run the lines inside. # means id, as . means class.

id: on-click

document.querySelector('#message').textContent = 'Go!';
change the text

Replace the words inside the element with id="message".

id: change-the-text

const name = document.querySelector('#name').value;
read a box

Read what someone typed into the box with id="name".

id: read-a-box

Bootstrap

<link rel="stylesheet" href="bootstrap.min.css">
Bootstrap link

Loads Bootstrap's stylesheet: a big CSS file someone else wrote. Put it first, above your own <style>. On your own site, download it from getbootstrap.com and put it beside your page.

id: bootstrap-link

<div class="container">
  content
</div>
container

A centred column with space at the sides. Most Bootstrap pages put everything inside one.

id: container

<button class="btn btn-primary">Join</button>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<button class="btn btn-primary">Save</button>
<button class="btn btn-outline-secondary">Cancel</button>
<button class="btn btn-danger btn-sm">Delete</button>
btn button

btn gives the button shape; a second class picks the colour: btn-primary, btn-secondary, btn-success, btn-danger, btn-warning, or btn-outline-primary for an outline. Add btn-lg or btn-sm for the size. It needs both btn and a colour.

What the example draws
Three buttons: blue, outlined and a small red one

id: btn-button

<div class="alert alert-warning">Message</div>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<div class="alert alert-info">The library is open at lunch.</div>
alert

A coloured message box. alert gives the box; alert-primary, alert-success, alert-danger, alert-warning or alert-info picks the colour.

What the example draws
A light blue message box

id: alert

<p class="mt-3 p-2 text-center">Text</p>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<p class="lead text-center">Welcome!</p>
<p class="mt-4 p-3 border">A box with space above and padding inside.</p>
spacing and text classes

m is margin and p is padding; then t, b, s, e (top, bottom, start, end), x (both sides) or y (top and bottom), then a size from 0 to 5: mt-3 is a space above. text-center centres text; lead makes a larger opening paragraph; fw-bold makes it bold.

What the example draws
A large centred line, then a padded box lower down

id: spacing-and-text-classes

<nav class="navbar navbar-expand bg-body-tertiary">
  <div class="container">
    <a class="navbar-brand" href="#">Club name</a>
    <ul class="navbar-nav">
      <li class="nav-item"><a class="nav-link" href="#part">Link</a></li>
    </ul>
  </div>
</nav>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<nav class="navbar navbar-expand bg-body-tertiary">
  <div class="container">
    <a class="navbar-brand" href="#">Library</a>
    <ul class="navbar-nav">
      <li class="nav-item"><a class="nav-link" href="#">Books</a></li>
      <li class="nav-item"><a class="nav-link" href="#">Hours</a></li>
    </ul>
  </div>
</nav>
navbar

The bar across the top: the brand (your name) on the left, then the menu. navbar-expand keeps the menu in a row (folding it into a button on phones needs Bootstrap's JavaScript).

What the example draws
A grey bar with a name and two menu links

id: navbar

<div class="card">
  <div class="card-body">
    <h5 class="card-title">Title</h5>
    <p class="card-text">Text</p>
  </div>
</div>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<div class="card m-2">
  <div class="card-body">
    <h5 class="card-title">Robotics</h5>
    <p class="card-text">Fridays in the lab.</p>
  </div>
</div>
card

A box with a border and rounded corners. The card-body inside gives it padding; card-title and card-text style what is in it.

What the example draws
A bordered box with a title and a line of text

id: card

<div class="row">
  <div class="col">first</div>
  <div class="col">second</div>
</div>

Example

<link rel="stylesheet" href="bootstrap.min.css">

<div class="container">
  <div class="row">
    <div class="col border">One</div>
    <div class="col border">Two</div>
    <div class="col border">Three</div>
  </div>
</div>
row and col

Bootstrap's grid: a row lines up the cols inside it, and each col takes an equal share. col-4 and col-8 split the row 4 to 8 (a row is 12 parts). Put a row inside a container.

What the example draws
Three boxes sharing a line equally

id: row-and-col

<label for="email" class="form-label">Email</label>
<input id="email" type="email" class="form-control">

Example

<link rel="stylesheet" href="bootstrap.min.css">

<div class="m-2">
  <label for="town" class="form-label">Town</label>
  <input id="town" type="text" class="form-control">
</div>
form-control

Bootstrap's look for a form: form-label on each label, form-control on each box, form-select on a drop-down. The for and id still link them.

What the example draws
A label above a full-width box

id: form-control

<table class="table table-striped">
  rows
</table>
table class

Bootstrap's look for a table: lines between rows and padding in the cells. table-striped shades every other row; table-bordered adds borders.

id: table-class

Other CSS frameworks

<link rel="stylesheet" href="bulma.min.css">
<button class="button is-primary">Join</button>

Example

<link rel="stylesheet" href="bulma.min.css">

<div class="p-3">
  <button class="button is-primary">Send</button>
  <button class="button is-danger is-light">Delete</button>
</div>
Bulma button

Bulma works like Bootstrap with different names: button for the shape, is-primary, is-success or is-danger for the colour, is-large for the size.

What the example draws
Two Bulma buttons

id: bulma-button

<script src="tailwind.js"></script>
<button class="bg-blue-600 text-white px-4 py-2 rounded-lg">Join</button>

Example

<script src="tailwind.js"></script>

<p class="m-3 p-3 bg-yellow-100 text-lg font-bold rounded">Sale today!</p>
Tailwind utility classes

Tailwind has one class per CSS property: bg-blue-600 is the background, text-white the text colour, px-4 and py-2 the padding, rounded-lg the corners. Here tailwind.js writes the CSS as the page loads (for trying things); real sites use a build tool.

What the example draws
A yellow box with large bold text

id: tailwind-utility-classes

<link rel="stylesheet" href="daisyui.css">
<script src="tailwind.js"></script>
<button class="btn btn-primary">Join</button>

Example

<link rel="stylesheet" href="daisyui.css">
<script src="tailwind.js"></script>

<div class="p-3">
  <button class="btn btn-secondary">Play</button>
</div>
daisyUI button

daisyUI puts Bootstrap-style names (btn, card, alert) on top of Tailwind, so you can mix whole parts with Tailwind's one-property classes.

What the example draws
A daisyUI button

id: daisyui-button

<link rel="stylesheet" href="pico.classless.min.css">
<main>
  plain tags
</main>

Example

<link rel="stylesheet" href="pico.classless.min.css">

<main>
  <h3>Hello</h3>
  <button>OK</button>
</main>
Pico (no classes)

Pico's classless version styles plain tags: a page with headings, paragraphs, forms and buttons looks tidy with no class names at all. Put the page in a <main>.

What the example draws
A heading and a button, styled with no classes

id: pico-no-classes

Vue

<div id="app">
  the page
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref, computed } = Vue;
  createApp({
    setup() {
      data and functions
      return { names };
    }
  }).mount("#app");
</script>

Example

<div id="app">
  <p>Today: {{ temp }} °C</p>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const temp = ref(21);
      return { temp };
    }
  }).mount("#app");
</script>
Vue app

Vue looks after everything inside the div with id app. setup makes the data and functions, and returns the names the page can use. vue.js sits beside the page.

What the example draws
A sentence showing the data: Today: 21 °C

id: vue-app

const score = ref(0);
ref data

Data that Vue watches: when it changes, the page follows. Return it from setup. In the script its value is score.value; on the page, just score.

id: ref-data

{{ score }}
show data {{ }}

Shows a value from setup on the page, and keeps it up to date. Any short expression works: {{ score * 2 }}.

id: show-data

<button @click="addPoint">Add</button>
@click event

Runs a function from setup when the button is clicked. A short change works too: @click="score++".

id: click-event

<li v-for="name in players">{{ name }}</li>

Example

<ul id="app">
  <li v-for="fruit in fruits">{{ fruit }}</li>
</ul>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const fruits = ref(["Apple", "Pear", "Plum"]);
      return { fruits };
    }
  }).mount("#app");
</script>
v-for

Makes this tag once for each item in the list. Put it on the tag you want more of (the li, not the ul).

What the example draws
A list with one item for each fruit

id: v-for

<p v-if="players.length === 0">No players yet.</p>
v-if

Shows the tag only while the condition is true. v-else on the next tag shows when it is false.

id: v-if

<input v-model="newName">
v-model

Joins a box to a ref, both ways: typing changes the data, and changing the data changes the box. On a tick box it is true or false.

id: v-model

function addPoint() {
  score.value++;
}
function in setup

A function the page can run (return it from setup). In the script, a ref's value is in .value.

id: function-in-setup

const total = computed(() => adults.value * 12);
computed

A value worked out from other data. Vue works it out again whenever that data changes, so it never goes stale. Never set it yourself.

id: computed

app.component("player-card", {
  props: ["name"],
  template: `<p>{{ name }}</p>`
});
component and props

A tag of your own, made once and used many times: <player-card :name="p.name"></player-card>. The colon passes a value. Make the app with const app = createApp(...), add components, then app.mount("#app").

id: component-and-props

Frameworks

<div id="app"></div>
<script src="preact.js"></script>
<script>
  const { html, render, useState } = htmPreact;
  function App() {
    state and functions
    return html`the page`;
  }
  render(html`<${App} />`, document.querySelector("#app"));
</script>
Preact app

Preact works like React. A component is a function that returns its page, written in html`…` (htm). Preact runs the function again after every change.

id: preact-app

const [stars, setStars] = useState([]);
useState

Preact and React state: the value, and a function to change it. Always give it a new value: setStars([...stars, item]), never stars.push.

id: usestate

<textarea data-svelte hidden>
  <script>
    state and functions
  </script>
  the page
</textarea>
<script src="svelte.js"></script>

Example

<textarea data-svelte hidden>
  <script>
    let name = $state("world");
  </script>
  <p>Hello, {name}!</p>
</textarea>
<script src="svelte.js"></script>
Svelte component

Here a Svelte component sits in a hidden textarea and svelte.js compiles it in the page. In a real project it is its own file, App.svelte.

What the example draws
Hello, world!

id: svelte-component

let count = $state(0);
let double = $derived(count * 2);
$state and $derived

Svelte's data that changes, and a value worked out from it. Change $state data directly (count++, list.push). Show it with {count}; choose with {#if …} … {/if}.

id: state-and-derived

Fetch and JSON

const response = await fetch("weather.json");
const data = await response.json();

Example

<p id="out">Loading…</p>
<script>
  async function load() {
    const response = await fetch("weather.json");
    const data = await response.json();
    document.querySelector("#out").textContent = "Days: " + data.daily.time.length;
  }
  load();
</script>
fetch JSON

Asks for a file or web address, waits for the reply, then turns it into data (objects and arrays). Both lines need await, inside an async function.

What the example draws
Loading… becomes Days: 7 once the file has arrived

id: fetch-json

async function load() {
  … await …
}
load();
async function

A function that may wait for slow things with await. Call it like any function; the rest of the page keeps going while it waits.

id: async-function

try {
  what might fail
} catch (error) {
  plan B
}
try and catch

Runs the lines in try; if one fails (no network, a blocked site), jumps to catch instead of stopping with an error.

id: try-and-catch

A page for everyone, online

<img src="trophy.svg" alt="A gold trophy">
alt text

Says what a picture shows, for anyone who can't see it (screen readers read it aloud). Use alt="" for a picture that is only decoration.

id: alt-text

<h1>Page title</h1>
<h2>A part of the page</h2>
<h3>A part of that part</h3>
heading outline

Headings are the page's outline: one h1, then h2 for each part, h3 inside those. Never pick a level for its size; set the size with CSS.

id: heading-outline

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My page</title>
</head>
<body>
  the page
</body>
</html>
whole page

What a page file needs around its content: the language, the text encoding, a phone-friendly width and a title for the tab. The Download button adds it for you.

id: whole-page

<img src="trophy.svg" alt="…">
<link rel="stylesheet" href="style.css">
relative path

A file name with no folder means "beside this page"; images/trophy.svg means "in the images folder beside this page". Never a path from your own computer (C:/Users/…).

id: relative-path