- HTML Tutorial
- HTML Introduction
- HTML Editors
- HTML Basic
- HTML Comments
- HTML Elements
- HTML Attributes
- HTML Id & Classes
- HTML Skeletal Tags
- HTML Heading Tags
- HTML Paragraph Tag
- HTML Line Break Tag
- HTML Pre Tag
- HTML Anchor Tag
- HTML Image Tag
- HTML Horizontal Line Tag
- HTML Inline & Block
- HTML Inline
- HTML Block
- HTML LInks
- HTML Images
- HTML Formatting
- HTML Head
- HTML Head
- HTML Title
- HTML Meta Elements
- HTML Favicon
- HTML Style
- HTML List
- HTML Lists
- HTML Unordered List
- HTML Ordered List
- HTML Description List
- HTML Table
- HTML Tables
- HTML Table Headers
- HTML Table Styling
- HTML Table Colgroup
- HTML Form
- HTML Forms
- HTML Form Elements
- HTML Form Attributes
- HTML Input Types
- HTML Input Attributes
- HTML Form Actions
- HTML Semantic
- HTML Semantics
- HTML Graphics & Media
- HTML Canvas
- HTML SVG
- HTML Video & Audio
- HTML Plug-ins
- iFrames in HTML
- HTML Miscellaneous Tags
- HTML Code Tag
- HTML Entities
- HTML Quotation
- HTML Global Attributes
- HTML Obsolete Tags
- HTML Emojis
- HTML Symbols
- HTML Events
- HTML Colors
HTML Anchor Tag
The anchor tag (<a>) is the backbone of the World Wide Web. Without it, we wouldn't have "links," and the internet would just be a collection of isolated documents. By using the anchor tag, you can connect your users to other websites, specific files, email addresses, or even different sections within the same page.
Usage:
To create a hyperlink, you wrap the <a> tag around the text or image you want to make clickable. By default, browsers will render these links in blue with an underline to signal to the user that the element is interactive.
<!-- A basic link to an external site -->
<a href="https://www.google.com">Search on Google</a>
<img> tag to create clickable image buttons, or even wrap it around a <div> to make an entire card clickable.
Href Attribute:
- The href (Hypertext Reference) attribute is the most important part of the anchor tag. It tells the browser exactly where the user should go when they click the link.
- Absolute URLs: These point to a full web address, including the protocol (http:// or https://). Use these when linking to external websites.
- Relative URLs: These point to a file within your own project. They are much better for internal navigation because they won't break if you change your domain name.
<!-- Absolute URL -->
<a href="https://github.com">View my GitHub</a>
<!-- Relative URL -->
<a href="/contact-us.html">Contact Our Team</a>
href="www.google.com", the browser will think you are looking for a folder named "www.google.com" inside your own website.
Text Content:
The text placed between the opening <a> and closing </a> tags is what the user actually sees on the screen. This is often referred to as "Anchor Text."
Target Attribute:
The target attribute tells the browser where to open the linked document. While there are several options, _blank is the one you will use most often.
- _self: The default. The link opens in the same tab you are currently using.
- _blank: Opens the link in a brand-new tab or window.
- _parent / _top: Used mainly when working with iframes to break out of a frame and open the link in the full window.
<!-- Opening a link in a new tab -->
<a href="https://www.wikipedia.org" target="_blank">Open Wikipedia in a New Tab</a>
target="_blank", always add rel="noopener" or rel="noreferrer" for security and performance reasons. This prevents the new page from potentially accessing your original page's window object.
Linking to Sections within the Same Page (Anchor Links):
You can create a "Jump Link" or "Table of Contents" by linking to the ID of another HTML element on the same page. This is incredibly useful for long-form articles or "Back to Top" buttons.
<!-- The Link -->
<a href="#pricing-table">View Our Pricing</a>
<!-- The Target -->
<section id="pricing-table">
<h2>Our Plans</h2>
<p>Choose the best plan for your needs.</p>
</section>
scroll-behavior: smooth; to your CSS for the html element.
Accessibility Considerations:
Accessibility ensures that everyone, including people using assistive technologies, can use your site. Links are a major part of this.
- Visual Cues: Don't rely on color alone to identify a link. Keeping the underline is a standard way to help colorblind users.
- Keyboard Navigation: Users should be able to "Tab" through your links. Ensure your CSS doesn't hide the
:focusoutline. - Title Attribute: You can add a
title="Extra info"attribute to provide a tooltip when a user hovers over the link, though this shouldn't contain critical information.
Styling with CSS:
CSS allows you to completely transform how links look. Most developers use "Pseudo-classes" to style links based on their state (unvisited, visited, hovered, or active).
/* Standard link state */
a {
color: #007bff;
text-decoration: none; /* Removes the default underline */
transition: color 0.3s ease;
}
/* When the user hovers over the link */
a:hover {
color: #0056b3;
text-decoration: underline;
}
/* When the link is actually being clicked */
a:active {
color: #ff0000;
}
:hover and :focus states together. This ensures that users navigating via keyboard get the same visual feedback as mouse users.
The anchor tag is a simple yet powerful tool. By mastering its attributes and following accessibility best practices, you create a seamless and professional navigation experience for your users.