How to Open a Link in a New Tab (HTML target=”_blank”)
Contents
To open a link in a new tab, add target="_blank" to the <a> tag. Add rel="noopener noreferrer" too when the link goes to another site:
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example</a>
That’s the whole answer. Below: what each part does, how to do it from JavaScript, how to make it accessible, and when it’s better not to.
Click the link: it opens in a new tab. The icon shows sighted users what will happen, and the hidden text tells screen reader users. More small wins like this in these HTML tips.
The HTML
target="_blank"
The target attribute decides where the linked page opens. _blank means “a new, unnamed tab or window”. Browsers open a tab by default; users can’t tell the difference unless they’ve changed their settings.
<a href="https://example.com" target="_blank">Opens in a new tab</a>
rel="noopener noreferrer"
A page opened with target="_blank" used to get a reference to your page through window.opener, and could use it to redirect your tab to a phishing page. noopener cuts that link. How that attack works, and when the reference is still useful, is in my post on window.opener.
All current browsers now apply noopener automatically to target="_blank" links, but adding it explicitly is still good practice for older browsers. noreferrer goes further and also hides your page’s URL from the site you link to (it implies noopener). Use noopener alone if you want the other site to see your traffic in their analytics.
<!-- External link: safe and private -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example</a>
<!-- External link that still shows up as a referrer -->
<a href="https://example.com" target="_blank" rel="noopener">Example</a>
Other target values
_self: the same tab (the default)._parentand_top: break out of an iframe, into the parent frame or the whole tab.- Any other name, like
target="docs": opens a tab with that name the first time, and reuses the same tab on later clicks. Useful for “preview” links so they don’t pile up tabs.
<a href="/preview/1" target="preview">Preview post 1</a>
<a href="/preview/2" target="preview">Preview post 2</a> <!-- same tab -->
Open every link on a page in a new tab
The <base> element in the <head> sets the default target for all links. Use it with care: it also affects internal links and forms.
<head>
<base target="_blank">
</head>
With JavaScript
window.open()
window.open(url, '_blank') opens a new tab from code. Pass 'noopener' as the third argument for the same protection as the HTML attribute. It’s one of those JavaScript basics with a catch: browsers only allow this in response to a user action, like a click; calls from timers or page load get blocked as pop-ups.
button.addEventListener('click', () => {
window.open('https://example.com', '_blank', 'noopener');
});
Make all external links open in a new tab
If you can’t change the HTML (content from a CMS, Markdown), set the attributes with a small script. Markdown has no syntax for target, so this is the usual approach there.
document.querySelectorAll('a[href^="http"]').forEach((link) => {
if (link.hostname !== location.hostname) {
link.target = '_blank';
link.rel = 'noopener noreferrer';
}
});
Should you open links in a new tab?
Most of the time, no. Users can always open a link in a new tab themselves (Ctrl/Cmd + click, or middle-click), but they can’t force a _blank link to stay in the same tab. A new tab also breaks the Back button, which confuses many people, especially on mobile.
Good reasons to use it:
- The user is in the middle of something they’d lose, like filling in a form or watching a video.
- Help or documentation links from inside an app.
- Files that open in the browser, like PDFs.
Whatever you choose, tell users a link opens a new tab, with an icon and screen-reader text like in the demo above. That’s a WCAG recommendation, not just a nicety.
FAQ
How do I open a link in a new tab in HTML?
Add target="_blank" to the link: <a href="https://example.com" target="_blank" rel="noopener noreferrer">.
Is rel=”noopener” still needed?
Modern browsers apply it automatically to target="_blank" links, so it’s no longer strictly required. It’s still a good habit, and some linters and SEO tools flag links without it.
Does target=”_blank” affect SEO?
No. Search engines don’t care whether a link opens in a new tab. rel="noopener noreferrer" doesn’t affect SEO either; it’s unrelated to nofollow.
Can I open a link in a new tab with CSS?
No. Where a link opens is behavior, not style, so it needs the HTML attribute or JavaScript.
How do I open a link in a new tab in WordPress?
In the block editor, select the link text, click the link, and switch on Open in new tab. WordPress adds target="_blank" and rel="noopener" for you.
Related: HTML tips and tricks, HTML input types and HTTP status codes.