Create anchor, new-tab and mailto links in HTML
3 Three kinds of link
fairWrite a link that jumps within the page, a link that opens a new tab safely, and a link that starts an email. Say out loud, in the link text, which one leaves the page.
Stuck? Three hints, smallest first
- 1. An in-page link is
href="#id"pointing at an element with thatid. - 2.
target="_blank"needsrel="noopener", or the new page gets a handle on yours. - 3. A new tab with no warning is disorienting. Put it in the link text, not only in an icon.
<p><a href="#fares">Jump to the fares table</a></p>
<p><a href="https://example.com/timetable.pdf" target="_blank" rel="noopener noreferrer">
Full timetable (PDF, opens in a new tab)</a></p>
<p><a href="mailto:tickets@example.com?subject=Refund">Email the ticket office</a></p>
<h2 id="fares">Fares</h2>
<p>Lagos to Abuja is ₦18,000 one way.</p>
a {
color: #3055d8;
text-decoration: underline; /* keep it; the underline is the affordance */
text-underline-offset: 2px;
}
a:hover { color: #2543b4; }
a:focus-visible {
outline: 2px solid #17171a;
outline-offset: 2px;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('an in-page link points at a fragment', /href="#fares"/.test(html), true);
test('and something on the page carries that id', /id="fares"/.test(html), true);
test('the new tab is opened deliberately', /target="_blank"/.test(html), true);
test('and rel="noopener" comes with it', /rel="noopener/.test(html), true);
test('the link text warns that it leaves the page', /opens in a new tab/i.test(html), true);
test('an email link is a real href, not a handler', /href="mailto:/.test(html), true);
test('the underline is kept, not removed', /text-decoration\s*:\s*none/.test(css), false);
test('keyboard focus is visible', /:focus-visible/.test(css), true);
Output
rel="noopener" is the one to remember: without it the opened page can reach back through window.opener and navigate the tab it came from. Modern browsers imply it for target="_blank", but stating it is still the honest habit because you cannot know which browser you got. The warning in the link text matters for the same reason a new tab is jarring on a screen reader: focus moves to a document that was never announced.