HTML Cheat Sheet
6 groups · 18 topics · Updated
HTML says what is on the page. Looks come from CSS and behaviour from JavaScript.
Basics
How HTML is written and what every page needs.
How a tag works
<a href="/about/">About us</a>| Part | Meaning |
|---|---|
a |
Tag name: the kind of element |
href="/about/" |
Attribute: a setting |
About us |
Content: what the user sees |
</a> |
Closing tag |
No closing tag: <img>, <br>, <hr>, <input>, <meta>, <link>.
Page skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width,
initial-scale=1" />
<title>Page title</title>
<link rel="stylesheet"
href="styles.css" />
</head>
<body>
<h1>Hello, world</h1>
<script src="app.js"></script>
</body>
</html><head>: information about the page, not shown.<body>: everything the user sees.
Inside the head
<title>Shown in the tab</title>
<meta name="description"
content="Shown in search results" />
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script src="app.js" defer></script>
<style>
p { color: gray; }
</style>defer runs the script after the page has been read.
Text and links
Words, lists and getting from page to page.
Text
<h1>Main title</h1> <!-- one per page -->
<h2>Section</h2> <!-- h1 down to h6 -->
<p>A paragraph</p>
<strong>Important</strong>
<em>Emphasised</em>
<mark>Highlighted</mark>
<small>Fine print</small>
<br /> <!-- line break -->
<hr /> <!-- divider line -->
<blockquote>A quotation</blockquote>
<code>short code</code>
<pre>keeps spaces
and line breaks</pre>
H<sub>2</sub>O and x<sup>2</sup>Links
<a href="https://example.com">Other site</a>
<a href="/about/">Page on this site</a>
<a href="#contact">Jump to id="contact"</a>
<a href="mailto:hi@example.com">Email</a>
<a href="tel:+911234567890">Call</a>
<a href="file.pdf" download>Download</a>
<!-- open in a new tab -->
<a href="https://example.com"
target="_blank" rel="noopener">New tab</a>Lists
<ul> <!-- bullets -->
<li>Milk</li>
<li>Bread</li>
</ul>
<ol> <!-- numbers -->
<li>Boil water</li>
<li>Add tea</li>
</ol>
<dl> <!-- term + meaning -->
<dt>HTML</dt>
<dd>Page structure</dd>
</dl>Special characters
| Write | To show |
|---|---|
< |
< |
> |
> |
& |
& |
" |
" |
|
A space that never wraps |
© |
© |
<!-- A comment: not shown on the page -->Media
Pictures, sound, video and embedded pages.
Images
<img src="photo.jpg" alt="A red bicycle"
width="600" height="400" loading="lazy" />
<figure>
<img src="chart.png" alt="Sales rising" />
<figcaption>Sales this year</figcaption>
</figure>altdescribes the image. Always write it.widthandheightstop the page jumping.loading="lazy"loads it only when needed.
Audio, video and embeds
<video src="clip.mp4" controls
poster="preview.jpg" width="640"></video>
<audio src="song.mp3" controls></audio>
<iframe src="https://example.com/map"
title="Office map"
width="600" height="400"></iframe>Also: autoplay, muted, loop. Autoplay only works when the video is muted.
Tables and forms
Showing data and collecting input.
Tables
<table>
<caption>Scores</caption>
<thead>
<tr>
<th>Name</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>Asha</td>
<td>92</td>
</tr>
</tbody>
</table><tr>row,<th>heading cell,<td>data cell.colspan="2"orrowspan="2"stretches a cell.
Forms
<form action="/signup" method="post">
<label for="email">Email</label>
<input type="email" id="email"
name="email" required />
<select name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
<textarea name="note" rows="3"></textarea>
<label>
<input type="checkbox" name="news" />
Send me news
</label>
<button type="submit">Sign up</button>
</form>Match the label's for with the input's id.
Input types
type |
You get |
|---|---|
text |
One line of text |
email |
Text checked as an email |
password |
Text shown as dots |
number |
Numbers only |
tel |
Phone number keypad |
url |
A web address |
search |
A search box |
date, time |
A date or time picker |
checkbox |
Tick any number |
radio |
Choose one per name |
file |
A file picker |
range |
A slider |
color |
A colour picker |
hidden |
Sent but not shown |
Input attributes
| Attribute | Effect |
|---|---|
name |
Key the value is sent under |
value |
Starting value |
placeholder |
Grey hint text |
required |
Must be filled in |
disabled |
Cannot be used or sent |
readonly |
Can be read, not changed |
min, max, step |
Number and date limits |
maxlength |
Character limit |
pattern |
Rule the text must match |
autocomplete |
Let the browser fill it |
checked |
Start ticked |
Page structure
Giving each part of the page a meaning.
Layout tags
<header>Logo and title</header>
<nav>Menu links</nav>
<main> <!-- one per page -->
<article> <!-- stands alone -->
<section>Part of it</section>
</article>
<aside>Related links</aside>
</main>
<footer>Copyright</footer>They look like a <div>, but search engines and screen readers understand them.
div and span
<div class="card">
<p>Price: <span class="red">499</span></p>
</div>| Tag | Behaviour |
|---|---|
<div> |
Block: new line, full width |
<span> |
Inline: stays in the text line |
- Block:
<p>,<h1>,<ul>,<section>. - Inline:
<a>,<strong>,<em>,<img>.
Attributes for any tag
| Attribute | Effect |
|---|---|
id |
Unique name on the page |
class |
Shared name, for CSS |
style |
CSS on this element (avoid) |
title |
Tooltip text |
hidden |
Hides the element |
lang |
Language of the content |
data-* |
Your own data for JavaScript |
tabindex |
Reachable with the Tab key |
Good habits
What makes a page work for everyone.
Accessibility
- Every image has
alttext. - Every input has a
<label>. <button>for actions,<a>for navigation.- Headings go in order:
h1,h2,h3. - Set the language:
<html lang="en">. - Link text makes sense alone, not "click here".
Common mistakes
| Mistake | Fix |
|---|---|
| Tag never closed | Add the closing tag |
| Tags closed in the wrong order | Close the inner one first |
Same id used twice |
Use class for groups |
No alt on an image |
Always add it |
<br> used for spacing |
Use CSS margin |
<div> for everything |
Use layout tags and <button> |
| Table used for layout | Use flexbox or grid |
Input without name |
Its value is never sent |