The anchor tag creates a clickable link in HTML
A link in HTML is made with the anchor tag, written as <a>. The tag wraps around the text you want to be clickable, and an href attribute tells the browser where to send the reader when they click. Here is the basic structure:
<a href="https://example.com">Click here</a>
When a browser displays this code, the reader sees "Click here" as a clickable link. The href value — the web address in quotes — is where the browser navigates when someone clicks. The text between the opening and closing tags is what appears on the page. Without the anchor tag and href attribute, text is just text; with them, it becomes interactive.
Key Takeaways
- The anchor tag <a> with an href attribute creates a clickable link; the text between the tags is what the reader sees.
- Links to other websites need the full web address starting with http:// or https://.
- Links to pages on your own site use relative paths like href="about.html" or href="pages/contact.html".
- The target="_blank" attribute opens a link in a new tab instead of replacing the current page.
- Anchor tags can wrap around images, buttons, or other elements to make them clickable, not just text.
External links to other websites
When you link to a website you do not own, use the full web address in the href. This address must start with http:// or https:// so the browser knows it is a complete web address, not a file on your own server.
<a href="https://www.wikipedia.org">Visit Wikipedia</a>
Without the https:// part, the browser will treat www.wikipedia.org as a relative path — it will look for a folder called www.wikipedia.org on your own site and fail. Always include the protocol (http:// or https://) when linking outward. This is one of the most common mistakes when writing links for the first time.
Internal links within your own site
Links to other pages on your own website do not need the full web address. Instead, use a relative path — just the filename or folder structure relative to where you are now.
If your site has this structure:
- index.html (your home page)
- about.html (in the same folder)
- pages/contact.html (in a subfolder called pages)
From index.html, you would link to about.html like this:
<a href="about.html">About Us</a>
To link to the contact page in the pages subfolder, use:
<a href="pages/contact.html">Contact</a>
If you are on the contact page and need to link back to the home page, use href="../index.html" — the two dots mean "go up one folder level." This relative path approach keeps your links working even if you move your entire site to a different server, as long as the folder structure stays the same.
Opening links in a new tab
By default, clicking a link replaces the current page with the new one. If you want the link to open in a new tab instead, add the target="_blank" attribute:
<a href="https://example.com" target="_blank">Open in new tab</a>
This is useful when you link to external sites and want readers to stay on your page. Many sites use this for outbound links so visitors do not lose their place. However, use it sparingly — opening new tabs without warning can feel jarring to some users, so reserve it for cases where you genuinely want to preserve the reader's location.
Making images and buttons clickable
Anchor tags do not have to wrap around text. You can wrap them around images to make an image clickable:
<a href="gallery.html"><img src="thumbnail.jpg" alt="Gallery"></a>
You can also wrap an anchor tag around a button element to make the button navigate somewhere:
<a href="signup.html"><button>Sign Up</button></a>
In both cases, the entire image or button becomes clickable and behaves like a link. This is how you turn visual elements into navigation tools without writing extra JavaScript code.
Linking to a specific section on a page
You can link directly to a specific part of a page using an anchor ID. First, give an element an id attribute:
<h2 id="pricing">Our Pricing</h2>
Then link to that section using a hash symbol followed by the ID:
<a href="https://example.com/page.html#pricing">Jump to Pricing</a>
Or, if you are linking to a section on the same page:
<a href="#pricing">Jump to Pricing</a>
When someone clicks this link, the browser scrolls to the element with that ID. This technique is useful for long pages with multiple sections — it lets readers jump directly to the part they care about without scrolling through everything above it.
Frequently Asked Questions
What is the difference between href and src?
href is used in anchor tags to link to a page or resource. src is used in image and script tags to load a file into the page. Anchor tags navigate; image tags embed.
Do I need https:// or just http://?
Use https:// whenever possible — it is the secure version. Most websites now use HTTPS by default. If a site only supports HTTP, the browser will usually upgrade it automatically, but typing HTTPS first is the safer choice.
Can I link to a PDF or other file type?
Yes. Use the full path to the file in the href: <a href="documents/resume.pdf">Download Resume</a>. The browser will either open the file or prompt the user to download it, depending on the file type and browser settings.
What does it mean if a link shows as purple instead of blue?
Purple (or a different color) usually means you have already visited that link. Browsers track visited links and style them differently by default. You can change these colors with CSS if you want.
Can I link to an email address?
Yes, using a mailto: link: <a href="mailto:contact@example.com">Email Us</a>. Clicking this opens the user's default email program with that address filled in.