HTML Links
Links are what make the web a web. A link (also called a hyperlink) lets the reader jump to another page, another site, a file, an email address, or a different spot on the same page.
The anchor element
Links are created with the <a> (anchor) element. The href attribute holds the destination, and the content between the tags is what the user clicks.
<a href="https://example.com">Visit Example</a>The clickable content can be text, an image, or almost any other inline content. By default, browsers show unvisited links underlined in blue and visited links in purple.
<p>Read the <a href="https://developer.mozilla.org/">MDN Web Docs</a> to learn more.</p>
<p>Or jump to the <a href="#bottom">bottom of this example</a>.</p>
<div style="height: 400px; background: linear-gradient(#fff, #e8f0ff);"></div>
<p id="bottom">You made it to the bottom! <a href="#top">Back to top</a></p>Note: Playgrounds run in a sandboxed frame, so links to external sites may not open from inside the example. Links to
#idson the same page always work.
Useful link attributes
| Attribute | Example | Purpose |
|---|---|---|
href |
href="about.html" |
Where the link goes |
target |
target="_blank" |
Where to open it (_self = same tab, the default; _blank = new tab) |
title |
title="Opens our help page" |
Extra info shown as a tooltip |
rel |
rel="noopener noreferrer" |
Relationship to the linked page; important with _blank |
download |
download="notes.txt" |
Ask the browser to download instead of navigate |
When you open a link in a new tab with target="_blank", it is good practice to add rel="noopener" so the new page cannot control the original one. Modern browsers do this automatically, but writing it explicitly is still common.
Absolute vs relative URLs
- An absolute URL includes the full address:
https://www.example.com/blog/post.html. Use it for other websites. - A relative URL is relative to the current page:
contact.html,images/logo.png,../index.html. Use it for pages within your own site, so links keep working if the domain changes.
You will learn more in the File Paths chapter.
Special kinds of links
href value |
What it does |
|---|---|
#section-id |
Scrolls to the element with that id on the same page |
mailto:hi@example.com |
Opens the user's email program |
tel:+15551234567 |
Starts a phone call on phones |
page.html#faq |
Opens another page and scrolls to #faq |
javascript:... |
Runs script — avoid; use a <button> instead |
Same-page links are perfect for a table of contents:
<style>
body { font-family: sans-serif; }
section { min-height: 220px; padding: 10px; margin-bottom: 10px; border-left: 4px solid #e34c26; }
</style>
<nav>
<strong>Contents:</strong>
<a href="#cats">Cats</a> |
<a href="#dogs">Dogs</a> |
<a href="#fish">Fish</a>
</nav>
<section id="cats"><h2>🐱 Cats</h2><p>Independent and curious.</p></section>
<section id="dogs"><h2>🐶 Dogs</h2><p>Loyal and energetic.</p></section>
<section id="fish"><h2>🐟 Fish</h2><p>Calm and colorful.</p> <a href="#cats">↑ Back to the top</a></section>Images as links
Wrap an image (or any element) in <a> to make it clickable. Always give the image good alt text, because for screen reader users the alt text is the link text.
<a href="#info">
<svg width="80" height="80" role="img" aria-label="Information">
<circle cx="40" cy="40" r="36" fill="#2a7de1"/>
<text x="40" y="54" font-size="40" text-anchor="middle" fill="white" font-family="serif">i</text>
</svg>
</a>
<p id="info">Clicking the blue circle brings you here.</p>Styling link states
CSS pseudo-classes let you style each state of a link:
| Selector | When it applies |
|---|---|
a:link |
Unvisited link |
a:visited |
A link the user has already visited |
a:hover |
The mouse is over the link |
a:focus |
The link is selected with the keyboard |
a:active |
The moment it is being clicked |
<style>
a.button {
display: inline-block;
padding: 10px 18px;
background: #e34c26;
color: white;
text-decoration: none;
border-radius: 6px;
font-family: sans-serif;
}
a.button:hover { background: #b8391a; }
a.button:focus { outline: 3px solid #ffbf47; }
a.button:active { transform: translateY(2px); }
</style>
<a class="button" href="#">I am a link styled as a button</a>Tip: Write link text that makes sense on its own. "Download the 2025 report" is far more useful than "click here", especially for screen reader users who often skim a list of links.
Test yourself
Which attribute specifies the destination of a link?
srclinkhrefurl
What does target="_blank" do?
- Opens the link in a new tab or window
- Removes the underline
- Downloads the file
- Opens the link in the same tab
How do you link to an element with id="faq" on the same page?
<a href="faq"><a href="#faq"><a href=".faq"><a id="faq">