Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 122 ยท Links and Navigation

Link Targets and Page Jumps

Control where links open with target, keep new tabs safe with rel="noopener noreferrer", and jump to exact sections with fragment links, :target and back-to-top links.

By default a link replaces the current page with a new one, and the new page starts at the top. That is usually what people want. Sometimes, though, you want a link to open in a new tab, or to land in the middle of a long page instead of at the top.

This lesson covers both: the target attribute, which decides where a link opens, and fragment links, which decide which part of a page is shown.

The target attribute

target names the browsing context (a tab, a window or an iframe) where the linked page should open. It accepts a few special keywords:

Value Where the page opens
_self The same tab or frame. This is the default, so you rarely write it.
_blank A new tab or window.
_parent The parent of the current frame. On a normal page, the same as _self.
_top The whole tab, breaking out of every frame.
any other name A tab, window or iframe with that name; created if it doesn't exist yet.

_parent and _top only matter when your page is shown inside an iframe. Named targets are useful in the same situation: give an iframe a name and point links at it.

Opening links in a named iframe

The first two links load their content into the iframe named viewer instead of replacing the page. (Their href values are data: URLs, which carry a tiny page inside the address itself, so the demo needs no extra files.) The last one opens a new tab.

Usually not. Opening a new tab takes control away from the visitor: anyone who wants a new tab can already middle-click, Ctrl+click or long-press a link. Forcing one breaks the Back button for that click and can disorient people using screen magnifiers or screen readers, who may not notice that a new tab appeared.

target="_blank" is reasonable when leaving the page would lose something, for example a help link next to a half-filled form. In that case, tell people that the link opens a new tab:

HTML
<a href="/help/shipping" target="_blank">
  Shipping help (opens in a new tab)
</a>

Keeping new tabs safe with rel

A page opened with target="_blank" used to receive a reference back to your page through window.opener. A malicious site could use it to quietly swap your page for a fake login screen while the visitor was looking at the new tab. That attack is called tabnabbing.

Two rel values close that door:

  • rel="noopener" opens the new page without the window.opener reference.
  • rel="noreferrer" does that and stops the browser from sending the Referer header, so the other site doesn't learn which page the visitor came from.
HTML
<a href="https://example.org/" target="_blank" rel="noopener noreferrer">
  Partner site (opens in a new tab)
</a>

Modern browsers now treat every target="_blank" link as noopener automatically. Writing it explicitly is still a good habit: it documents your intent and protects visitors on older browsers. Add noreferrer when you want privacy as well, but remember that it also hides your site from the other site's analytics.

A fragment is the part of a URL after #. When a link's href is just a fragment, the browser stays on the page and scrolls to the element whose id matches it:

HTML
<a href="#faq">Jump to the FAQ</a>

<!-- ...lots of content... -->

<h2 id="faq">Frequently asked questions</h2>

Any element with an id can be a target: a heading, a section, a table, a figure. The match is case-sensitive, so #FAQ will not find id="faq". Each id must be unique on the page, as the IDs lesson explains.

A table of contents with fragment links

Notice that the section you jump to turns yellow. That is the :target CSS pseudo-class at work, explained below.

Linking to a section of another page

Add the fragment to the end of any URL to land on a specific part of another page:

HTML
<a href="garden-guide.html#watering">Watering tips</a>
<a href="https://example.com/guide#harvest">When to harvest</a>

The browser loads the page first, then scrolls to the element with that id. If no element matches, it simply shows the top of the page; nothing breaks. Because other sites may link to your sections, try not to rename an id once a page is published.

Text fragments. Modern browsers also understand links like https://example.com/guide#:~:text=soil%20moist, which scroll to and highlight the first occurrence of that text, even without an id. You will see them when you copy a "link to highlight" from a browser. They are a handy extra, but a stable id is still the most reliable target.

Styling the target with :target

The CSS :target pseudo-class matches the element named by the current URL's fragment. It is a simple way to show visitors where they landed:

CSS
:target {
  background: #fff7cc;
  outline: 2px solid #e0b000;
}

Two more CSS properties make page jumps nicer:

  • scroll-margin-top leaves room above the target, so a sticky header doesn't cover the heading you just jumped to.
  • scroll-behavior: smooth on the html element animates the jump instead of snapping.
Sticky header and smooth scrolling

Respect reduced motion. Some people get dizzy from smooth scrolling. Wrap it in a media query so it only applies when they haven't asked for less motion: @media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }.

Long pages benefit from a link that returns to the start. HTML has a built-in shortcut for this: href="#top" scrolls to the top of the page even when no element has id="top". A bare href="#" does the same, but #top says what it means.

HTML
<a href="#top">Back to top</a>

If your page does contain an element with id="top", the link scrolls to that element instead. Either way, put the link at the end of long sections or at the bottom of the page, where people actually need it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which target value opens the linked page in a new tab or window?

    _blank opens a new browsing context. _new is not a keyword: the browser treats it as a window named "_new".
  2. 2. What does rel="noopener" prevent?

    noopener removes the window.opener reference, which blocks tabnabbing. noreferrer additionally hides the Referer header.
  3. 3. A link has href="#Pricing" and the heading has id="pricing". What happens?

    Fragments must match the id exactly, including letter case. With no match, the browser just stays where it is.
  4. 4. Your sticky header hides headings after a fragment jump. Which CSS property fixes it?

    scroll-margin-top on the target leaves space above it when the browser scrolls it into view.

Summary

  • target chooses where a link opens: _self (default), _blank, _parent, _top or a named frame or window.
  • Open new tabs sparingly, and say so in the link text when you do.
  • Add rel="noopener" (or noopener noreferrer) to target="_blank" links.
  • A fragment link (href="#id") scrolls to the element with that id; add #id to any URL to link to a section of another page.
  • :target styles the element you jumped to; scroll-margin-top and scroll-behavior improve the jump.
  • href="#top" returns to the top of the page.

Next lesson: Email, Phone and Download Links.