HTML Links
Hyperlink elements establish clickable connections between documents, facilitating web navigation through anchor tags configured with href attributes and supplementary properties.
Complete working example
Links and in-page targets
Try a working page anchor and learn how to label a link that opens a new tab.
Read the complete HTML document
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My code example</title>
<style>
:root {
font-family: system-ui, sans-serif;
color: #18223b;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
padding: 24px;
background: #edf1ff;
display: grid;
place-items: center;
}
main {
min-width: 0;
width: min(100%, 600px);
background: white;
border-radius: 20px;
padding: clamp(20px, 5vw, 32px);
}
h1 {
font-size: clamp(24px, 5vw, 32px);
line-height: 1.2;
margin: 0 0 16px;
}
h2 {
font-size: 20px;
margin: 24px 0 12px;
}
p,
li {
line-height: 1.65;
}
a {
color: #2545a8;
text-underline-offset: 3px;
}
button,
input,
select,
textarea {
font: inherit;
}
button {
min-height: 44px;
border: 1px solid #9eaed0;
border-radius: 10px;
padding: 10px 14px;
background: #eff3ff;
color: #18223b;
cursor: pointer;
}
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
outline: 3px solid #3658bf;
outline-offset: 3px;
}
pre {
max-width: 100%;
overflow: auto;
white-space: pre-wrap;
overflow-wrap: anywhere;
background: #eef2fa;
padding: 16px;
border-radius: 10px;
}
code {
font-family: ui-monospace, monospace;
}
#packing {
margin-top: 32px;
padding-top: 16px;
border-top: 1px solid #dbe2f1;
}
</style>
</head>
<body>
<main>
<h1>Links with a clear destination</h1>
<nav aria-label="On this page"><a href="#packing">Jump to the packing list</a></nav>
<p>A descriptive link tells the reader what they will find.</p>
<p
><a href="https://html.spec.whatwg.org/" target="_blank" rel="noopener noreferrer"
>Read the HTML Standard (opens a new tab)</a
></p
>
<section id="packing" tabindex="-1"
><h2>Packing list</h2
><ul
><li>Notebook</li
><li>Water bottle</li></ul
></section
>
</main>
<script>
</script>
</body>
</html>How it works
The #packing link matches a unique id in this document. The external reference is a plain destination link, with an announced new tab and noopener noreferrer. The isolated site preview demonstrates external links without opening destinations; a copied document follows them normally.
Troubleshooting and limits
Each target id must be unique and match the fragment exactly. Replace the external destination only with a URL you trust. Relative page links need the corresponding local file. Links navigate; buttons perform actions.
Quick Answer
How do you create links in HTML?
Create links using the <a> (anchor) tag with href attribute: <a href="URL">Link Text</a>. Add target="_blank" to open in new tabs, use relative paths for internal pages, and mailto: for email links. Links support images, buttons, and styled text for navigation and user interaction.
Hyperlinks enable seamless movement across pages, document
sections, and external resources. Constructed via
<a> (anchor) elements, these navigational
components form the connective tissue enabling intuitive website
exploration.
Defining HTML Hyperlinks
Hyperlink technology transforms the web into an interconnected information network. These clickable references facilitate page-to-page movement, intra-document jumping, and external resource access.
Core Hyperlink Capabilities
- Establish cross-document connectivity pathways
- Facilitate same-page section navigation
- Support diverse targets including files, email, telephony
- Accommodate varied visual states and styling
- Constitute fundamental usability infrastructure
Fundamental Hyperlink Construction
Elementary links employ <a> elements paired
with href attributes designating target
destinations.
Standard Hyperlink Patterns
<h1>Explore HTML Free Codes</h1
>
<!-- Remote destination -->
<p
>Access <a href="https://htmlfreecodes.com">HTML Free Codes</a
> for additional lessons.</p
>
<!-- Alternative external reference -->
<p
>Expand your skills at <a href="https://htmlfreecodes.com/tutorials"
>our educational hub</a
>.</p
>
<!-- Custom anchor text -->
<p
><a href="https://htmlfreecodes.com/contact">Reach our team</a
> with inquiries.</p
>
<!-- Embedded multiple anchors -->
<p
>Study <a href="https://htmlfreecodes.com/html"
>HTML</a
>,
<a href="https://htmlfreecodes.com/css"
>CSS</a
>, plus
<a href="https://htmlfreecodes.com/javascript"
>JavaScript</a
> courses.</p
>
Result:
Explore HTML Free Codes
Access HTML Free Codes for additional lessons.
Learn more about web development at our tutorials page.
Contact us if you have any questions.
We offer HTML, CSS, and JavaScript tutorials.
Link Attributes
HTML links support several attributes that control their behavior and appearance.
Link Attribute Examples
<h2>Different Link Attributes</h2
>
<!-- Link that opens in new tab -->
<p
><a
href="https://htmlfreecodes.com"
target="_blank"
>Open HTML Free Codes in new tab</a
></p
>
<!-- Link with title attribute (tooltip) -->
<p
><a
href="https://htmlfreecodes.com"
title="Best place to learn HTML"
>HTML Free Codes</a
> (hover to see tooltip)</p
>
<!-- Link with custom styling -->
<p
><a
href="https://htmlfreecodes.com"
style="color: #e74c3c; text-decoration: none; font-weight: bold;"
>Styled link</a
></p
>
<!-- Link with rel attribute for security -->
<p
><a
href="https://example.com"
target="_blank"
rel="noopener noreferrer"
>Secure external link</a
></p
>
<!-- Download link -->
<p
><a
href="/playground.html"
download
>Download our HTML tutorial (PDF)</a
></p
>
<!-- Link with multiple attributes -->
<p
><a
href="https://htmlfreecodes.com/advanced"
target="_blank"
title="Advanced HTML tutorials"
rel="noopener"
>Advanced Tutorials</a
></p
>
Result:
Different Link Attributes
Open HTML Free Codes in new tab
HTML Free Codes (hover to see tooltip)
Styled link
Download our HTML tutorial (PDF)
Advanced Tutorials
Internal Links and Anchors
Internal links allow you to jump to different sections within the same page using anchor points.
Internal Link Examples
<h1>Table of Contents</h1
>
<!-- Navigation menu with internal links -->
<nav
>
<ul
>
<li><a href="#introduction">Introduction</a></li
>
<li><a href="#getting-started">Getting Started</a></li
>
<li><a href="#examples">Examples</a></li
>
<li><a href="#conclusion">Conclusion</a></li
>
</ul
>
</nav
>
<!-- Section 1 -->
<section
id="introduction"
>
<h2>Introduction</h2>
<p>Welcome to HTML Free Codes! This section introduces our tutorial platform.</p>
</section
>
<!-- Section 2 -->
<section
id="getting-started"
>
<h2>Getting Started</h2>
<p>Here's how to begin your HTML learning journey with htmlfreecodes.com.</p>
</section
>
<!-- Section 3 -->
<section id="examples"
>
<h2>Examples</h2>
<p>Check out these practical examples from HTML Free Codes.</p>
</section
>
<!-- Section 4 -->
<section id="conclusion"
>
<h2>Conclusion</h2>
<p>Thank you for learning with HTML Free Codes!</p>
<p
><a href="#introduction">Back to top</a></p
>
</section
>
Result:
Table of Contents
Introduction
Welcome to HTML Free Codes! This section introduces our tutorial platform.
Getting Started
Here's how to begin your HTML learning journey with htmlfreecodes.com.
Examples
Check out these practical examples from HTML Free Codes.
Conclusion
Thank you for learning with HTML Free Codes!
Email and Phone Links
HTML links can also trigger email clients or phone calls using special URL schemes.
Email and Phone Link Examples
<h2>Contact HTML Free Codes</h2
>
<!-- Basic email link -->
<p
>Email us: <a href="mailto:support@htmlfreecodes.com"
>support@htmlfreecodes.com</a
></p
>
<!-- Email with subject line -->
<p
><a href="mailto:support@htmlfreecodes.com?subject=HTML Tutorial Question"
>Email us about HTML tutorials</a
></p
>
<!-- Email with subject and body -->
<p
><a
href="mailto:support@htmlfreecodes.com?subject=Feedback&body=Hello HTML Free Codes team,%0D%0A%0D%0AI wanted to share my feedback..."
>Send feedback</a
></p
>
<!-- Email with CC and BCC -->
<p
><a
href="mailto:support@htmlfreecodes.com?cc=info@htmlfreecodes.com&subject=Important Message"
>Email with CC</a
></p
>
<!-- Phone number link -->
<p
>Call us: <a href="tel:+1234567890">(123) 456-7890</a></p
>
<!-- SMS link -->
<p
>Text us: <a href="sms:+1234567890?body=Hello HTML Free Codes"
>Send SMS</a
></p
>
<!-- WhatsApp link -->
<p
>WhatsApp: <a
href="https://wa.me/1234567890?text=Hello%20HTML%20Free%20Codes"
>Chat on WhatsApp</a
></p
>
Result:
Contact HTML Free Codes
Email us:
Call us:
Text us:
WhatsApp:
Link Styling and States
Links have different visual states that can be styled with CSS to improve user experience.
Link State Examples
<style>
/* Normal link state */
.styled-link {
color: #3498db;
text-decoration: none;
font-weight: bold;
padding: 5px 10px;
border-radius: 3px;
transition: all 0.3s ease;
}
/* Visited link state */
.styled-link:visited {
color: #9b59b6;
}
/* Hover state */
.styled-link:hover {
color: white;
background-color: #3498db;
text-decoration: underline;
}
/* Button-style link */
.button-link {
display: inline-block;
background-color: #e74c3c;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
}
.button-link:hover {
background-color: #c0392b;
}
</style>
<h2>Different Link Styles</h2>
<p>This is a <a href="https://htmlfreecodes.com" class="styled-link">styled link</a> with hover effects.</p>
<p><a href="https://htmlfreecodes.com/start" class="button-link">Start Learning</a></p>
<p>Visit our <a href="https://htmlfreecodes.com/blog">blog</a> for the latest web development tips.</p>
Result:
Different Link Styles
This is a styled link with hover effects.
Start Learning
Visit our blog for the latest web development tips.
Complete Links Example
Here's a comprehensive example demonstrating various types of HTML links working together:
Complete Links Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Free Codes - Links Tutorial</title>
</head>
<body>
<header>
<h1>Welcome to HTML Free Codes</h1>
<nav>
<a href="#about">About</a>
<a href="#tutorials">Tutorials</a>
<a href="#contact">Contact</a>
</nav>
</header>
<main>
<section id="about">
<h2>About Us</h2>
<p>Learn web development at <a href="https://htmlfreecodes.com" target="_blank">htmlfreecodes.com</a>.</p>
</section>
<section id="tutorials">
<h2>Our Tutorials</h2>
<ul>
<li><a href="https://htmlfreecodes.com/html">HTML Tutorial</a></li>
<li><a href="https://htmlfreecodes.com/css">CSS Tutorial</a></li>
<li><a href="https://htmlfreecodes.com/javascript">JavaScript Tutorial</a></li>
</ul>
</section>
<section id="contact">
<h2>Contact Us</h2>
<p>Email: <a href="mailto:support@htmlfreecodes.com">support@htmlfreecodes.com</a></p>
<p>Phone: <a href="tel:+1234567890">(123) 456-7890</a></p>
</section>
</main>
<footer>
<p><a href="#about">Back to top</a></p>
</footer>
</body>
</html>
🎯 Link Best Practices
- Use descriptive text: Avoid "click here" or "read more"
-
Open external links in new tabs: Use
target="_blank" -
Add security attributes: Use
rel="noopener noreferrer"for external links - Provide clear hover states: Help users understand what's clickable
- Use meaningful URLs: Make them readable and SEO-friendly
- Test all links: Ensure they work correctly
- Consider accessibility: Use proper contrast and focus indicators
Test Your Knowledge
5 practice questions are available. Choose Start practice to try them.
Sample contact and navigation controls above are demonstrations. They do not contact anyone or open a destination.
A smaller starting example and practical notes
The anchor element creates navigation when it has an href. Write link text that explains the destination. A visitor should be able to understand the link without relying on an adjacent icon or a vague label such as click here.
<a href="/examples/">Browse working examples</a>
<a href="#next-step">Jump to the next step</a>
<section id="next-step">
<h2>Your next step</h2>
<p>Choose one small example and change its text.</p>
</section>Know which path you are using
A path starting with / is relative to the site's root. A path such as next.html is relative to the current document location. A fragment starting with # targets an element with the matching id in the same document. Exact filename spelling and capitalization can matter on the web server even if they appeared forgiving on your Mac.
Use navigation for destinations
Use a button when the visitor performs an action, such as pausing an animation or resetting a counter. Do not use href="#" as a stand-in for an unfinished action: it changes navigation without providing a useful destination. Let links retain a visible focus indicator and distinguish them from surrounding body text.
Verify the destination
Follow the link in a browser and confirm it loads the intended content. Check fragment links against real ids. For a removed URL with an equivalent permanent replacement, a server redirect is preferable to an unrelated homepage handoff.