Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a link in HTML

Understanding the Basics

Before we start, it's important to understand what a link is. In the simplest terms, a link, or a hyperlink, is a way to connect two web pages. It's like a bridge that allows you to travel from one place to another on the internet. In HTML, we use the <a> tag to create a link.

But let's not get ahead of ourselves. Let's take this one step at a time.

What is an <a> tag?

The <a> tag is a fundamental building block of HTML. It's like a door in your house that allows you to move from one room to another. In HTML, the <a> tag allows you to navigate from one web page to another, or even to a different place on the same page.

Here's what an <a> tag looks like in its simplest form:

<a href="http://www.example.com">Click here</a>

In this example, http://www.example.com is the destination of the link, and Click here is the text that will be displayed on the web page. When you click on the text, you will be taken to the destination URL.

The href Attribute

The href attribute is like the address you put into your GPS. It tells the browser where to go when the link is clicked. The value of the href attribute is the URL of the webpage you want to link to.

Here's an example:

<a href="http://www.google.com">Visit Google</a>

In this example, when the text Visit Google is clicked, the browser will navigate to http://www.google.com.

Relative vs Absolute URLs

When specifying the href attribute, you can use either an absolute URL or a relative URL. An absolute URL is like a full address, it contains all the information needed to find a particular location. On the other hand, a relative URL is like directions from your current location.

For example, an absolute URL looks like this:

<a href="http://www.example.com/page.html">Link Text</a>

And a relative URL might look like this:

<a href="page.html">Link Text</a>

In the relative URL example, page.html is located in the same directory as the current page. If it was in a directory called pages, you would write href="pages/page.html".

You can also add a title to your link using the title attribute. This is like giving your link a label that describes where it leads. The title will be displayed as a tooltip when you hover over the link.

Here's an example:

<a href="http://www.example.com" title="Example Website">Visit Example</a>

When you hover over the link text Visit Example, a small box will appear with the text Example Website.

Sometimes, you might want to open a link in a new tab. This can be done using the target attribute. The target attribute is like a command that tells the browser how to open the link.

Here's an example:

<a href="http://www.example.com" target="_blank">Visit Example</a>

In this example, target="_blank" tells the browser to open the link in a new tab or window.

Linking to an Email Address

You can also create a link that opens a new email in the user's default email program. This is done using the mailto: scheme.

Here's an example:

<a href="mailto:example@example.com">Email us</a>

In this example, when the user clicks on Email us, a new email will open in their default email program with example@example.com in the To field.

Conclusion

Creating a link in HTML is like creating a door that leads from one web page to another. Using the <a> tag and the href attribute, you can create a simple link. You can also use the title attribute to add a tooltip to your link, the target attribute to control how the link is opened, and the mailto: scheme to create a link that opens a new email. Remember, the best way to learn is by doing, so try creating some links of your own!