fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← HTML

HTML / 7 MIN READ

Anchor tags

Linking to the future

From the original Fervor library. Examples may use older package versions.

Sure thing! Let’s dive into the magic of the <a> tag in HTML. By the end of this, you’ll know how to make clickable links that lead anywhere you want—on the web or within your site.


📚 HTML <a> Tag Tutorial

The <a> tag stands for “anchor.” Think of it as a doorway to another place—whether that’s another page, a section of your own page, or even a file to download.

Basic Syntax

Here’s what the <a> tag looks like in its simplest form:

<a href="URL">Clickable Text</a>
  • href: Stands for “Hypertext REFerence.” This tells the browser where the link goes.
  • Clickable Text: This is the part users see and click.

Step-by-Step Guide

1. Linking to a Website

Want to link to Google? Easy!

<a href="https://www.google.com">Go to Google</a>
  • When users click “Go to Google,” they’ll be whisked away to Google.
  • Pro Tip: Always use full URLs (starting with https:// or http://) when linking to external websites.

2. Linking to Another Page on Your Site

If your website has multiple pages, you can link between them:

<a href="about.html">Learn About Us</a>
  • This assumes you have a file named about.html in the same folder as your current file.

3. Adding a Target Attribute

Want the link to open in a new tab? Use the target attribute:

<a href="https://www.example.com" target="_blank">Open in New Tab</a>
  • target="_blank": Opens the link in a new browser tab.
  • Heads up! Some browsers treat this as a security risk unless you add rel="noopener noreferrer":
<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">Open in New Tab</a>

4. Linking to a Section of the Same Page

Anchors can link to specific sections. First, give the target section an ID:

<h2 id="about">About Us</h2>

Now link to it:

<a href="#about">Go to About Section</a>
  • The # symbol tells the browser to jump to the element with the matching id.

5. Adding a Tooltip

Want to add some context? Use the title attribute:

<a href="https://www.wikipedia.org" title="The Free Encyclopedia">Visit Wikipedia</a>

When users hover over the link, the title pops up as a tooltip.


6. Linking to an Email Address

Want to let users email you with a single click? Use mailto:

<a href="mailto:yourname@example.com">Email Me</a>
  • Add a subject line and body:
<a href="mailto:yourname@example.com?subject=Hello&body=Hi!">Email Me</a>

7. Download a File

Use the download attribute to let users download a file instead of visiting it:

<a href="file.pdf" download>Download PDF</a>
  • The download attribute tells the browser to download the file instead of opening it.

Common Mistakes to Avoid

  1. Broken Links: Double-check your URLs. A missing .html or typo can break a link.

  2. Unclear Link Text: Avoid generic text like “Click Here.” Be descriptive!

  3. Forgetting Accessibility: Screen readers rely on meaningful link text. Instead of:

    <a href="page.html">Click Here</a>
    

    Use:

    <a href="page.html">Learn More About Our Services</a>
    

Examples to Play With

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Link Examples</title>
</head>
<body>
    <h1>Explore Links</h1>
    <p><a href="https://www.google.com" target="_blank" rel="noopener noreferrer">Visit Google</a></p>
    <p><a href="about.html">About Us</a></p>
    <p><a href="#contact">Go to Contact Section</a></p>
    <p><a href="mailto:support@example.com">Email Support</a></p>
    <p><a href="brochure.pdf" download>Download Brochure</a></p>

    <h2 id="contact">Contact Us</h2>
    <p>Here is the contact section you linked to above.</p>
</body>
</html>

You’re Ready!

That’s the lowdown on the <a> tag. Play around, experiment with different attributes, and watch your links come to life. Remember: <a> is your gateway to making the web connected. 🌐

Bonus Cool stuff

Let’s level up your <a> tag game with some creative, practical, and downright cool tricks! These will not only make your links functional but also visually appealing and interactive.


Cool Tricks with <a> Tags

You can style your links to look like buttons. This is great for call-to-action (CTA) elements like “Sign Up” or “Learn More.”

<a href="signup.html" class="button">Sign Up Now</a>

And some CSS:

.button {
    display: inline-block;
    background-color: #007bff;
    color: white;
    padding: 10px 20px;
    text-decoration: none;
    border-radius: 5px;
    font-size: 16px;
}

.button:hover {
    background-color: #0056b3;
}

Now your link looks and feels like a professional button!


2. Smooth Scrolling to Anchors

Instead of jumping abruptly to a section, use smooth scrolling for a seamless experience.

HTML:

<a href="#features">Explore Features</a>
<h2 id="features">Our Features</h2>

CSS for Smooth Scroll:

html {
    scroll-behavior: smooth;
}

When you click the link, the page will glide smoothly to the target section. 🛶


3. Tooltips with Custom Styling

Enhance the default tooltip with some CSS magic:

<a href="https://example.com" class="tooltip" title="Visit Example Website">Hover Over Me</a>

CSS:

.tooltip {
    position: relative;
}

.tooltip::after {
    content: attr(title);
    position: absolute;
    bottom: 150%;
    left: 50%;
    transform: translateX(-50%);
    background-color: #333;
    color: #fff;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 14px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.tooltip:hover::after {
    opacity: 1;
}

Now, when users hover over the link, they’ll see a custom tooltip. 🛠️


Add icons to your links using font libraries like Font Awesome.

<a href="https://twitter.com" target="_blank">
    <i class="fa fa-twitter"></i> Follow Us on Twitter
</a>

Don’t forget to include Font Awesome in your <head>:

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">

Now your links are stylish and instantly recognizable. ✨


Want an entire card or area to be clickable? Wrap the <a> around a block element.

<a href="product.html" class="card">
    <div>
        <h2>Awesome Product</h2>
        <p>Click here to learn more!</p>
    </div>
</a>

CSS:

.card {
    display: block;
    border: 1px solid #ccc;
    padding: 20px;
    text-decoration: none;
    color: inherit;
    transition: transform 0.2s;
}

.card:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

This transforms your link into an interactive clickable card. Perfect for products or articles! 🃏


Protect email addresses from spam bots by dynamically generating them with JavaScript.

<a href="#" id="email-link">Contact Us</a>

<script>
    const email = "support";
    const domain = "example.com";
    document.getElementById("email-link").setAttribute("href", `mailto:${email}@${domain}`);
</script>

When users click the link, it opens their email client, but bots won’t easily detect it.


7. Hover Effects with Animations

Bring your links to life with animated hover effects.

<a href="https://example.com" class="cool-link">Hover Over Me</a>

CSS:

.cool-link {
    color: #007bff;
    text-decoration: none;
    position: relative;
    display: inline-block;
}

.cool-link::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 2px;
    background: #007bff;
    bottom: 0;
    left: 0;
    transform: scaleX(0);
    transform-origin: right;
    transition: transform 0.3s ease-out;
}

