Skip to content

How to open link in new tab in markdown?


Markdown is a great lightweight markup language. The Markdown can be used for everything. People use it to create websites, documents, notes, books, presentations, email messages, and technical documentation.

To create a link in Markdown, enclose the link text in brackets and follow it immediately with the URL in parentheses.


The above will give us this ->

But if you click on the link, you will notice that it opens the link in the same tab as this article. It’s great if I want to refer to other articles within this website, but when I am referring to another website, I want it to open the link in another tab.

I want this ->

So, there are a couple of solutions to achieve this.

Solution 1: Using anchor tags

<a href="" target="_self"></a>

The only downside is that your markdown document is no longer purely Markdown as it contains HTML.

Solution 2: Using a base element

<base target="_blank" />

You can place this at the top of the markdown page. This will add target=“_blank” to every link on your page.

Install the rehype-external-links plugin.

npm install rehype-external-links

Import the plugin into your astro.config.mjs file.

// ...
import rehypeExternalLinks from "rehype-external-links";

export default defineConfig({
  // ...
  markdown: {
    rehypePlugins: [
          target: "_blank",

This will add target="_blank" to all links that are linked to external sources.