In the previous lesson you used href to take visitors from one page to another. But a link can point to many more destinations: to a specific section of a page, long or short, on your site or someone else's; to the email application with the recipient and subject already filled in; to the phone dialler on a mobile; or to a file to download. All with the same a element, just by changing the value of href or adding an attribute. In this lesson you will learn about these special links and apply them to The Blue Cup: an internal index on the menu, a phone number and email address that can be tapped directly, and a downloadable PDF menu.
Contents
- Anchors: linking to a section of the same page
- Anchors to another page
- The "back to top" link
- Email links:
mailto: - Phone links:
tel:(andsms:) - Download links: the
downloadattribute - The
hreflangandtypeattributes - Project: menu index, clickable contact details and PDF menu
Anchors: linking to a section of the same page
An anchor (or fragment link) leads to a specific point on a page. It's built in two steps:
- The destination has a unique
idattribute. You already prepared these in Block and Inline Elements: div and span (#bakery,#offer,#opening-hours…). - The link points to that
idby writing a hash#followed by the identifier inhref.
<p>We are open every day: see <a href="#opening-hours">our opening hours and location</a>.</p>
<!-- … further down, on the same page … -->
<div class="block" id="opening-hours">
<h2>Opening hours and location</h2>
<p>We are open every day from <strong>8:00 to 20:00</strong>.</p>
</div>When the link is activated, the browser scrolls the page until the element with id="opening-hours" is at the top of the window, and the address bar shows index.html#opening-hours. That final part, #opening-hours, is called the fragment.
Details worth knowing:
- The
hrefvalue and theidvalue must match exactly, including case:#Opening-hourswon't findid="opening-hours". - You don't write the hash in the
id:id="opening-hours", notid="#opening-hours". - The
idcan be on any element: adiv, anh2, anli… It's usually placed on the section's container or on its heading. - If the page is short and the destination is already visible, you won't notice any scrolling: to test anchors, reduce the height of the window.
- Jumping to a fragment doesn't reload the page and is recorded in the history: the "Back" button takes you back to where you were.
In older documents you will see anchors created with
<a name="opening-hours"></a>. Thenameattribute onais obsolete in HTML5: always useid.
graph LR
A["a href='#opening-hours'"] -- "when activated, scrolls to" --> B["div id='opening-hours'"]
C["a href='menu.html#coffees'"] -- "opens menu.html and scrolls to" --> D["div id='coffees' in menu.html"]
Anchors to another page
You can combine a path with a fragment to open another page directly at a section:
The browser opens menu.html and, as soon as it has loaded, scrolls to the element with id="coffees". If that id doesn't exist, there's no error: the page is simply shown from the top. That's why these mistakes go unnoticed; check them by hand.
The same works with external URLs: https://reviews.example/the-blue-cup#latest would take you to the latest section of that page, if it existed.
href |
Destination |
|---|---|
#coffees |
The coffees section of the current page |
menu.html#coffees |
The coffees section of menu.html |
index.html#opening-hours |
The opening-hours section of the home page |
https://reviews.example/the-blue-cup#latest |
The latest section of an external page |
The "back to top" link
On long pages, a link that takes you back to the beginning is useful. There are two ways to do it:
<!-- Option 1: the special fragment #top -->
<p><a href="#top">Back to top</a></p>
<!-- Option 2: your own id on the first element of the page -->
<h1 id="menu-top">Our menu</h1>
<!-- … -->
<p><a href="#menu-top">Back to the start of the menu</a></p>- The HTML specification states that
#top(and also a bare#) leads to the start of the document even if there is no element with thatid. If there were an element withid="top", the link would go to it. - Option 2 is more explicit and lets you choose the exact point. Both are valid; in the project you will use
#top. - Place these links at the end of each long section or at the end of the page, not after every paragraph.
Email links: mailto:
A link whose href starts with mailto: opens the user's email program with a new message addressed to that address:
<p>Email us at <a href="mailto:[email protected]">[email protected]</a>.</p>As well as the recipient, you can fill in other fields by adding parameters after a ? sign and separating them with &:
| Parameter | Fills in | Example |
|---|---|---|
subject |
The subject | ?subject=Group%20booking |
body |
The body of the message | &body=Hello%2C%20there%20are%208%20of%20us. |
cc |
Copy | &[email protected] |
bcc |
Blind copy | &[email protected] |
<a href="mailto:[email protected]?subject=Group%20booking&body=Hello%2C%20there%20are%208%20of%20us.">Ask for a group quote by email</a>Let's analyse it:
mailto:[email protected]: the recipient.?subject=Group%20booking: the?opens the parameters. Spaces can't go into a URL as they are: they are encoded as%20.&body=…: the&separates parameters. Inside an HTML attribute, the most correct way to write it is&(browsers also accept a bare&, but the validator may warn you). Remember that&has been a special character in HTML since Block Quotes and Preformatted Text.%2Cis the encoded comma. In general, encode any punctuation mark you are unsure about.
The most common characters and their encoding:
| Character | Encoded |
|---|---|
| space | %20 |
, |
%2C |
? |
%3F |
& (within the text) |
%26 |
| line break | %0D%0A |
é (as in "café"), £… |
%C3%A9, %C2%A3… (best avoided in the subject, or left for the browser to encode) |
Limitations of mailto::
- It only works if the visitor has an email program set up. If not, nothing happens or a system dialog appears. That's why you should always show the address as the link text, so that it can be copied.
- Publishing the address on the web exposes it to programs that harvest email addresses to send spam. For long messages, a contact form (Module 5) is usually a better option.
Phone links: tel: (and sms:)
With tel:, on a mobile, the link opens the dialler with the number already entered; on a computer it may open a calling application (or do nothing):
- In the
href, write the number in international format:+, the country code (34 for Spain) and the number, with no spaces or hyphens. That way it works from any country. - In the visible text, write it with spaces so that it's easy to read.
- As with email, show the number in the text: someone on a computer will be able to read it and dial it from their phone.
There is also the sms: scheme, which opens the messaging application: <a href="sms:+34900000000">Send us a text</a>. Support for pre-filling the message text varies between systems, so it's rarely used; you just need to know it exists.
Download links: the download attribute
When you link to a file, the browser decides what to do with it: a PDF or an image is normally opened in the tab itself; a .zip file is downloaded. The download attribute asks it to download the file instead of opening it:
If you give it a value, that will be the suggested name for the downloaded file:
<a href="media/menu-2026-v3-final.pdf" download="the-blue-cup-menu.pdf">Download the menu (PDF, 120 KB)</a>Here the file on the server has an unfriendly internal name, but the visitor will save it as the-blue-cup-menu.pdf.
Things to bear in mind:
downloadonly works with files from the same origin (your own site). If you link to a file on another domain, the browser ignores it and behaves as it would with a normal link.- In some browsers it doesn't work when you open the page with
file:///; test it with Live Server. - State the file's format and size in the link text: visitors will know what they are getting before they tap, which is especially important on mobile data.
The hreflang and type attributes
Two informative attributes that you can add to any link:
| Attribute | Indicates | Example |
|---|---|---|
hreflang |
The language of the destination page | hreflang="es" |
type |
The file type (MIME type) of the destination | type="application/pdf" |
<a href="https://thebluecup.example/es/" hreflang="es" lang="es">Versión en español</a>
<a href="media/the-blue-cup-menu.pdf" type="application/pdf" download>Download the menu (PDF, 120 KB)</a>They are hints for the browser and search engines; they don't change how the link behaves. Notice the difference: hreflang states which language the destination is in, and lang which language the link text is in ("Versión en español"). You will come across them again in Metadata: meta, SEO and Open Graph.
Project: menu index, clickable contact details and PDF menu
- Internal index in
menu.html
menu.htmlIf you did Exercise 3 in Block and Inline Elements: div and span, each section of the menu is already in a div class="menu-section" with its id. If not, add them now. The structure should look like this (with the content of each section the same as before):
<h1>Our menu</h1>
<p>Speciality coffee <span class="highlight">roasted in house</span> and pastries from our bakery.</p>
<p><small>Prices include <abbr title="Value Added Tax">VAT</abbr>.</small></p>
<p>On this page:</p>
<ul class="page-index">
<li><a href="#specialities">House specialities</a></li>
<li><a href="#coffees">Coffees</a></li>
<li><a href="#brewing">How we make our filter coffee</a></li>
<li><a href="#glossary">Coffee glossary</a></li>
</ul>
<p><a href="media/the-blue-cup-menu.pdf" type="application/pdf" download="the-blue-cup-menu.pdf">Download the full menu (PDF, 120 KB)</a></p>
<div class="menu-section" id="specialities">
<h2>House specialities</h2>
<!-- … list of specialities … -->
</div>
<div class="menu-section" id="coffees">
<h2>Coffees</h2>
<!-- … nested list of coffees … -->
</div>
<div class="menu-section" id="brewing">
<h2>How we make our filter coffee</h2>
<!-- … steps … -->
</div>
<div class="menu-section" id="glossary">
<h2>Coffee glossary</h2>
<!-- … dl … -->
</div>
<p><a href="#top">Back to top</a></p>- The index is a list of anchors: each text matches the
h2of its section. - The PDF goes in the
media/folder, alongside the other downloadable resources. Create a test PDF (for example, by printingmenu.htmlto PDF from the browser) and save it asmedia/the-blue-cup-menu.pdf. - "Back to top" closes the page, after the last section.
- Clickable phone number and email in
index.html
index.htmlIn the #opening-hours section, replace the plain-text phone number:
<p>12 Example Street<br>
Fictional Town<br>
Phone: <a href="tel:+34900000000">+34 900 000 000</a></p>In the #contact section, turn the email address into a link with a pre-filled subject:
<p>Email us at <a href="mailto:[email protected]?subject=Enquiry%20from%20the%20website">[email protected]</a> and we'll get back to you as soon as possible.</p>And in the welcome, add two useful anchors, one to the page itself and another to the menu:
<p>We are open every day: see <a href="#opening-hours">our opening hours and location</a> or take a look at <a href="menu.html#coffees">our coffees</a>.</p>
- The same details in
contact.html
contact.htmlThe contact page should have the details more readily available than any other. After the paragraph with the address, add:
<p>Phone: <a href="tel:+34900000000">+34 900 000 000</a></p>
<p>Email: <a href="mailto:[email protected]">[email protected]</a></p>
<p>You can also check <a href="index.html#opening-hours">our opening hours</a>.</p>Test everything with Live Server: reduce the height of the window and use the menu index; tap the phone number on your mobile (if Live Server is reachable on your network) or check that your computer offers to open an application; click the email address and check that the subject appears; and download the PDF to see what name it is given.
Common Mistakes and Tips
- Writing the hash in the
id(id="#coffees") or forgetting it in thehref(href="coffees"): the first won't be found and the second looks for a file calledcoffees. - Repeating an
idon the same page: the anchor will only go to the first one. - Leaving spaces unencoded in
mailto:(?subject=Group booking): some programs will cut the subject short. Use%20. - Phone numbers with spaces or without the international code in
tel:: this may fail on some devices or from abroad. - Hiding the details behind the link ("Call us", "Email us"): show the number and the address in the text for anyone who can't use the link.
- Expecting
downloadto work with files on another domain: it only works with files on your own site. - Not stating the format and size of a downloadable file.
- Tip: use short
idvalues, in lower case and with hyphens, that describe the content (#coffees,#brewing). They will become part of URLs that other people may share, so avoid changing them once they're published.
Exercises
Exercise 1. Write an email link to [email protected] with the subject "Bread-making workshop" and the body "Hello, I'd like to sign up for the next workshop at your café.". Encode the spaces and punctuation correctly.
Exercise 2. news.html has three main news items (seasonal coffee, bread-making workshop and cold brew). Add an id to the h2 of each one and create an index of anchors at the start of the page (after the h1). Also add a link in the #bakery section of index.html that leads directly to the workshop news item.
Exercise 3. Find the errors:
<a href="tel:900 000 000">Call us</a>
<a href="#Glossary">Glossary</a> <!-- the destination is <div id="glossary"> -->
<a href="https://othersite.example/menu.pdf" download="menu.pdf">Menu</a>Solutions
Solution 1.
<a href="mailto:[email protected]?subject=Bread-making%20workshop&body=Hello%2C%20I%27d%20like%20to%20sign%20up%20for%20the%20next%20workshop%20at%20your%20caf%C3%A9.">Sign up for the workshop by emailing [email protected]</a>The spaces are %20, the comma %2C, the apostrophe in "I'd" %27 and the é in "café" %C3%A9. The hyphen in "Bread-making" doesn't need encoding. The & that separates parameters is written & inside the attribute. A simpler alternative would be to write the body without the accent ("cafe"), but the encoded result is the correct one.
Solution 2.
<h1>News from The Blue Cup</h1>
<p>On this page:</p>
<ul class="page-index">
<li><a href="#autumn-coffee">Our autumn seasonal coffee has arrived</a></li>
<li><a href="#bread-workshop">Bread-making workshop for beginners</a></li>
<li><a href="#cold-brew">A recipe for home: our cold brew</a></li>
</ul>
<h2 id="autumn-coffee">Our autumn seasonal coffee has arrived</h2>
<!-- … -->
<h2 id="bread-workshop">Bread-making workshop for beginners</h2>
<!-- … -->
<h2 id="cold-brew">A recipe for home: our cold brew</h2>In index.html, inside #bakery:
<p>Want to learn? Sign up for <a href="news.html#bread-workshop">our bread-making workshop for beginners</a>.</p>Solution 3.
tel:900 000 000: it contains spaces and is missing the country code; correct:href="tel:+34900000000". What's more, "Call us" hides the number; better:Call us on <a href="tel:+34900000000">+34 900 000 000</a>.#Glossarydoesn't matchid="glossary"because of the capital letter; correct:href="#glossary".downloaddoesn't work with files on another domain: the browser will open it like a normal link. The text should also state the format and size: "Othersite's menu (PDF, 300 KB)".
Conclusion
You have seen that href accepts much more than paths to pages: #id fragments to jump to a section of the same page or another one (menu.html#coffees), the special fragment #top to go back to the top, mailto: with encoded parameters to open the email program, tel: with the number in international format to call with a single tap, and the download attribute to offer files with a friendly name, as well as the hreflang and type hints. The Blue Cup becomes easier to use: the menu has an internal index and can be downloaded as a PDF, and the phone number and email address on the home page and the contact page work with a single tap. So far, however, the site is all text. In the next lesson, Adding Images, you will add the logo, the storefront and the product photos, and learn how to describe them for people who can't see them.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
