Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to put a link in HTML

Getting Started

Welcome to the world of programming! If you've ever dreamt of creating elegant web pages, you're in the right place. Today we're going to discuss a fundamental aspect of HTML: how to put a link in HTML. This is an essential skill for any budding web developer, so let's dive in!

Before we start, it's important to understand what a link is in the context of HTML. In simple terms, a link or hyperlink is a way for users to navigate between pages on the internet. Think of it as a bridge that connects two different locations, allowing you to travel from one point to another.

The Anchor Tag: Your Bridge Builder

The key to creating a link in HTML is the 'anchor' tag. This is denoted by the less-than and greater-than symbols with the letter 'a' in between, like this: <a>.

But an empty anchor tag isn't going to do much. It's like having a bridge with no destination. To make it functional, we need to add an attribute.

The HREF Attribute: Defining the Destination

In HTML, attributes provide additional information about an element. For the anchor tag, we primarily use the 'href' attribute to specify the link's destination.

Imagine you're the captain of a ship. The anchor tag is your ship, and the 'href' attribute is your map, showing you where to go.

Here's what an anchor tag with a href attribute looks like:

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

In the above example, 'https://www.google.com' is the URL (or destination) you want to link to, and "Visit Google" is the clickable text that users will see.

There are two main types of links you can create in HTML: absolute and relative.

An absolute link is a complete URL, pointing to an external webpage. It's like the full address of a house, with the street name, city, and zip code.

Here's an example of an absolute link:

<a href="https://www.wikipedia.org/">Go to Wikipedia!</a>

When clicked, this link takes you directly to the Wikipedia homepage.

A relative link, on the other hand, points to a file within the same website. It's like saying "the kitchen is next to the living room" in a house.

If you had a webpage called 'about.html' in the same directory as your current page, you could link to it like this:

<a href="about.html">About</a>

This link will take you to the 'about.html' page when clicked.

Open in a New Tab: target attribute

Sometimes, you might want a link to open in a new tab or window. This is where the 'target' attribute comes into play, with _blank as its value.

Consider this your teleportation device, instantly transporting you to a new location (or in this case, a new browser tab).

Here's an example:

<a href="https://www.google.com" target="_blank">Visit Google</a>

Clicking this link opens Google in a new tab, leaving your original page open in the previous tab.

Wrapping Up

Congratulations, you've just learned how to put a link in HTML! You now know how to use the anchor tag, the href attribute, and the difference between absolute and relative links.

Remember, practice makes perfect. The more you use these tags and attributes, the more comfortable you'll become with them. So go ahead, start building bridges and teleporting around the web with your new skills. Happy coding!