Optimize your website’s structure and search rankings by mastering HTML hyperlinks. Learn best practices for the a tag, internal/external linking, and anchor text.
Hyperlinks, often referred to as "links," are the fundamental building blocks of the World Wide Web, enabling navigation between web pages and resources. They allow users to jump from one document to another with a simple click or tap. Understanding how to create and use them effectively is essential for any web developer.
The Element: Creating Links
The anchor element, <a>, is used to define hyperlinks. It requires at least one attribute,
href, which specifies the destination URL or resource.
-
href(Hypertext Reference): This is the most important attribute. It specifies the address (URL) of the page the link points to.-
Absolute URLs: Link to resources on other websites or specific locations on the internet (e.g.,
https://www.example.com/about). -
Relative URLs: Link to resources within the same website, relative to the current page's location (e.g.,
about.html,/products/item.html,../images/logo.png). -
Fragment Identifiers: Link to a specific section within the current page or another page (e.g.,
#section-idorpage.html#section-id). This requires an element with a matchingidattribute on the target page. -
Other Protocols:
hrefcan also specify other protocols likemailto:(to open an email client),tel:(to initiate a phone call), orjavascript:(to execute JavaScript, though this is less common and often discouraged for direct linking).
-
-
targetattribute: This attribute specifies where to open the linked document.-
_self(default): Opens the linked document in the same frame or window as the link. -
_blank: Opens the linked document in a new, unnamed window or tab. This is often used for external links to avoid navigating the user away from the original site. -
_parent: Opens the linked document in the parent frame of the current one. -
_top: Opens the linked document in the full body of the window, canceling out all other frames.
-
-
rel(Relationship) attribute: This attribute defines the relationship between the current document and the linked document. It's particularly important for SEO and security, especially withtarget="_blank".-
nofollow: Tells search engines not to pass "link juice" or credit to the linked page. Often used for user-generated content or paid links. -
noopener: Prevents the new page from having access to the window object of the originating page, improving security when usingtarget="_blank". -
noreferrer: Prevents the browser from sending the referring URL to the linked page, enhancing privacy.
-
-
downloadattribute: If present, this attribute suggests to the browser that the linked resource should be downloaded rather than displayed, optionally with a specified filename.
Example:
<!-- Link to an external page, opening in a new tab -->
<a href="https://www.wikipedia.org/" target="_blank" rel="noopener noreferrer">Visit Wikipedia</a>
<!-- Link to a page within the same site -->
<a href="about.html">About Us</a>
<!-- Link to a specific section on the current page -->
<a href="#contact">Jump to Contact Section</a>
<!-- Link to download a file -->
<a href="/documents/report.pdf" download="AnnualReport.pdf">Download Report (PDF)</a>
<!-- Link to open an email client -->
<a href="mailto:info@example.com">Email Us</a>
Link Text: The Visible Part of the Link
The text between the opening <a> tag and the closing </a> tag is the visible,
clickable part of the hyperlink. This text is crucial for user experience and SEO.
-
Be Descriptive: Link text should clearly indicate what the user can expect if they click the link. Phrases like "Click Here" are generally discouraged because they offer no context without surrounding text.
-
Be Concise: While descriptive, link text should also be brief and to the point.
-
Context Matters: The surrounding text provides context for the link. Ensure the link text makes sense both on its own and within the paragraph or list it resides.
-
Search Engine Optimization (SEO): Search engines use link text (anchor text) to understand the topic of the linked page. Incorporating relevant keywords naturally within descriptive anchor text can help improve search rankings.
Linking to Different Resource Types
-
Web Pages: The most common use, linking to other HTML documents.
-
Images: While images are often displayed directly using
<img>, you can also link to an image file so that clicking it opens the image in a new tab or initiates a download.<a href="images/large-photo.jpg" target="_blank">View Large Photo</a> -
Documents: PDFs, Word documents, etc., can be linked, allowing users to download or view them.
-
Email Addresses: Using the
mailto:protocol opens the user's default email client with a pre-filled recipient address. -
Phone Numbers: Using the
tel:protocol allows mobile users to initiate a call directly from the link.<a href="tel:+1234567890">Call Us: +1 (234) 567-890</a>
Styling Links with CSS
Links have several pseudo-classes that allow for distinct styling based on their state:
-
:link: An unvisited link. -
:visited: A link the user has already visited. -
:hover: A link when the user's mouse pointer is over it. -
:active: A link at the moment it is clicked. -
:focus: A link that currently has keyboard focus.
It's common practice to define these states in a specific order (e.g., LVHA: :link,
:visited, :hover, :active) to avoid unintended style overrides.
Example CSS:
a {
color: #0066cc; /* Default link color */
text-decoration: none; /* Remove underline by default */
}
a:visited {
color: #663399; /* Color for visited links */
}
a:hover,
a:focus {
color: #ff6600; /* Color on hover/focus */
text-decoration: underline; /* Add underline on hover/focus for clarity */
}
a:active {
color: #cc0000; /* Color when link is being clicked */
}
Accessibility Considerations for Links
-
Sufficient Contrast: Ensure that link colors have enough contrast against the background, both in their default and hover/active states, to be easily discernible.
-
Clear Visual Cues: Provide clear visual indicators (like color change and/or underline) for interactive states (
:hover,:focus,:active). -
Avoid Relying Solely on Color: Don't use color alone to distinguish links from regular text, especially for
:visitedstates, as users with color blindness may not perceive the difference. Underlines or other text decorations are helpful. -
Descriptive Anchor Text: As mentioned, descriptive anchor text is vital for screen reader users to understand where a link will take them.
-
aria-label: For icon-only links or links where the visible text isn't descriptive enough,aria-labelcan provide an accessible name.
By mastering the creation and styling of hyperlinks, you empower users to navigate your website efficiently and provide them with a seamless experience across the vast landscape of the internet.
Related Articles
Our most attended masterclasses
Comments
Our most attended masterclasses