.cool-link:hover::after {
    transform: scaleX(1);
    transform-origin: left;
}

Now your links get a snazzy underline animation when hovered. ✨


Use <a> tags to trigger JavaScript functions for added interactivity.

<a href="#" onclick="sayHello()">Click Me</a>

<script>
    function sayHello() {
        alert("Hello, World!");
    }
</script>

When clicked, the link pops up a friendly alert box. 🖱️


Make your site mobile-friendly with clickable phone numbers.

<a href="tel:+1234567890">Call Us: +1 234 567 890</a>

When clicked on a phone, it starts a call immediately. 📞


Help users quickly navigate back to the top of a page.

<a href="#top" class="back-to-top">Back to Top</a>

CSS:

.back-to-top {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: #007bff;
    color: white;
    padding: 10px 15px;
    text-decoration: none;
    border-radius: 50%;
    display: none;
}

.back-to-top:hover {
    background: #0056b3;
}

body.scrolled .back-to-top {
    display: block;
}

JavaScript:

window.addEventListener('scroll', function () {
    if (window.scrollY > 200) {
        document.body.classList.add('scrolled');
    } else {
        document.body.classList.remove('scrolled');
    }
});

Voilà—a functional back-to-top button appears after scrolling down.


The <a> tag might seem simple, but it’s a powerhouse when combined with CSS, JavaScript, and creative ideas. Start implementing these tricks to make your links engaging, user-friendly, and visually stunning.

Keep your curiosity going.Explore more HTML →
287 TUTORIALS · 22 TOPICSREADY