Markdown

Creating Links in Markdown: A Complete Guide on Creating, Formatting, and Optimizing Links to URLs, Sections, Files, Emails, and More

In digital content creation, links connect the information and enhance the user experience. Markdown, a lightweight markup language, is a popular choice for creating documents, blogs, and other forms of online content due to its simplicity and ease of use.

This comprehensive guide explores the various techniques to create, format, and optimize links, be it URLs, sections within the same document, local files, email addresses, and more.

Markdown Link Syntax

Links are utilized to link to other websites, documents, or resources in Markdown. When you click on these links, you can move to another page.

Before we dive into the specifics of creating different types of links in Markdown, let’s start with the basics. Link creation in Markdown is done using a straightforward syntax.

Here’s the basic structure of a link in Markdown:

[Link Text](URL)]

This syntax is used to create a basic inline link in Markdown. It allows you to insert a hyperlink in your Markdown text with a visible link text and a target URL.

Here, [Link Text] is the text that we want to display as the clickable link. It appears in square brackets [ ]. It can be any text that we choose and should specify the link’s purpose or content.

Then, we have the Uniform Resource Locator (URL) or web address that we want to link. It appears in parentheses ( ). We place the URL inside the parentheses without any quotation marks or additional formatting. It typically starts with “http://” or “https://” for web links.

Now that we’re familiar with the fundamental link syntax, let’s explore the various techniques for creating and optimizing links in Markdown.

Creating Hyperlinks to URLs

The most common use of links in Markdown is to direct the readers to external websites. Crafting a link to a URL is straightforward; you only need to replace the [Link Text] with the text that you want to showcase and replace the (URL) with the actual web address.

Here’s an example:

[Visit Google](https://www.google.com)

Let’s break down this script.

In this example, the [Visit Google] is the link text that is enclosed within the square brackets. The text is displayed as the clickable link in our Markdown document. In this case, “Visit Google” is the link text. Then, we provide the URL enclosed within the parentheses which is the web address to which the link takes the reader when they click on the link text. In this case, it’s “(https://www.google.com)” which is the URL for Google’s homepage.

So, when this Markdown code is rendered, it appears as follows:

Clicking on the “Visit Google” link takes us to the Google homepage at “https://www.google.com.”

This is the basic syntax for creating a hyperlink in Markdown using the inline links where we specify the link text and the corresponding URL within the brackets and parentheses, respectively.

Linking to Specific Sections (Anchors)

Linking to specific sections or anchors within a Markdown document is valuable, especially for long articles, documentation, or web pages. It allows the readers to jump directly to a specific part of the content without scrolling through the entire document. To achieve this, we need to create the anchor links within our Markdown document and then link them to those anchors.

Here’s a step-by-step guide with a complete example:

We need to use the HTML tags to create an anchor within our Markdown document because Markdown itself doesn’t provide a native way to create anchors.

## Table of Contents <a name="table-of-contents"></a>

In this example, we create an anchor named “table-of-contents” within a level-two header entitled “Table of Contents.” The <a name=”table-of-contents”></a> HTML code defines the anchor, and “table-of-contents” is the name that we use when linking to this specific section.

Now that you created an anchor, you can create a link that navigates to this anchor. To do this, follow these steps:

[Go to Table of Contents](#table-of-contents)

In this step, we create a link that says, “Go to Table of Contents.” The key part here is the URL in the link: #table-of-contents. This URL is a reference to the anchor. We generated it in the previous phase. When we click on this link, the document automatically scrolls to the “Table of Contents” section, making the navigation more manageable and user-friendly.

We then put it all together to gather a complete example on how the anchor and link work together in a Markdown document:

# My Document

Welcome to my document. This is the introduction.

## Table of Contents <a name="table-of-contents"></a>

This is the table of contents. It contains links to various sections of this document.

### Section 1

This is the first section.

### Section 2

This is the second section.

#### Section 3

This is the third section.

##### Section 4

This is the fourth section.

[Go to Table of Contents](#table-of-contents)

This example starts with the “My Document” heading and an introduction. Then, we have a “Table of Contents” section with the anchor tag which serves as the entry point for the link. Following that, we have four sections: “Section 1”, “Section 2”, “Section 3”, and “Section 4.”

At the end of the document, we create a link that says, “Go to Table of Contents”. This link takes us directly to the “Table of Contents” section, improving the overall user experience.

This technique is beneficial when we have lengthy documents with multiple sections or we want to provide a quick way for readers to jump to specific parts of our content.

Link to Local Files

Linking to local files in Markdown can be useful when we want to provide links to documents, images, or other files that are stored on our local system or within the same directory as our Markdown document.

The general syntax for linking to local files in Markdown is the same as for web URLs, but we must provide the local file path instead of a URL.

Here’s the format:

[Link Text](file-path)

We want to display the link text as the link in our Markdown document, while the file-path is the relative or absolute path to the local file that we want to link to.

Let’s create an example.

Suppose we have a local PDF document named “example.pdf” in the same folder as our Markdown document. We can create a link to this local PDF like this:

[Link to Local PDF](example.pdf)

In this example, “Link to Local PDF” is the link text that is displayed in your Markdown document, and “example.pdf” is the local file that we want to link to.

After clicking on the link, we will be prompted to download or open the “example.pdf” file.

Here, you can see that the content of the specified file has been displayed in the following snapshot:

An important thing to note here is that the browser or security settings may restrict the ability to open the local files directly from a web page, and it might not work consistently across different platforms or Markdown processors.

Relative Links

Relative links in Markdown are used to create links to other files or resources within the same directory or folder structure. These links are related to the Markdown file’s current position.  Relative links are helpful when you want to keep your file structure organized and easily portable.

Linking to Another Markdown File Using a Relative Path

To link to another Markdown file from within a Markdown document, we can use the relative links which specify the path to the target Markdown file relative to the location of the current Markdown file.

Let’s suppose we have two Markdown files, “file1.md” and “file2.md”, located in the same directory, and we want to create a link from “file2.md” to “file1.md”.

Here is our “file1.md”:

In “file2.md”, use the following Markdown syntax to create a link to “file1.md”:

[Click here to read more](file1.md)

In this example, “Click here to read more” is the text that is displayed as the clickable link. The “file1.md” is a relative file path. Relative paths in Markdown are interpreted based on the location of the current Markdown file. If both the Markdown file containing this link and “file1.md” are in the same directory, this relative link will work.

When this Markdown code is rendered in a Markdown viewer or on a website, it appears as a clickable link.

Clicking on this link takes us to the content of the “file1.md” Markdown file in the same directory as the current file.

We can view the contents of “file1.md” here:

Linking to an Image Using a Relative Path

If we want to link to an image or any other type of file (e.g., PDF, text file) in the same directory, we can also use the relative path. To link to an image, you typically provide an image file’s path or URL, along with an optional alternative text for the image.

If our Markdown file and the image file are both located in the same directory, we can use a relative path to link to it. For instance, we have an image file named “images.jpg” in the same directory as our Markdown file:

![Image of a cat](images.jpg)

The [Pie Chart] is the image’s alternative text which is shown in case the specified image can’t be displayed or for accessibility reasons. Then, we provide the file name since our image is located in the same directory as the current markdown file. If the picture is online-hosted, we provide the complete URL.

Here is the output:

Internal Links

Internal links in Markdown allow us to create hyperlinks within the same document or to the other sections of the document. This is helpful in making a table of contents, cross-referencing sections, or providing an easy navigation within longer documents. We can use the anchor tags or reference-style links to create internal links in Markdown.

Here’s an example of an internal link:

[Visit the About page](about.md)

This Markdown script provides a link to a file named “about.md” in the same directory or location where the Markdown document is. When this link is rendered, it can be used to navigate to the “About” page if the file exists and if the Markdown rendering environment supports such file-based linking.

Creating Email Links

In Markdown, you can create email links using a simple syntax to turn a text label into a clickable link that opens the default email client with a pre-filled recipient email address.

For example, if we want to create a “Contact Us” link that opens the default email client with the pre-filled email address of “[email protected]“, we can use this Markdown code:

[Contact Us](<a href="mailto:[email protected]">mailto:[email protected]</a>)

When this Markdown code is rendered in a Markdown viewer or on a website, it appears as a clickable email link:

Clicking on this link opens the user’s default email client and populates the “To” field with the “[email protected]” email address.

Formatting the Links

Formatting the links in Markdown often involves customizing the appearance of the link text. We can format the links to make them bold, italic, or underlined. Here’s how to format the links using various Markdown formatting options:

Bold Links

To make a link text bold, we can use the double asterisks or double underscores to enclose the link text.

**[Bold Link](https://www.example.com)**

The double asterisks (**) are used for text formatting and to indicate that the enclosed text should be in bold. In this case, “Bold Link” is the link text that will be displayed in bold. Then, we provide a URL that specifies where the link will take us when we click on the link text.

When this Markdown code is rendered, the “Bold Link” link text will appear in bold, and clicking on it navigates to “https://www.example.com“.

Italic Links

To make a link text italic, we can use single asterisks or single underscores to enclose the link text.

*[Italic Link](https://www.example.com)*

This will be rendered as:

Underlined Links

To underline the link text, we can use the HTML <u> tags which is not a standard Markdown feature but may work in some Markdown processors.

<u>[Underlined Link](https://www.example.com)</u>

This renders as:

These formatting options can help us emphasize the link text in our Markdown documents, making the links stand out and providing additional visual cues to the reader.

Conclusion

Markdown provides a simple yet powerful way to create, format, and optimize the links to various destinations including URLs, sections within the same document, local files, email addresses, and more. In this guide, we thoroughly explained each one of these practices. By mastering the techniques, you can enhance the usability and interactivity of your Markdown content.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.