HTML Links

Links are one of the most important features of the web. They allow users to move between pages, navigate websites, and jump to specific sections of content. Without links, the web would be a collection of isolated pages instead of a connected network of information.

In this lesson, you’ll learn how HTML links work and how to create effective hyperlinks using the <a> (anchor) element. You’ll practice creating text and image links, understand the difference between absolute and relative URLs, and learn how to enhance links using additional attributes.

By the end of this lesson, you’ll be able to confidently connect pages and build meaningful navigation for your websites.

Desired Outcomes:

By the end of this lecture, you should be able to:

  • Understand the role of links in web navigation
  • Create clickable text and image links
  • Differentiate between absolute and relative URLs
  • Apply attributes to customize link behavior

Let's dive into the world of HTML links and learn how to create effective and interactive hyperlinks.

Text links are the most common type of link on the web. They are created using the <a> (anchor) element. The text inside the anchor tag becomes clickable and takes the user to another page or location.

<a href="destination_url">Link Text</a>
  • href specifies the destination URL
  • The text between the tags is what users click

Example:

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

In this example, clicking Visit Example.com will navigate the user to the specified website.

Best practice

Always use clear and descriptive link text so users know where the link will take them.

Links don’t have to be text — images can also be clickable. To create an image link, place the <img> element inside an <a> element.

<a href="destination_url">
    <img src="image_source" alt="Image Description">
</a>

Replace "destination_url" with the URL you want the link to point to. Set the "src" attribute of the <img> tag to the image source URL. The "alt" attribute provides alternative text for the image, serving as a description for visually impaired users or if the image fails to load.

Example:

<a href="https://www.example.com">
    <img src="image.jpg" alt="Example Image">
</a>

In this example:

  • The image becomes clickable
  • Clicking the image navigates to the specified URL
  • The alt attribute provides accessibility and fallback text

Image links are commonly used for logos, banners, and navigation icons.

Absolute and Relative URLs

Links can point to different web pages or sections within the same page. There are two types of URLs: absolute and relative.

  • Absolute URLs: An absolute URL specifies the complete web address, including the protocol (http:// or https://). Example: "https://www.example.com"
  • Relative URLs: A relative URL specifies the path to the file or location relative to the current webpage. Example: "about.html" or "../contact.html"

Absolute File Paths

Absolute file paths specify the complete URL or file path from the root directory. They provide the full address for a file, including the domain name. Absolute file paths are useful when linking to external websites or when you want to specify the exact file location.

Absolute Path Example:

<a href="https://www.example.com/about.html">About</a>

In this example, clicking on the "About" link will navigate to the "about.html" page on the "www.example.com" domain.

Relative File Paths

Relative file paths specify the location of a file relative to the current webpage's location. By using relative file paths, you can link pages within your website without relying on the complete URL.

Let's understand the concept of relative file paths.

  • The file path "./" represents the current directory (folder). For example, "./about.html" points to an "about.html" file in the current directory.
  • The file path "../" represents the parent directory (folder). For example, "../contact.html" points to a "contact.html" file in the parent directory.

Relative Path Example:

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

In this example, clicking on the "About" link will navigate to the "about.html" page in the current directory.

You can customize link behavior and appearance by using additional attributes:

  • target: Specifies how the link opens. For example, you can set target="_blank" to open the link in a new tab or window.
  • download: Specifies that the linked resource should be downloaded when clicked. It is commonly used for file downloads.

Example:

<a href="https://www.example.com/some-pdf.pdf" 
     target="_blank" download>
     Download Example
</a>

In this example, the link will open in a new tab or window when clicked, and if the linked resource is a file, it will be downloaded.

Remember to use descriptive and meaningful link text to improve accessibility and user experience.

Linking to a Specific Page Section

HTML also allows you to create links that navigate to specific sections within a single webpage. 

To link to a specific section within the same webpage, you can use anchor links. Anchor links allow users to jump directly to a particular section by clicking on a link. They are created using the id attribute to identify the target section and the href attribute to specify the target with a preceding # symbol. 

Example:

<a href="#section2">Go to Section 2</a>

In this case, the id attribute section2 should be assigned to the target section:

<section id="section2">
    <h2>Section 2</h2>
    <p>This is the second section of the webpage.</p>
 </section> 

When the anchor link is clicked, the webpage will scroll to the section with the id of section2.

If you would like to link to a specific page section on another page, all you have to do is specify the URL of the page followed by an anchor, like so:

<a href="./about-page.html#section2">Go to Section 2 on the About page</a>

Understanding file paths and creating links between HTML pages is essential for building a well-structured and interconnected website. Experiment with different file path techniques and explore the possibilities of linking pages together.

Congratulations on learning how to link HTML pages! In the next lecture, we'll explore HTML lists, which allow you to present information in organized and structured formats.

Keep practicing and implementing these techniques to enhance the navigation experience for your website visitors.

Conclusion

Links are the backbone of web navigation. By mastering text links, image links, URLs, and anchor links, you can create websites that are easy to explore and logically connected.

Understanding how links work also prepares you for more advanced topics like navigation menus, accessibility improvements, and SEO-friendly site structures.

In the next lesson, you’ll learn about HTML lists, which allow you to present information in structured and organized formats.

This lesson is part of our complete Learn HTML guide.

Updated on:

-33%

Time remaining:
days 00: 00: 00

Learn to code HTML & CSS

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

HTML & CSS: From Zero to Hero
Learn to code HTML & CSS

-33%

Time remaining:
days 00: 00: 00

Learn to code JavaScript & jQuery

From Zero to Hero

Check out this course on Udemy, now at 75% off! Inside this interactive course, I will teach you how to develop components from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world reusable components.

JavaScript & jQuery: From Zero to Hero
Learn to code JavaScript & jQuery

-25%

Time remaining:
days 00: 00: 00

Learn Frontend Web Development

From Zero to Hero

Check out this course on Udemy, now at 75% off! A bundle of the previous two courses. Inside this interactive course, I will teach you how to develop websites from scratch moving from beginner to advanced concepts. I take time to explain every detail and finish off by building some real-world websites.

Frontend Web Development: From Zero to Hero
Learn Frontend Web Development