← Back to HTML & CSS, page 10

html-css-0182 · Easy fresher

How do you write links for downloads, email addresses and phone numbers?

The protocol in href decides the action: mailto: for email with optional subject, tel: for phone numbers in international format, and normal addresses for pages. The download attribute saves same-origin files with a suggested name. Link text should be clear and say when a link opens a file or a new tab.

Full answer

An a element can point to more than a web page. The protocol at the start of href tells the browser what to do.

  • A normal page link uses https:, a relative path or a fragment such as #section.
  • mailto: opens the mail program. mailto:hello@example.com?subject=Hello can add a subject, and values must be URL-encoded. Several addresses are separated by commas.
  • tel: starts a phone call on phones and some desktop apps. Write the number with the country code and no spaces: tel:+911234567890. The visible text can be written for readers with spaces.
  • sms: opens a text message on phones, and its support differs by device.
  • download tells the browser to save the target as a file and not open it. A value gives the suggested file name: download="report.pdf". It works only for same-origin addresses (and for blob: and data: addresses). For a file from another site, the browser may ignore it and just navigate.
  • href="#" links to the top of the page and is not a good button. Use a button for actions.

Other useful attributes:

  • target="_blank" opens a new tab. Warn users when links open a new tab, and add rel="noopener" if you target older browsers.
  • hreflang and type give hints about the target. ping is not widely supported.
  • For file links, show the file type and size in the text: "Syllabus (PDF, 240 KB)". This helps people on slow connections and people using screen readers.

Links must have clear text. "Click here" says nothing when links are listed by a screen reader. Use "Download the syllabus" instead.

The first link downloads a file with the suggested name. The second opens a new email with the subject filled in. The third offers a call on devices that support it. The last opens a new tab and says so in its text.

Test these links on a phone and a desktop, because what happens depends on the device and the installed apps.

html
<ul>
  <li><a id="dl" href="/files/syllabus.pdf" download="syllabus.pdf">Download the syllabus (PDF)</a></li>
  <li><a id="mail" href="mailto:hello@example.com?subject=Course%20question">Email us</a></li>
  <li><a id="tel" href="tel:+911234567890">+91 12345 67890</a></li>
  <li><a id="ext" href="https://example.org" target="_blank" rel="noopener">External site (opens in a new tab)</a></li>
</ul>

The check read the link properties. Opening a mail program, making a call and saving a file depend on the device and were not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • mailto: and tel: open the mail and phone apps.
  • Write phone numbers in international format without spaces.
  • download works for same-origin addresses.
  • Use buttons for actions, not href="#".
  • Link text should be clear and mention files and new tabs.

Common mistakes

  • Using href="#" with a click handler instead of a button.
  • Writing phone numbers with spaces or no country code in the href.
  • Expecting the download attribute to work for another site's file.

Follow-ups

  • When does the download attribute work?
  • Why include the file type and size in the link text?

Tips

  • List the schemes and one point for each.
  • Mention link text that works out of context.