HTML Links: href, Absolute and Relative URLs, and rel
Lesson 7 of 36 in the HTML5 learning path.
How do I write accessible links with safe href and rel values?
An HTML link is an anchor whose href is a destination and whose text names that destination. The href may be a file in the same folder, a path from the site root, an absolute URL, a mailto address, or a fragment that jumps to an id on the page. The readable text matters as much as the URL. Screen readers often list links out of context, so Read the syllabus works and click here does not.
Use a link when the action navigates or opens mail. Use a button when the action stays on the page and runs a script. Add rel="noopener noreferrer" when target="_blank" opens a new tab, so that tab does not keep a handle back to your page. Write href values from paths you control, and do not drop an untrusted string into href. A course menu, a citation, and an email address are all anchors.
Syntax & Parameters
<a href="syllabus.html">Syllabus</a> <a href="https://example.edu" target="_blank" rel="noopener noreferrer">Reference</a>
Example
Interactive preview available<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Course links</title>
<style>
body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
h2 { font-size: 1.25rem; margin: 1.4rem 0 0.45rem; }
p, li { margin: 0 0 0.75rem; }
a { color: #1d4ed8; }
footer { max-width: 42rem; margin: 0 auto; padding: 0 1.5rem 2.5rem; color: #44403c; }
</style>
</head>
<body>
<header>
<p>North Campus, Web Systems 1</p>
</header>
<main>
<h1>Course links</h1>
<p>Each anchor has a readable name and an href that goes somewhere real. The name is the text inside the anchor, not a bare URL and not the words click here.</p>
<nav aria-label="Course">
<p>
<a href="links.html" aria-current="page">Links lesson</a>
<a href="syllabus.html">Syllabus in this folder</a>
<a href="/labs">Lab index from the site root</a>
</p>
</nav>
<h2 id="office-hours">Office hours</h2>
<p><a href="#office-hours">Skip to office hours</a> stays on this page and moves the address to this heading.</p>
<p>Email the instructor at <a href="mailto:studio@learn.example.edu">studio@learn.example.edu</a> before Thursday at 15:00.</p>
<!-- target plus rel keeps a new tab from retaining a handle back to this page. -->
<p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a" target="_blank" rel="noopener noreferrer">HTML anchor reference on MDN</a></p>
</main>
<footer>
<p>Department of Computer Studies</p>
</footer>
</body>
</html>
Expected Output
The page shows a heading, a short nav, an Office hours heading, a mail link, and the MDN reference. Choosing Syllabus in this folder requests syllabus.html in the current directory. Choosing Lab index from the site root requests /labs on the host. Choosing Skip to office hours scrolls to the heading and the URL ends in #office-hours. Choosing the email address opens a mail draft addressed to studio@learn.example.edu. Choosing the MDN reference opens a new tab to the absolute https URL and leaves this tab in place. The new tab does not receive a usable window.opener because rel includes noopener.
Line-by-Line Walkthrough
- The nav groups three in-site links so they are a navigation landmark, not a loose row of anchors.
- href="links.html" is a relative URL in the same folder, and aria-current="page" marks the lesson you are already reading.
- href="syllabus.html" is also same-folder, so it keeps working if the course folder is renamed as a unit.
- href="/labs" is root-relative. It always means the labs path on this host, not a labs file beside the current page.
- href="#office-hours" looks for the element whose id is office-hours and scrolls it into view. The address bar gains that fragment.
- The mailto link uses the address as both the href and the visible text, so the destination and the name match.
- The MDN anchor uses an absolute https URL, target="_blank" to open a new tab, and rel="noopener noreferrer".
- noopener stops the new tab from using window.opener to reach back into this page. noreferrer also omits the Referer header.
- None of the names say click here. Each name can stand alone in a list of links.
Activate each link in the browser. Same-folder and root-relative links request different paths, the fragment jumps to the heading, mailto opens a message, and the reference opens a new tab.
Common Pitfalls & Security
A link is a navigation. The dangerous mistakes are an href you do not control, a new tab that keeps a handle to the opener, and a name that does not say where the person is going.
- target="_blank" without rel="noopener" lets the opened page reach window.opener on older browsers and replace the tab the student came from.
- Putting a user-supplied string straight into href can turn a link into a javascript scheme that runs on click. Allow only the schemes you expect.
- "Click here" and raw URLs as the only link text fail when a screen reader lists links, and they fail when a link is copied into a search snippet.
- A relative href="labs" from /courses/web/links.html requests /courses/web/labs, while href="/labs" requests /labs. Mixing those up sends students to the wrong file.
- javascript: links and anchors with no href are not links. If the control does not navigate, use a button.
Best Practices & Modern Standards
HTML5 anchors need an href and a name that makes sense alone. Prefer same-folder or root-relative URLs for your own pages so the site can move hosts, and use absolute URLs for other sites. Open a new tab only when leaving the page would throw away work the student still needs, and always pair that with rel="noopener noreferrer".
Mark the current page with aria-current="page" instead of removing the href. Keep the destination readable: a syllabus link should say syllabus. Test the paths from the real folder the file will live in, not only from a preview that rewrites URLs.
Real-World Use
The course links page in the Web Systems folder is the menu students use in week one. Syllabus is a file beside links.html. The lab index is served from the site root. Office hours is a jump on the same page, and the instructor email is a mailto link with the address visible.
The MDN citation at the bottom opens in a new tab so the lesson stays open. rel="noopener noreferrer" is on that citation because it leaves the department site. The same rules apply to a handbook article that links to readings and to a footer that links to the campus home page.
Frequently Asked Questions
When should a URL be root-relative instead of a filename?
Use a filename or a same-folder path when the target lives next to the current file and should move with it. Use a root-relative path such as /labs when the target is a stable site path from the host root. An absolute URL is for another site.
Do I need rel on every external link?
You need rel="noopener noreferrer" when the link uses target="_blank". An ordinary external link that replaces the current tab does not keep window.opener, so noopener is less important there. noreferrer also drops the referrer, which you may want when leaving a signed-in page.
What is wrong with an anchor that has no href?
Without href, the anchor is not a link. It is not in the tab order the way a link is, and it has nowhere to go. Use a button for an on-page action. Use an anchor only when you have a real destination.
How does a fragment link differ from a button that scrolls?
A fragment such as #office-hours updates the URL, so the jump can be shared and restored. The browser also moves focus to the target when that target is focusable or has a heading in modern browsers. A script that only scrolls can leave the URL and the focus unchanged.