Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a link in HTML

Imagine you're reading your favorite detective novel, and you come across a character reference that you can't quite remember. Wouldn't it be handy if you could just tap on the character's name and get a brief summary of who they are and what their role is in the story? That's precisely what a link or hyperlink does on the web. It acts as a portal, or a magic door, that transports you from one set of content to another.

In HTML, a hyperlink is created using the <a> tag, which stands for 'anchor'. This tag takes a few attributes, but the most important one is href, which stands for 'HyperText Reference'. This attribute tells the browser where to go when the link is clicked.

Here's a basic example:

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

When this code is rendered by the browser, it will display the text "Go to Google". When you click on that text, it will take you to www.google.com.

The Importance of href

The href attribute is where the 'magic' happens. This attribute contains the URL or the web address of the page that you want to link to. The URL can be a full web address (like "https://www.google.com"), a relative path to a file on your website, or even a location on the same page.

Here's a code snippet showing different href values:

<!-- Link to an external website -->
<a href="https://www.google.com">Go to Google</a>

<!-- Link to another page on your website -->
<a href="/about.html">About Us</a>

<!-- Link to a location on the same page -->
<a href="#section2">Go to Section 2</a>

The third example in the above code snippet is interesting. When the href attribute starts with a #, it refers to an 'id' on the same page. This is particularly useful for long web pages, where you might want to provide quick navigation to different sections.

For example, if you have an element with the id "section2", like this:

<h2 id="section2">Section 2</h2>

The link <a href="#section2">Go to Section 2</a> would take the user to this section of the page when clicked.

The target Attribute

Sometimes, you might want a link to open in a new browser tab or window. This is where the target attribute comes in. This attribute can have several values, but the most commonly used one is _blank, which tells the browser to open the link in a new window or tab.

Here's an example:

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

When this link is clicked, it will open Google in a new tab.

Web accessibility is a crucial aspect of web development. It ensures that people with disabilities can use the web. For links, it means making sure that the purpose of the link is clear from its link text.

Avoid link texts like "click here" or "read more". Instead, use descriptive link texts that explain where the link goes or what it does. This helps users who use screen readers and also improves SEO (Search Engine Optimization, a practice that improves your site's visibility on search engines).

Here's an example of a more accessible link:

<a href="/about.html">Learn more about our company</a>

Recap

Links are fundamental to the web, allowing us to navigate from one page to another or even within the same page. They are created in HTML using the <a> tag and the href attribute, which contains the URL of the destination page. The target attribute can be used to control how the link is opened, and it's important to use clear, descriptive link text for accessibility purposes.

Remember, creating links is like creating a network of doors between your pages, making it easy for users to explore and find the information they need. So, keep practicing and experimenting with different attributes and types of links. Your web development journey is just beginning!