Welcome to our free HTML Training tutorial. This tutorial is based on Webucator's Introduction to HTML Training course.
The ability to link from one page to another is what makes HTML hyper. Calling it Hypertext, however, is a bit of a misnomer, as images and other elements can also be linked.
The tag for a link is perhaps the least intuitive of all the HTML tags. It is
<a> and it comes from the word "anchor," as
<a> tags used to be used to create locations to link to, known as anchors, as well as the links themselves.
By itself, the
<a> tag does nothing. To create a link, it requires the
which takes as a value the path to the file to which to link. The syntax is as follows:
<a href="path_to_file">Link Text</a>
<a href="bios/john-lennon.html">John Lennon</a>
<a> elements may be direct children of the
<body> <a href="https://www.webucator.com">Webucator</a> </body>
An absolute path shows the complete path to a file starting from the web root. To illustrate this, see the following directory tree:
Assume that the wwwroot folder is the web root. This means that when a user types http://www.example.com/index.html, the index.html page within the wwwroot folder will be downloaded to the browser. The absolute path to the web root from a page on the same domain is simply a forward slash (/). So, a link on Company.html to index.html could be written like this:
<a href="/index.html">Home Page</a>
The same link could be placed on any page in any folder below the wwwroot folder or on any page in the wwwroot folder itself.
Using an absolute path, a link to company.html would include the about directory, like this:
<a href="/about/company.html">About Our Company</a>
When linking to a file at a different domain you must identify the location of the domain using the domain name (or IP address) of the site. To illustrate, assume that the directory structure shown above is found at http://www.beatles.com. A link to company.html from another site would be written like this:
<a href="http://www.beatles.com/about/company.html"> About the Beatles</a>
See Links/Demos/links.html for more examples of links with absolute paths.
Relative paths can only be used to link to other files under the same web root. A relative path indicates where a file is relative to the file that contains the link. The folder (or directory) that contains the file being worked on is called the current directory. The relative path to another file that is also in the current directory is just the name of that file. For example, since company.html and contact.html are found in the same directory, they can link to each other simply by specifying the file name. The following shows a link that could be used in contact.html to company.html.
<a href="company.html">About Our Company</a>
The relative path to a file in a subdirectory of the current directory must include the name of the subdirectory. For example, to link to company.html from index.html you must first point to the about directory, like so:
<a href="about/company.html">About Our Company</a>
The relative path to a file in a directory above the current directory should begin with ../ For example, the following shows a link to index.html from company.html.
<a href="../index.html">Home Page</a>
See Links/Demos/links.html for more examples of links with relative paths.
The web server administrator can set up default page names to look for when a path ends with a directory name without specifying a file. Often these files are called index.html or Default.html. In this case, the following two URLs would be identical, both loading index.html.
target attribute is used to specify the browser window in which the linked page will open.
<a href="company.html" target="newwin">Our Company</a>
If there is no open browser window with the specified target name, a new window will be opened with that name. As long as that window stays open,
future links with the same
target value will target that window. To force each link to target a brand new window, use "_blank" as the
value of the
<a href="company.html" target="_blank">Our Company</a>
See Links/Demos/links.html for more examples of links with targets.
Email links are used to open an email client to start a new email message. The syntax is similar to the links we have seen thus far.
The value of the
href attribute begins with
mailto: and ends with an email address. For example:
<a href="mailto:firstname.lastname@example.org">Email Paul</a>
It is good practice to include the email address as the text of the link, so that people who are printing the page or whose setup does not support email links can see the actual email address. For example:
Email Paul at <a href="mailto:email@example.com">firstname.lastname@example.org</a>.
See Links/Demos/links.html for more examples of links with email links.
In this exercise you will add links to index.html. The completed page should look like this:
Add links to resources.html to external resources.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Runners Home™</title> </head> <body> <div> <a href="index.html">Home</a> | <a href="races.html">Races</a> | <a href="resources.html">Resources</a> | <a href="calculator.html" target="newwin">Calculator</a> | <a href="running-log.html">Running Log</a> | <a href="my-account.html">My Account</a> | <a href="logout.html">Log out</a> </div> <p>Hello, Stranger!</p> <h1>Welcome to Runners Home™</h1> <p> Runners Home™ is dedicated to providing you with:<br> • <a href="races.html">the most up-to-date information on running races</a>.<br> • <a href="resources.html">the best resources for runners</a>. </p> <hr> <p>© 2020 Runners Home. All rights reserved. For questions, send email to <a href="mailto:email@example.com">firstname.lastname@example.org</a>.</p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Resources</title> </head> <body> <h1>Resources</h1> <div> <a href="https://www.mapmyrun.com/">Map My Run</a><br> <a href="https://www.runnersworld.com">Runners World</a><br> <a href="https://strengthrunning.com/">Strength Running</a> </div> </body> </html>
Every HTML element can take an
id attribute, which uniquely identifies that element on the page.
To target a specific location on the page, use the pound sign (
#) followed by the
id value of the element at that location as the
href value of that link.
For example, assume you have an
<h1> tag with the
id of "john":
<h1 id="john">John Lennon</h1>
To target that location, use:
<a href="#john">Read about John</a>
You can also link to locations on other pages.
<a href="about.html#john">Read about John</a>
<a href="http://www.beatles.com/about.html#john">Read about John</a>
The file below shows more examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Targeting Locations on a Page</title> </head> <body> <h1 id="top">Targeting Locations on a Page</h1> <h2>Links to Locations on Remote Pages</h2> <div> <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-href">Mozilla href Documentation</a> </div> <h2>Links to Locations on This Page</h2> <div><a href="#winnie" title="A short story about Winnie the Pooh">Pooh Goes Visiting</a></div> <div><a href="#alice">A Mad Tea-Party</a></div> <div><a href="#naughtyboy">The Naughty Boy</a></div> <hr> <h2>Locations on This Page</h2> <p>Each title below has an id attribute.</p> <h3 id="winnie">POOH GOES VISITING - A.A. Milne</h3> <p>Pooh always liked a little something at eleven o'clock in the morning, and he was very glad to see Rabbit getting out the plates and mugs; and when Rabbit said, 'Honey or condensed milk with your bread?' he was so excited that he said, 'Both' and then, so as not to seem greedy, he added, 'But don't bother about the bread, please.'</p> ---- C O D E O M I T T E D ---- <hr> <h3 id="alice">A MAD TEA-PARTY - Lewis Carroll</h3> <p>There was a table set out under a tree in front of the house, and the March Hare and the Hatter were having tea at it: a Dormouse was sitting between them, fast asleep, and the other two were using it as a cushion, resting their elbows on it, and the talking over its head. 'Very uncomfortable for the Dormouse,' thought Alice; 'only, as it's asleep, I suppose it doesn't mind.'</p> ---- C O D E O M I T T E D ---- <hr> <h3 id="naughtyboy">THE NAUGHTY BOY - Hans Christian Andersen</h3> <p>Along time ago, there lived an old poet, a thoroughly kind old poet. As he was sitting one evening in his room, a dreadful storm arose without, and the rain streamed down from heaven; but the old poet sat warm and comfortable in his chimney-comer, where the fire blazed and the roasting apple hissed.</p> ---- C O D E O M I T T E D ---- <div><a href="#top">Back to top</a></div> </body> </html>
title attribute can be used to provide a description about a link. The description is displayed by the browser as a tooltip. Screen readers might read the description to a blind user.
Here's an example:
<a href="#winnie" title="A short story about Winnie the Pooh">Pooh Goes Visiting</a>
When you hover over this link, the title appears as a tip:
This is especially useful when an image is actually a link (we'll find out how to do this later).
It might not be clear from the image itself where the link points. The
title attribute can be used to make the link destination clear.