Cheat sheet #2

Links and file paths in HTML

A cheat sheet for web design students. Learn how to build links with the anchor element, and how to write the path in its href attribute so that the link always finds its target.

The short version

A link is an <a> element. Its href attribute holds the path to the target, and the text between the opening and closing tags is what people click.

<a href="about-us.html">About us</a>
On this cheat sheet

Anatomy of a link

The anchor element

The a stands for anchor. Like most elements, it has an opening tag, some content (the link text) and a closing tag. The href (hypertext reference) attribute is the only attribute you must include.

<a href="contact.html">Contact the team</a> ^ opening tag ^ href value ^ link text

Always write link text that makes sense on its own, such as “Read the opening times” rather than “Click here”. That’s because screen reader users often jump through a list of the links on a page, so each one needs to say where it goes.

Other things an href can point to

TargetExample
A page on your site<a href="about-us.html">
A folder (opens its index.html)<a href="blog/">
A spot on the same page<a href="#contact-details">
A spot on another page<a href="about-us.html#our-history">
A download such as a PDF<a href="files/price-list.pdf">
An email address<a href="mailto:hello@example.com">
A phone number<a href="tel:+441234567890">

A link that begins with # jumps to the element with the matching id. For example, href="#contact-details" jumps to <h2 id="contact-details">.

Opening a link in a new tab

By default, links open in the same tab, which is what people expect. If you really do need a link to open in a new tab (for an external site, say), add target="_blank" and rel="noopener" together.

<a href="https://www.example.com/" target="_blank" rel="noopener">Example site</a>

However, you should avoid this type of link unless you have a specific use case (e.g. displaying terms and conditions). Forcing a new tab takes the choice away from the user, and that makes for a poor user experience.

The three kinds of file path

The value of href is a path, the route from one file to another, and there are three ways to write it. Every example below uses this site structure:

1. Absolute paths

An absolute path is the full web address of the target, including the protocol (https://) and the domain name. It always points to the same target, wherever it appears.

<a href="https://www.example.com/about-us.html">About us</a> <a href="https://www.wikipedia.org/">Wikipedia</a>
Pros
  • It works from any page, in any folder, on any website, and even in emails and social media posts.
  • It’s the only way to link to someone else’s website.
  • It’s unambiguous, so there’s no need to count folders.
Cons
  • It’s long to type, and easy to mistype.
  • If your domain name or protocol changes, you’ll need to edit every link.
  • When you test locally, these links take you to the live site rather than your working copy.

Use it for: links to other websites, but avoid it for links between your own pages.

2. Document-relative paths

A document-relative path describes the route to the target starting from the document that contains the link. It has no domain, and just three patterns cover everything you’ll need:

Target is…WriteMeaning
In the same foldercontact.htmlJust the file name
In a folder belowblog/launch-news.htmlFolder name, then /, then file
In a folder above../about-us.html../ means “go up one folder”
Two folders above../../index.htmlRepeat ../ for each level up

The same target needs a different path from different pages. Here are links to the same files, written from two different pages:

<!-- Written in about-us.html (top folder) --> <a href="blog/launch-news.html">Launch news</a> <img src="images/team-photo-london.jpg" alt="The London team"> <!-- Written in blog/launch-news.html (one folder down) --> <a href="../about-us.html">About us</a> <a href="index.html">Blog home</a> <img src="../images/team-photo-london.jpg" alt="The London team">
Pros
  • It works offline, and when you double-click a file to preview it.
  • It’s portable, so you can move the whole site to a new domain or into a subfolder and nothing will break.
  • It’s short and tidy for files that sit close to each other.
  • It’s easy to read when the target is in the same folder.
Cons
  • If you move a page to a different folder, you’ll need to rewrite its paths.
  • Chains such as ../../../ are easy to miscount.
  • You can’t copy a menu from one folder level to another without editing every link.

Use it for: links between your own pages and files, especially on small sites and while you’re learning.

3. Root-relative paths

A root-relative path starts with a forward slash and describes the route to the target from public_html, the root of the website. As with document-relative paths, you don’t need the domain or the protocol.

<a href="/about-us.html">About us</a> <a href="/blog/launch-news.html">Launch news</a> <link rel="stylesheet" href="/css/main-styles.css">

These paths are identical whether they’re written in index.html or deep inside blog/. Note that the leading / means “the root of the website”, not the root of your computer’s drive.

Pros
  • The same path works from every page, so shared menus and footers can be copied anywhere.
  • Moving a page to another folder won’t break its links.
  • It’s shorter than an absolute path, and the domain isn’t hard-coded.
Cons
  • It needs a web server. If you open a file straight from your drive, / points at the root of the drive and the link fails.
  • It breaks if the site lives in a subfolder, such as example.com/project/.
  • It can’t link to other websites.

Use it for: larger sites with shared navigation, and when you test with a local server (such as the Live Server extension in VS Code) rather than by opening files directly.

Which one should I use?

AbsoluteDocument-relativeRoot-relative
Looks likehttps://www.example.com/blog/post.html../blog/post.html/blog/post.html
Starts fromThe full web addressThe current page’s folderThe top folder of the site
Other websitesYesNoNo
Works offlineNo (goes to live site)YesNo (needs a server)
Survives moving the siteNoYesOnly if still at the domain root
Survives moving a pageYesNoYes
Best forExternal linksSmall sites, learningLarge sites, shared menus

A good default for students is to use absolute paths for other websites, and document-relative paths for your own pages.

Common mistakes

Wrong slash direction

Web paths always use forward slashes, even on Windows.

Doimages/team-photo-london.jpg
Don’timages\team-photo-london.jpg

Linking to a file on your own computer

A path that starts with a drive letter only works on your own machine, so the link will be broken for everyone else.

Do../images/team-photo-london.jpg
Don’tC:\Users\sam\Desktop\site\images\team-photo-london.jpg
file:///C:/Users/sam/site/index.html

Case and spelling that don’t match the file

A path must match the file name exactly, including the extension. On a Linux server, About-Us.html and about-us.html are two different files. If you follow the lowercase naming rules in cheat sheet 1, you won’t have this problem.

Doabout-us.html
Don’tAbout-Us.html
about-us
about us.html

Missing the protocol on an external link

Without https://, the browser treats the address as a document-relative path inside your own site, so the link breaks.

Dohttps://www.example.com/
Don’twww.example.com

Counting ../ from the wrong place

Always count from the folder that holds the page you’re editing, not from the folder that holds the target. If an image won’t load, check how many folders up the file really is.

Check before you upload

Learn more

Tip: if a link works on your computer but not online, check the case of the file name first. Then check that you’re not opening a root-relative path without a server.