The Racers Life - Copy (11)
The Racers Life - Copy (11)
If you are new to HTML this book aims to help you get
started. If you are a seasoned Web Developer this book
will improve your knowledge.
HTML BASICS
HTML is a standard defined by the WHATWG, an
acronym for Web Hypertext Application Technology
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.
In the past the W3C (World Wide Web Consortium) was
the organization in charge of creating the HTML
standard.
The control informally moved from W3C to WHATWG
when it became clear that the W3C push towards
XHTML was not a good idea.
<!DOCTYPE html>
Then we have the html element, which has an opening
and closing tag:
<!DOCTYPE html>
<html>
...
</html>
Most tags come in pairs with an opening tag and a
closing tag. The closing tag is written the same as the
opening tag, but with a /:
<sometag>some content</sometag>
There are a few self-closing tags, which means they
don't need a separate closing tag as they don't contain
anything in them.
The html starting tag is used at the beginning of the
document, right after the document type declaration.
The html ending tag is the last thing present in an HTML
document.
Inside the html element we have 2
elements: head and body:
<!DOCTYPE html>
<html>
<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.
Inside body we will have the content of the page.
The visible stuff.
Tags vs elements
I mentioned tags and elements. What's the difference?
Elements have a starting tag and a closing tag. In this
example, we use the p starting and closing tags to
create a p element:
<p>A paragraph of text</p>
So, an element constitutes the whole package:
starting tag
Attributes
The starting tag of an element can have special
snippets of information we can attach,
called attributes.
Attributes have the key="value" syntax:
<p class="a-class">A paragraph of text</p>
You can also use single quotes, but using double quotes
in HTML is a nice convention.
<p>A paragraph
of
text </p>
Using the white-space CSS property you can change
how things behave. You can find more information on
how CSS processes white space in the CSS Spec
I typically favor
<p>
A paragraph of text
</p>
Nested tags should be indented with 2 or 4 characters,
depending on your preference:
<body>
<p>
A paragraph of text
</p>
<ul>
<li>A list item</li>
</ul>
</body>
Note: this "white space is not relevant" feature means
that if you want to add additional space, it can make
you pretty mad. I suggest you use CSS to make more
space when needed.
<script>
..some JS
</script>
Or you can load an external JavaScript file by using
the src attribute:
<script src="file.js"></script>
The type attribute by default is set to text/javascript, so
it's completely optional.
There is something pretty important to know about this
tag.
...
</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>
...
</html>
The media attribute allows the loading of different
stylesheets depending on the device capabilities:
<link href="file.css" media="screen" rel="stylesheet">
<link href="print.css" media="print" rel="stylesheet">
We can also link to resources other than stylesheets.
Usage:
<style>
.some-css {}
</style>
As with the link tag, you can use the media attribute to
use that CSS only on the specified medium:
<style media="print">
.some-css {}
</style>
<!DOCTYPE html>
<html>
<head>
...
<base href="https://flaviocopes.com/">
...
</head>
...
</html>
Note that using CSS we can change the default for each
element, setting a p tag to be inline, for example, or
a span to be a block element.
Another difference is that inline elements can be
contained in block elements. The reverse is not true.
<p>Some text</p>
It's a block element.
The br tag
This tag represents a line break. It's an inline element,
and does not need a closing tag.
Code blocks
The code tag is especially useful to show code, because
browsers give it a monospaced font.
That's typically the only thing that browsers do. This is
the CSS applied by Chrome:
code {
font-family: monospace;
}
This tag is typically wrapped in a pre tag, because
the code element ignores whitespace and line breaks.
Like the p tag.
Chrome gives pre this default styling:
pre {
display: block;
font-family: monospace;
white-space: pre;
margin: 1em 0px;
}
which prevents white space collapsing and makes it a
block element.
Lists
We have 3 types of lists:
unordered lists
ordered lists
definition lists
Unordered lists are created using the ul tag. Each item
in the list is created with the li tag:
<ul>
<li>First</li>
<li>Second</li>
</ul>
Ordered lists are similar, just made with the ol tag:
<ol>
<li>First</li>
<li>Second</li>
</ol>
The difference between the two is that ordered lists
have a number before each item:
Definition lists are a bit different. You have a term, and
its definition:
<dl>
<dt>Flavio</dt>
<dd>The name</dd>
<dt>Copes</dt>
<dd>The surname</dd>
</dl>
This is how browsers typically render them:
I must say you rarely see them in the wild, for sure not
much as ul and ol, but sometimes they might be useful.
Other text tags
There is a number of tags with presentational purposes:
LINKS
Links are defined using the a tag. The link destination is
set via its href attribute.
Example:
<a href="https://flaviocopes.com">click here</a>
Between the starting and closing tag we have the link
text.
<a href="https://flaviocopes.com">
<img src="test.jpg">
</a>
or any other elements, except other <a> tags.
If you want to open the link in a new tab, you can use
the target attribute:
<a href="https://flaviocopes.com" target="_blank">open in new tab</a>
We have:
article
section
div
and it can be confusing to understand the difference
between them.
article
The article tag identifies a thing that can be
independent from other things in a page.
For example a list of blog posts in the homepage.
Or a list of links.
<div>
<article>
<h2>A blog post</h2>
<a ...>Read more</a>
</article>
<article>
<h2>Another blog post</h2>
<a ...>Read more</a>
</article>
</div>
We're not limited to lists: an article can be the main
element in a page.
<article>
<h2>A blog post</h2>
<p>Here is the content...</p>
</article>
Inside an article tag we should have a title (h1-h6) and
section
Represents a section of a document. Each section has a
heading tag (h1-h6), then the section body.
Example:
<section>
<h2>A section of the page</h2>
<p>...</p>
<img ...>
</section>
It's useful to break a long article into different sections.
Shouldn't be used as a generic container element. div is
made for this.
div
div is the generic container element:
<div>
...
</div>
You often add a class or id attribute to this element, to
allow it to be styled using CSS.
We use div in any place where we need a container but
the existing tags are not suited.
Tags related to page
nav
This tag is used to create the markup that defines the
page navigation. Into this we typically add
an ulor ol 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 piece of content that is
related to the main content.
A box where to add a quote, for example. Or a sidebar.
Example:
<div>
<p>some text..</p>
<aside>
<p>A quote..</p>
</aside>
<p>other text...</p>
</div>
Using aside is a signal that the things it contains are not
part of the regular flow of the section it lives into.
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>
<p>Footer notes..</p>
</footer>
</div>
FORMS
Forms are the way you can interact with a page, or an
app, built with Web technologies.
So if the form is in
the https://flaviocopes.com/contacts page, pressing the
"submit" button will make a request to that same URL.
Which might result in nothing happening.
<input>
Equivalent to using:
<input type="text">
As with all the other fields that follow, you need to give
the field a name in order for its content to be sent to
the server when the form is submitted:
Email
Using type="email" will validate client-side (in the
browser) an email for correctness (semantic
correctness, not ensuring the email address is existing)
before submitting.
<input type="email" name="email" placeholder="Your email">
Password
Using type="password" will make every key 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:
Hidden field
Fields can be hidden from the user. They will still be
sent to the server upon the form submit:
Form submit
The type="submit" field is a button that, once pressed by
the user, submits the form:
<input type="submit">
The value attribute sets the text on the button, which if
missing shows the "Submit" text:
<input type="submit" value="Click me">
Form validation
Browsers provide client-side validation functionality to
forms.
Other fields
File uploads
You can load files from your local computer and send
them to the server using a type="file" input element:
<input type="file" name="secret-documents">
You can attach multiple files:
Buttons
The type="button" input fields can be used to add
additional buttons to the form, that are not submit
buttons:
<input type="button" value="Click me">
They are used to programmatically do something, using
JavaScript.
<input type="reset">
Radio buttons
Radio buttons are used to create a set of choices, of
which one is pressed and all the others are disabled.
The name comes from old car radios that had this kind
of interface.
Range
This input element shows a slider element. People can
use it to move from a starting value to an ending value:
Telephone
The type="tel" input field is used to enter a phone
number:
<input type="tel" name="telephone-number">
The main selling point for using tel over text is on
mobile, where the device can choose to show a numeric
keyboard.
Specify a pattern attribute for additional validation:
<input type="tel" pattern="[0-9]{3}-[0-9]{8}" name="telephone-number">
URL
The type="url" field is used to enter a URL.
<input type="url" name="website">
You can validate it using the pattern attribute:
<input type="url" name="website" pattern="https://.*">
<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>
Options can be grouped using the optgroup tag. Each
option group has a label attribute:
<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>
TABLES
In the early days of the web tables were a very
important part of building layouts.
</table>
Inside the table we'll define the data. We reason in
terms of rows, which means we add rows into a table
(not columns). We'll define columns inside a row.
Rows
A row is added using the tr tag, and that's the only
thing we can add into a table element:
<table>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
This is a table with 3 rows.
Row headings
Before I explained how you can have column headings,
using the th tag inside the first tr tag of the table.
You can add a th tag as the first element inside
a tr that's not the first tr of the table, to have row
headings:
<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>
thead
tbody
tfoot
They wrap the tr tags to clearly define the different
sections of the table. Here's an example:
<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>
<td>Footer of Col 1</td>
<td>Footer of 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>
MULTIMEDIA
TAGS: AUDIO AND VIDEO
In this section I want to show you
the audio and video tags.
The audio tag
This tag allows you to embed audio content in your
HTML pages.
You can specify the MIME type of the audio file using
the type attribute. If not set, the browser will try to
automatically determine it:
<audio src="file.mp3" controls type="audio/mpeg">
An audio file by default does not play automatically.
Add the autoplay attribute to play the audio
automatically:
<audio src="file.mp3" controls autoplay>
Note: mobile browsers don't allow autoplay
You can specify the MIME type of the video file using
the type attribute. If not set, the browser will try to
automatically determine it:
<video src="file.mp4" controls type="video/mp4">
A video file by default does not play automatically. Add
the autoplay attribute to play the video automatically:
<video src="file.mp4" controls autoplay>
Some browsers also require the muted attribute to
autoplay. The video autoplays only if muted:
<audio src="file.mp3" controls autoplay muted>
The loop attribute restarts the video playing at 0:00 if
set; otherwise, if not present, the video stops at the end
of the file:
<video src="file.mp4" controls autoplay loop>
You can set an image to be the poster image:
You can set the width and height attributes to set the
space that the element will take so that the browser
can account for it and it does not change the layout
when it's finally loaded. It takes a numeric value,
expressed in pixels.
Using JavaScript you can listen for various events
happening on an video element, the most basic of which
are:
play when the file starts playing
pause when the video was paused
playing when the video is resumed from a pause
ended when the end of the video file was reached
IFRAMES
The iframe tag allows us to embed content coming from
other origins (other sites) into our web page.
Technically, an iframe creates a new nested browsing
context. This means that anything in the iframe does
not interfere with the parent page, and vice versa.
JavaScript and CSS do not "leak" to/from iframes.
<iframe src="page.html"></iframe>
You can load an absolute URL, too:
<iframe src="https://site.com/page.html"></iframe>
You can set a set of width and height parameters (or set
them using CSS) otherwise the iframe will use the
defaults, a 300x150 pixels box:
Srcdoc
The srcdoc attribute lets you specify some inline HTML to
show. It's an alternative to src, but recent and not
supported in Edge 18 and lower, and in IE:
<iframe srcdoc="<p>My dog is a good dog</p>"></iframe>
Sandbox
The sandbox attribute allows us to limit the operations
allowed in the iframes.
If we omit it, everything is allowed:
<iframe src="page.html"></iframe>
If we set it to "", nothing is allowed:
Referrer
When loading an iframe, the browser sends it important
information about who is loading it in the Referer header
(notice the single r, a typo we must live with).
The misspelling of referrer originated in the original
proposal by computer scientist Phillip Hallam-Baker to
incorporate the field into the HTTP specification. The
misspelling was set in stone by the time of its
incorporation into the Request for Comments standards
document RFC 1945
IMAGES
Images can be displayed using the img tag.
This tag accepts a src attribute, which we use to set the
image source:
<img src="image.png">
We can use a wide set of images. The most common
ones are PNG, JPEG, GIF, SVG and more recently WebP.
<img src="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 measure to indicate the
window width.
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, 800px"
srcset="dog-500.png 500w,
dog-800.png 800w,
dog-1000.png 1000w,
dog-1400.png 1400w">
In this example the (max-width: 500px) 100vw, (max-width:
900px) 50vw, 800px string in the sizes attribute describes
the size of the image in relation to the viewport, with
multiple conditions separated by a semicolon.
The media condition max-width: 500px sets the size of the
image in correlation to the viewport width. In short, if
the window size is < 500px, it renders the image at
100% of the window size.
If the window size is bigger but < 900px, it renders the
image at 50% of the window size.
And if even bigger, it renders the image at 800px.
ACCESSIBILITY
It's important we design our HTML with accessibility in
mind.
It's a lot and for the full reference of each of them I give
you this MDN link. But you don't need to assign a role to
every element in the page. Screen readers can infer
from the HTML tag in most cases. For example you
don't need to add a role tag to semantic tags
like nav, button, form.
Let's take the nav tag example. You can use it to define
the page navigation like this:
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</nav>
If you were forced to use a div tag instead of nav, you'd
use the navigation role:
<div role="navigation">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</div>
So here you got a practical example: role is used to
assign a meaningful value when the tag does not
convey the meaning already.
Use the tabindex attribute
The tabindex attribute allows you to change the order of
how pressing the Tab key selects "selectable" elements.
By defaults only links and form elements are
"selectable" by navigation using the Tab key (and you
don't need to set tabindex on them).
Adding tabindex="0" makes an element selectable:
<div tabindex="0">
...
</div>
Using tabindex="-1" instead removes an element from
this tab-based navigation, and it can be pretty useful.
Use the aria attributes
ARIA is an acronym that means Accessible Rich Internet
Applications and defines semantics that can be applied
to elements.
aria-label
This attribute is used to add a string to describe an
element.
Example:
aria-labelledby
This attribute sets a correlation between the current
element and the one that labels it.
If you know how an input element can be associated to
a label element, that's similar.
We pass the item id that describes the current element.
Example:
<p aria-labelledby="description">
...
</p>
aria-describedby
This attribute lets us associate an element with another
element that serves as description.
Example:
https://www.w3.org/TR/WCAG20/
https://webaim.org
https://developers.google.com/web/fundamentals/
accessibility/
You reached the end of the HTML Handbook.
If you read this far, thank the author to show them you
care. Say Thanks
Learn to code for free. freeCodeCamp's open source
curriculum has helped more than 40,000 people get
jobs as developers. Get started
Our mission: to help people learn to code for free. We accomplish this by
creating thousands of videos, articles, and interactive coding lessons - all
freely available to the public.
Mobile App
Publication powered by HashnodeAboutAlumni NetworkOpen
SourceShopSupportSponsorsAcademic HonestyCode of ConductPrivacy PolicyTerms of
ServiceCopyright Policy