HTML Handbook
HTML Handbook
Introduction
Preface
HTML
Basics
The document heading
The document body
Tags that interact with text
Links
Container tags and page structure HTML
Forms
Tables
Multimedia tags: audioand video
iframes
Images
Accessibility
Introduction
2
Introduction
Welcome!
I wrotethis book to help you quickly learn HTML
and get familiar with the advanced HTML topics.
Introduction
3
Even if you don't write HTML in your day to day
work,knowing how HTML works can help save you some
headacheswhenyou need to understand it from time to
time, for example while tweaking a web page.
Flavio
My website is flaviocopes.com.
4
Preface
Preface
HTML is the foundation of the marvel called
the Web.
5
It did so because of another feature it
provides us: forgiveness. There are somerules,right, but
after you learn those, you have a lot of freedom.
Preface
We even know what the first web page was. It's this:
http://info.cern.ch/hypertext/WWW/TheProject.html
Don't take this for granted. I don't know any other platform
that gives us this ability.
6
I learned so muchwhile writing it, even though
I've been working with the Web for 20+ years, and
I'm sure you'll find something new, too.
7
HTML Basics
HTML Basics
HTML Basics
HTML is a standard defined by the WHATWG,
an acronym for Web Hypertext ApplicationTechnology
Working Group, an organization formed by people
working on the most popular web browser. This means
it's basically controlled by Google, Mozilla, Apple
and Microsoft.
8
HTML Basics
Busy times!
9
HTML Basics
10
HTML Basics
tag:
<ul>
<li>Firstitem</li> <li>Second item</li>
<li>Third item</li>
</ul>
11
HTML Basics
<!DOCTYPE html>
12
HTML Basics
<!DOCTYPE html>
<html> ...
</html>
<sometag>some content</sometag>
The
html
ending
tag
is
the
last
thing
present
in
an
HTML
document.
13
HTML Basics
<head>
...
</head>
<body>
...
</body>
</html>
Inside head we will have tags that are
essential to creating a web page, like the title, the
metadata, and internal or external CSS and JavaScript.
Mostly things that do not directly appear on
the page, but only help the browser (or bots like
the Google search bot) display it properly.
Tags vs elements
I mentioned tags and elements. What's the difference?
14
HTML Basics
Attributes
The starting tag of an element can have special
snippets of information we can attach, called
attributes.
15
HTML Basics
16
HTML Basics
Case insensitive
HTML is case insensitive.Tags can be written in all
caps,or lowercase. In the early days,caps were the norm.
Today lowercase is thenorm. It is a convention.
White space
Pretty important. In HTML, even if you add multiple
white spaces into a line, it's collapsed by the
browser's CSS engine.
17
HTML Basics
<p> A paragraph of
text</p>
<p>A paragraph
of
text
</p>
I typically favor
or
<p> A paragraph of
text
</p>
18
HTML Basics
<body>
<p> A
paragraph of text
</p>
<ul>
<li>A list item</li>
</ul>
</body>
19
The document heading
<head>
...
</head>
...
</html>
We never use attributes on this tag. And we don't
write content in it.
title
script
noscript
20
The document heading
link
style
base
meta
<script>
..some JS
</script>
Or you
can
load
an
external
JavaScript
file
21
The document heading
by
using
the
src
attribute:
<script src="file.js"></script>
22
The document heading
23
The document heading
In thiscase,the noscript
link
tags style
tags meta
tags
<head>
...
<noscript>
<style>
.no-script-
alert {
display: block;
24
The document heading
</style>
</noscript>
...
</head>
...
</html>
Let's solve the other case:if put in the body,it
can contain content, like paragraphs and other
tags, which are rendered in the UI.
Usage:
<!DOCTYPE html>
<html>
<head>
...
<link href="file.css"
rel="stylesheet">
...
</head>
25
The document heading
...
</html>
The media attribute allows the loading of
different stylesheets depending on the device
capabilities:
<linkhref="file.css" media="screen"rel="stylesheet">
<linkhref="print.css" media="print" rel="stylesheet">
26
The document heading
Usage:
<style>
.some-css {}
</style>
<style media="print">
.some-css {}
</style>
</head>
...
</html>
27
The document heading
<meta charset="utf-8">
28
The document heading
29
The document heading
30
The document body
<head>
...
</head>
<body>
...
</body>
</html>
Just like the head and html tags, we
can only have one body tag in one page.
31
Tags that interact with text
Technically, the start and ending tags are optional.
But I consider it a good practice to add them.
Just for clarity.
block elements (
p , div
32
The difference also lies in the visual properties we
can edit usingCSS. We can alter the width/height, margin,
padding and border of block elements. We can't do
that for inline elements.
to be a block element.
The p tag
This tag defines a paragraph of text.
<p>Some text</p>
Inside it, we
can add
33
Tags that interact with text
any inline
element we
like, like
span or
a .
We cannot nest a p
34
Tags that interact with text
The br tag
This tag represents a line break. It's an inline
element, and does not need a closing tag.
, h3 , h4 , h5 , h6 .
35
Tags that interact with text
36
Tags that interact with text
The em tag
This tag is used to mark the text inside it as
emphasized. Like with strong , it's not a visual
hint but a semantic hint.
Quotes
The blockquote HTML
tag is useful to insert
citations in the text.
37
Tags that interact with text
Horizontal line
Not reallybased on text, but the hr tag is
often used inside a page. It means horizontal rule
Code blocks
The code tag is especially useful to show
code,because browsers give it a monospaced font.
code{
font-family: monospace; }
pre {
display: block;
font-family: monospace;
white-space:
pre;
margin: 1em 0px; }
38
Tags that interact with text
Lists
We have 3 typesof lists:
unordered
lists ordered
lists definition
lists Unordered
lists are created
usingthe ul
tag. Each
item in the list
is created with the
li
tag:
<ul>
<li>First</li> <li>Second</li>
</ul>
tag:
<ol>
<li>First</li> <li>Second</li>
39
Tags that interact with text
</ol>
40
Tags that interact with text
Othertext tags
There is a number of tags with presentational
purposes:
the
mark
tag the
ins
tag
the del
tag
the sup
tag the
sub tag
the small
tag the
i tag
41
Tags that interact with text
the b tag
42
Links
Links
Links
Links are defined usingthe a tag. The link
destination is set via its href attribute.
Example:
43
Container tags and page structure HTML
Example, I'm on the page https://flaviocopes.com/axios/
Links
https://flaviocopes.com/axios/test
44
Container tags and page
structure HTML
Container tags
HTML provides a set of container tags. Those
tags can contain an unspecified set of other tags.
We have:
article
The article tag identifies a thing that can be
independent from other things in a page.
45
Container tags and page structure HTML
Or a list of links.
<div>
<article>
<h2>A blog
post</h2>
<a
...>Read more</a>
</article>
<article>
<article>
<h2>A blog post</h2> <p>Here
is the content...</p>
</article>
Inside an article
) and
46
Container tags and page structure HTML
section
Represents a section of a document. Each section
has a heading tag ( h1 - h6 ), then the section
body.
Example:
div
div is the generic container element:
<div> ...
</div>
47
Container tags and page structure HTML
list:
<nav>
<ol>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ol>
</nav>
aside
The aside tag is used to add a pieceof
content that is related to the main content.
Example:
<div> <p>some
text..</p>
<aside>
<p>A quote..</p>
</aside>
<p>other
text...</p>
</div>
48
Container tags and page structure HTML
header
The header tag represents a part of the page
that is the introduction. It can for example contain
one or more heading tag ( h1 - h6 ), the
tagline for the article, an image.
<article>
<header>
<h1>Article title</h1>
</header>
...
</div>
main
The main tag represents the main
part of a page:
<body>
....
<main>
<p>....</p>
</main>
</body>
footer
The footer tag is used to determine the footer
of an article, or the footer of the page:
<article> ....
<footer>
49
Container tags and page structure HTML
<p>Footer
notes..</p>
</footer>
</div>
50
Forms
Forms
Forms
Forms are the way you can interact with a page,
or an app, built with Web technologies.
<form> ...
</form>
51
Forms
parameter:
If the origin
(protocol +
domain +
port) is
https://flaviocopes.com
52
Forms
<input>
53
Forms
Equivalent to using:
<input type="text">
Email
Using type="email" will validate client-side (in
the browser) an email for correctness (semantic
correctness, not ensuring the emailaddress is existing)
before submitting.
54
Forms
Password
Using type="password" will
make everykey entered appear
as an
asterisk (*) or dot, useful for fields that host a
password.
<input type="password" name="password" placeholder="Your password"
>
Numbers
You can have an input element accept only numbers:
55
Forms
Hidden field
Fields can be hidden from the user. They will still be
sent to the server upon the form submit:
56
Forms
Form submit
The type="submit" field is a button that, once
pressed by the user, submits the form:
<input type="submit">
Form validation
Browsers provide client-side validation functionality to
forms.
57
Forms
Otherfields
File uploads
You can load files from your local computer and send them to
the server usinga type="file" input element:
58
Forms
Buttons
The type="button" input fields can be used to add
additional buttons to the form, that are not submit buttons:
<input type="reset">
59
Forms
Radio buttons
Radio buttons are used to create a set of
choices, of which one is pressed and all the others
are disabled.
attribute:
You can
set
the
value
that's
pre-selected
using
the
checked
attribute.
60
Forms
Checkboxes
Similar to radio boxes, but they allow multiple values
to be chosen, or none at all.
attribute:
Date andtime
We have a few input typesto accept date values.
61
Forms
62
Forms
Color picker
You can let users
pick a color
using the
type="color"
element:
attribute:
Range
This input element shows a sliderelement. People
can use it to move from a starting valueto an
ending value:
63
Forms
"10">
Telephone
The type="tel" input field is used to
enter a phone number:
URL
The type="url" field is used to
enter a URL.
attribute:
64
Forms
<textarea></textarea>
<textarea name="article"></textarea>
<select name="color">
65
Forms
<select name="color">
<option value="red" disabled>Red</option>
<option value="yellow">Yellow</option>
</select>
You can have one empty option:
<select name="color">
<option value="">None</option>
<option value="red">Red</option> <option
value="yellow">Yellow</option>
</select>
<select name="color">
<optgroup label="Primary">
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option
value="blue">Blue</option>
</optgroup>
<optgroup label="Others">
<option value="green">Green</option>
<option
value="pink">Pink</option>
</optgroup>
</select>
66
Tables
Tables
Tables
In the early days of the web tables were a very
important part of building layouts.
<table>
</table>
67
Tables
Rows
A row is added usingthe tr tag, and
that's the only thing we can add into a table
element:
<table>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
Column headers
The table header contains the name of a column,
typically in a bold font.
Think about
an
Excel
/
Google
Sheets
document.
The
top
A-B-C-D...
header.
68
Tables
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th> <th>Column 3</th>
</tr>
<tr></tr>
<tr></tr>
</table>
Thetable content
The content of the table is defined using td
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th> <th>Column 3</th>
</tr>
<tr>
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
<td>Row 1 Column 3</td>
69
Tables
</tr>
<tr>
<td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
<td>Row 2 Column 3</td>
</tr>
</table>
th, td {
padding: 10px;
border: 1px solid #333; }
70
Tables
71
Tables
Or it can span
over 2 or
more rows,
using the
rowspan
attribute:
<table>
<tr>
<th>Column 1</th>
<th>Column
2</th>
<th>Column 3</th>
</tr>
<tr> <td colspan="2"
rowspan="2">Rows 1-2 Columns 1-2</td>
<td>Row 1 Column 3</td>
</tr>
<tr> <td>Row 2 Column 3</td>
</tr>
</table>
Row headings
Before I explained how you can have column
headings, usingthe th tag inside the first tr
72
Tables
<table>
<tr>
<th></th>
<th>Column 2</th> <th>Column 3</th>
</tr>
<tr>
<th>Row 1</th>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tr>
<th>Row 2</th>
<td>Col 2</td>
<td>Col 3</td>
</tr>
</table>
73
Tables
thead
tbody
tfoot
<table>
<thead>
<tr>
<th></th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<th>Row 1</th>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tr>
<th>Row 2</th>
<td>Col 2</td>
<td>Col 3</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
74
Tables
Col 2</td>
</tr>
</tfoot>
</table>
Table caption
A table should have a caption tag that
describes its content. That tag should be put immediately
after the opening table tag:
<table>
<caption>Dogs age</caption>
<tr>
<th>Dog</th>
<th>Age</th>
</tr>
<tr>
<td>Roger</td>
<td>7</td>
</tr>
</table>
75
Multimedia tags: audioand video
attribute:
<audio src="file.mp3">
76
Multimedia tags: audioand video
To show the
built-in
controls, you
can add
the
controls
attribute:
77
Multimedia tags: audioand video
attribute:
<video src="file.mp4">
78
Multimedia tags: audioand video
To show the
built-in
controls, you
can add
the
controls
attribute:
79
Multimedia tags: audioand video
80
iframes
iframes
iframes
The iframe tag allows us to embed content
coming from other origins (other sites)into our web page.
<iframe src="page.html"></iframe>
<iframe src="https://site.com/page.html"></iframe>
81
iframes
Srcdoc
The srcdoc attribute lets you specify someinline
HTML to show. It's an alternative to src ,
but recent and not supported in Edge 18 and lower,
and in IE:
Sandbox
The sandbox attribute allows us to limit the
operations allowed in the iframes.
<iframe src="page.html"></iframe>
82
iframes
Allow
Currently experimental and only supported by Chromium-
based browsers, this is the future of resource sharing
between the parent window and the iframe.
83
iframes
Referrer
When loading an iframe, the browser sends it
important information about who is loading it in
the Referer header (notice the single r
84
iframes
85
iframes
86
Images
Images
Images
Images can be displayed using
the img tag.
<imgsrc="image.png">
87
Images
00">
88
Images
<imgsrc="dog.png"
alt="A picture of a dog"
srcset="dog-500.png
500w,
dog-800.png
800w,
dog-1000.png 1000w,
dog-
1400.png 1400w">
In the srcset we
use the w
Since we do so,
we also need to
use the sizes
attribute:
<img src="dog.png"
alt="A picture of a
dog"
sizes="(max-width: 500px) 100vw, (max-width:
900px) 50vw, 800 px"
srcset="dog-500.png
500w,
dog-800.png 800w,
dog-1000.png 1000w,
dog-1400.png 1400w">
89
Images
90
Images
<picture>
<sourcetype="image/webp" srcset="image.webp"> <img src="image.jpg"
alt="An image">
</picture>
91
Images
But that's not its use case,because as you can see it's
muchmore verbose.
92
Accessibility
Accessibility
Accessibility
It's important we design our HTML with accessibility
in mind.
Web pages and Web apps are not always built with
accessibility as one of their first goals, and maybe
version 1 is released not accessible but it's possible
to makea web page accessible after the fact. Sooner is
better, but it's never too late.
93
Accessibility
h1
h2
h3
h2
94
Accessibility
h2
h3
h4
<table>
<caption>Dogs age</caption>
<tr>
<th>Dog</th>
<th>Age</th>
</tr>
<tr>
<td>Roger</td>
<td>7</td>
</tr>
95
Accessibility
</table>
96
Accessibility
<nav>
<ul>
<li><a href="/">Home</a></li> <li><a
href="/blog">Blog</a></li>
</ul>
</nav>
role:
<div role="navigation">
<ul>
<li><a href="/">Home</a></li> <li><a
href="/blog">Blog</a></li>
</ul>
</div>
97
Accessibility
aria-label
This attribute is used to add a stringto describe an
element.
Example:
98
Accessibility
aria-labelledby
This attribute sets a correlation between the current
element and the one that labels it.
Example:
aria-describedby
This attribute lets us associate an element with another
element that servesas description.
Example:
99
Accessibility
https://www.w3.org/TR/WCAG20/ https://webaim.org
https://developers.google.com/web/fundamentals/accessibility/
100