Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add link in HTML

Learning programming can sometimes feel like learning a new language, and in many ways, it is. Today, we're going to tackle a vital aspect of HTML – creating links. In programming lingo, we call these "hyperlinks".

What is a Hyperlink?

Think of a hyperlink as a bridge. It connects one web page to another, enabling us to navigate through the vast expanse of the internet. Just like bridges connect cities, hyperlinks connect web pages, whether they're on the same website or different ones.

The Anchor Tag

To create a hyperlink, we use something called an anchor tag, denoted by <a> in HTML. You can think of the anchor tag as a 'label' that tells your browser, "Hey, this is a link."

Here's what an anchor tag looks like:

<a href="https://www.example.com">Visit Example.com</a>

In this code snippet, 'Visit Example.com' is the text that will display on your webpage. When a user clicks on this text, they will be directed to the URL you have specified in the href attribute – in this case, https://www.example.com.

Understanding the href Attribute

In our anchor tag, href stands for 'hypertext reference,' which is just a fancy way of saying 'the URL this link should take you to.' You can compare href to the destination address you'd add to your GPS before starting a car journey.

Here's another example:

<a href="https://www.google.com">Click here to visit Google</a>

In this case, 'Click here to visit Google' is the text that will display on your webpage. When clicked, it will take the user to https://www.google.com.

Linking to Different Sections on the Same Page

Sometimes, you might want to create a link that takes the user to a different section of the same webpage, rather than a new one. This is common in long articles that have a table of contents at the top.

It's simple to do this. First, you must assign an id to the section you want to link to. Think of an id as a unique name that you give to a specific part of your webpage. It's like naming your pets so you can call them when you need them.

Here's how you do it:

<h2 id="section1">This is Section 1</h2>

Now, you can create a link that takes the user to this section when clicked:

<a href="#section1">Go to Section 1</a>

The # symbol before 'section1' in the href attribute is crucial. It tells the browser that 'section1' is an id on the same page.

By default, a hyperlink will open in the same browser tab. But what if you want your link to open in a new tab? For that, we use the target attribute with the _blank value. Think of it as telling the browser, "Open this link, but in a new tab."

Here's how you can do it:

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

This code will open https://www.example.com in a new browser tab when the user clicks on 'Visit Example.com'.

You can add a title attribute to your link. This will display a small tooltip when the user hovers their mouse over the link. It's like a brief introduction or description about what they can expect if they click the link.

Here's an example:

<a href="https://www.example.com" title="This link will take you to Example.com">Visit Example.com</a>

Recap

Links are the bridges of the internet, connecting web pages and creating a vast, interconnected network. HTML makes creating these links straightforward with the use of anchor tags, href attributes, and a few other handy tools like the target and title attributes.

Remember to practice! The more you code, the more comfortable you'll become with it. Happy coding!