👀 Live Preview
An anchor with an absolute URL pointing to Example.com:
Visit Example.com (opens in new tab)
href="https://www.example.com" is an absolute URL. rel="noopener noreferrer" protects against tab-nabbing when using target="_blank".

The href attribute specifies where a hyperlink points. Its primary use is on <a href="..."> anchor elements to navigate to other pages, sections, email addresses, or phone numbers. It also appears on <link> (stylesheets, icons), <area> (image maps), and <base> (document base URL). Values can be absolute URLs, relative paths, fragments, mailto:, or tel: links.
<a href="..."> hyperlinks.
https:// full addresses.
/about, page.html.
#section-id jumps.
Email and phone links.
Update URL in JS.
href AttributeThe href attribute (short for hypertext reference) tells the browser where to go when a user activates a link. On <a> elements, it creates clickable hyperlinks to web pages, files, email clients, phone dialers, or in-page sections. Without a meaningful href, an anchor is not a navigation link.
Beyond anchors, href on <link> points to external resources like CSS stylesheets and favicons. On <area>, it defines clickable regions in an image map. On <base>, it sets the default base URL for all relative links in the document. Choose the right value type—absolute, relative, fragment, mailto:, or tel:—based on where you want users to go.
<button> over href="#" for actionsAn empty href="#" is a placeholder that scrolls to the top of the page. For UI actions that do not navigate (open a modal, submit a form, toggle a panel), use a <button type="button"> instead of a link with no real destination.
Add href to an anchor element with a valid URL, path, or protocol link:
<!-- External page: absolute URL -->
<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">
Visit Example.com
</a>
<!-- Same site: relative path -->
<a href="/html">HTML tutorials</a>
<!-- In-page jump: fragment with matching id -->
<a href="#details">Jump to details</a>
<section id="details">Details content</section>
<!-- Email and phone -->
<a href="mailto:hello@example.com">Email us</a>
<a href="tel:+15551234567">Call us</a><a> elements to create hyperlinks users can click or activate with the keyboard.https:// and include the full address (e.g. https://www.example.com/page).about.html, /html, ../images/logo.png).#id and must match an id on the target element—not the deprecated name attribute.mailto: and tel: for email and phone links; add download on anchors to suggest file downloads.target="_blank" with rel="noopener noreferrer" for security.The href attribute accepts a URL string. Common value types include:
href="https://www.example.com" — Absolute URL to an external or full web address.href="/html" or href="page.html" — Relative path within your site.href="#section-id" — Fragment link to an element with id="section-id" on the same or target page.href="mailto:hello@example.com" — Opens the user’s email client with the address filled in.href="tel:+15551234567" — Starts a phone call on devices that support tel: links.href="report.pdf" download — Suggests downloading the linked file instead of navigating to it.<!-- Stylesheet on link element -->
<link rel="stylesheet" href="/styles/main.css">
<!-- Document base URL -->
<base href="https://www.example.com/docs/">
<!-- JavaScript: change link destination -->
<script>
var link = document.getElementById("dynamicLink");
link.href = "https://www.example.com/new-page";
link.setAttribute("href", "https://www.example.com/alt-page");
</script>| Concept | Details | Notes |
|---|---|---|
| Primary element | <a href="..."> | Clickable hyperlinks |
| Also on | <link>, <area>, <base> | Resources, maps, base URL |
| Absolute URL | https://example.com/page | Full web address |
| Relative path | /html, about.html | From current or root |
| Fragment | #section-id | Matches target id |
| JS property | link.href = "url" | Or setAttribute("href", url) |
| Element | Supported? | Notes |
|---|---|---|
<a> | Yes | Primary use—clickable hyperlinks for users |
<link> | Yes | Stylesheets, icons, preload, and other document resources |
<area> | Yes | Clickable regions inside an HTML image map |
<base> | Yes | Sets default base URL for relative links in the document |
<button> | No | Use buttons for actions; use <a href> for navigation |
| Other HTML elements | No | href is limited to link-related elements per HTML spec |
mailto: href Values| Value Type | Example | When to Use |
|---|---|---|
| Absolute URL | href="https://www.example.com" | Link to an external site or full address; always includes scheme (https://) |
| Relative path | href="/html" or href="about.html" | Link within your site; paths resolve from root or current page |
| Fragment | href="#details" | Jump to id="details" on same page; combine with page URL for cross-page anchors |
mailto: | href="mailto:hello@example.com" | Open email client; optional subject with ?subject=Hello |
tel: | href="tel:+15551234567" | Start phone call on mobile or supported desktop apps |
| Download | href="file.pdf" download | Suggest downloading a file rather than opening it in the browser |
External absolute URL, mixed link types (relative, mailto, fragment), and dynamic href updates with JavaScript.
An anchor with an absolute URL pointing to Example.com:
Visit Example.com (opens in new tab)
href="https://www.example.com" is an absolute URL. rel="noopener noreferrer" protects against tab-nabbing when using target="_blank".
Link to another website with a full HTTPS address and open it safely in a new tab:
<a
href="https://www.example.com"
target="_blank"
rel="noopener noreferrer">
Visit Example.com
</a>The browser resolves https://www.example.com as a complete address and navigates there when the user clicks. target="_blank" opens a new tab; rel="noopener noreferrer" prevents the new page from accessing window.opener and hides referrer data for privacy.
Mix common href value types on one page—relative path, mailto:, and in-page fragment:
<nav>
<a href="/html">HTML tutorials</a>
<a href="mailto:hello@example.com">Email us</a>
<a href="#details">Jump to details</a>
</nav>
<section id="details">
<h2>Details</h2>
<p>Fragment links target this section via its id.</p>
</section>Relative paths like /html resolve from the site root. mailto: opens the user’s email client. Fragment links (#details) scroll to the matching id on the page—use id, not the deprecated name attribute.
Change where a link points at runtime using the link.href property:
<a id="dynamicLink" href="https://www.example.com">Go to site</a>
<button type="button" id="changeLink">Change link URL</button>
<script>
document.getElementById("changeLink").addEventListener("click", function () {
var link = document.getElementById("dynamicLink");
link.href = "https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/href";
});
</script>Assigning a string to link.href updates the anchor’s destination immediately. The next click follows the new URL. You can also use link.setAttribute("href", url). Use addEventListener rather than inline handlers for cleaner separation of markup and behavior.
<a href> for navigation and <button> for actions. Do not use href="#" as a fake button.target="_blank", consider visible text or an accessible label so users know a new tab opens.href="#id" matches a real id on the page so keyboard and screen reader users reach the intended section.URL, path, or fragment.
Absolute, relative, or hash.
Click, Enter, or tap.
Page, section, email, or call.
The href attribute is fully supported in all modern browsers on <a>, <link>, <area>, and <base> elements. Absolute URLs, relative paths, fragments, mailto:, and tel: links all work consistently across Chrome, Firefox, Safari, and Edge.
All major browsers honor href on anchors and expose the link.href property for JavaScript updates.
<a>, <link>, <area> 99% supportedBottom line: Use href confidently for hyperlinks; add rel="noopener noreferrer" on external target="_blank" links.
rel="noopener noreferrer" with target="_blank"id on fragment targets (href="#section-id")link.href = "url" in JavaScripthref="#" for non-navigation actionsname attributes for fragmentsrel="noopener noreferrer"<a> without href when you need a real linkThe href attribute is the foundation of web navigation. On <a> elements it creates hyperlinks to pages, sections, email, and phone numbers. It also powers resource links on <link>, clickable regions on <area>, and the document base on <base>.
Choose the right value type—absolute URL, relative path, fragment, mailto:, or tel:—for each use case. Write accessible, descriptive link text, secure external links with rel="noopener noreferrer", and update destinations dynamically with link.href when your UI requires it.
hrefBookmark these before adding your next hyperlink.
<a href> hyperlinks.
ScopeFull URL or path.
ValuesJump to section.
NavigateDynamic updates.
ScriptNot “click here”.
A11y<a> elements, it creates clickable links to web pages, files, email addresses, phone numbers, or in-page sections. It also appears on <link>, <area>, and <base> for resources and document base URLs.<a> (anchors), <link> (stylesheets, icons), <area> (image map regions), and <base> (document base URL). The most common use is on anchor elements for user-facing hyperlinks.https://www.example.com/page). A relative path points from the current page or site root (e.g. /html, about.html). Both are valid; relative paths keep same-site links portable.href="#section-id" to jump to an element with a matching id. For example, <a href="#details"> scrolls to <section id="details">. Use id on the target—not the deprecated name attribute.link.href = "https://example.com". You can also call link.setAttribute("href", url). Both update the link destination before the user clicks.href on <a>, <link>, <area>, and <base>. Use rel="noopener noreferrer" with target="_blank" on external links as a security best practice.Practice the href attribute with external, mixed link type, and JavaScript examples in the Try It editor.
4 people found this page helpful