Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a hyperlink in HTML

In the vast universe of the internet, websites are like individual planets. And just like in a real universe, all these planets (websites) are connected by an intricate network of pathways. In the world of web, these pathways are known as hyperlinks, or simply links.

Hyperlinks are a fundamental part of the internet. They allow us to navigate from one web page to another, from one website to another, or even to a different section of the same page. If you've ever clicked on a word or a phrase that redirected you to a different page, then you've used a hyperlink.

In HTML, a hyperlink is created using the <a> tag. This tag is short for 'anchor'. Just like an anchor connects a ship to the bottom of the sea, the <a> tag connects a piece of text or an image to another webpage or another part of the same webpage.

Here is a basic example:

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

In this example, the <a> is the HTML tag that creates the hyperlink. The href attribute inside the tag is used to specify the URL of the page that the link should go to. The text between the opening <a href="..."> tag and the closing </a> tag is the text that will be displayed as the hyperlink on the web page.

Let's break it down into steps.

  1. First, decide on the text that you want to turn into a hyperlink.
  2. Next, wrap that text with the <a> tag.
  3. Finally, in the opening <a> tag, add the href attribute followed by the URL of the webpage you want to link to.

Here's an example where we're linking the text "Visit our homepage" to a homepage.

<a href="https://www.homepage.com">Visit our homepage</a>

Linking to Different Sections on the Same Page

Hyperlinks can also be used to jump to different sections of the same page. This is especially useful for long pages with different sections.

To do this, we use the id attribute in conjunction with the <a> tag. First, we give the section we want to link to an id, like this:

<h2 id="section1">Section 1</h2>
<p>This is Section 1 of the page.</p>

Then, we create a hyperlink to that id using the <a> tag:

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

Notice how the href attribute contains a hash symbol (#) followed by the id of the section. This tells the browser to look for an element with that id on the current page and scroll to it when the link is clicked.

When creating hyperlinks, it's important to use descriptive text. "Click here" or "Read more" are not very descriptive and don't give any indication of where the link leads to. Instead, use text that describes the destination of the link. For example, "Visit our homepage" is more descriptive than "Click here".

Well, that's it! You now know how to create hyperlinks in HTML. Remember, the <a> tag is your friend, and always use descriptive link text. Happy coding!