HTML href Attribute

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 3 Examples
Links & Navigation

What You’ll Learn

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.

01

Anchor Links

<a href="..."> hyperlinks.

02

Absolute URLs

https:// full addresses.

03

Relative Paths

/about, page.html.

04

Fragments

#section-id jumps.

05

mailto & tel

Email and phone links.

06

link.href

Update URL in JS.

Purpose of href Attribute

The 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.

💡
Prefer <button> over href="#" for actions

An 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.

📝 Syntax

Add href to an anchor element with a valid URL, path, or protocol link:

href.html
<!-- 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>

Syntax Rules

  • Primary use is on <a> elements to create hyperlinks users can click or activate with the keyboard.
  • Absolute URLs start with a scheme such as https:// and include the full address (e.g. https://www.example.com/page).
  • Relative paths resolve from the current page or site root (e.g. about.html, /html, ../images/logo.png).
  • Fragment links use #id and must match an id on the target element—not the deprecated name attribute.
  • Use mailto: and tel: for email and phone links; add download on anchors to suggest file downloads.
  • For external links opened in a new tab, pair target="_blank" with rel="noopener noreferrer" for security.

💎 Values

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.
href-values.html
<!-- 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>

⚡ Quick Reference

ConceptDetailsNotes
Primary element<a href="...">Clickable hyperlinks
Also on<link>, <area>, <base>Resources, maps, base URL
Absolute URLhttps://example.com/pageFull web address
Relative path/html, about.htmlFrom current or root
Fragment#section-idMatches target id
JS propertylink.href = "url"Or setAttribute("href", url)

Applicable Elements

ElementSupported?Notes
<a>YesPrimary use—clickable hyperlinks for users
<link>YesStylesheets, icons, preload, and other document resources
<area>YesClickable regions inside an HTML image map
<base>YesSets default base URL for relative links in the document
<button>NoUse buttons for actions; use <a href> for navigation
Other HTML elementsNohref is limited to link-related elements per HTML spec

Absolute vs Relative vs Fragment vs mailto: href Values

Value TypeExampleWhen to Use
Absolute URLhref="https://www.example.com"Link to an external site or full address; always includes scheme (https://)
Relative pathhref="/html" or href="about.html"Link within your site; paths resolve from root or current page
Fragmenthref="#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
Downloadhref="file.pdf" downloadSuggest downloading a file rather than opening it in the browser

Examples Gallery

External absolute URL, mixed link types (relative, mailto, fragment), and dynamic href updates with JavaScript.

👀 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".

Example — External Absolute URL

Link to another website with a full HTTPS address and open it safely in a new tab:

external-link.html
<a
  href="https://www.example.com"
  target="_blank"
  rel="noopener noreferrer">
  Visit Example.com
</a>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Change where a link points at runtime using the link.href property:

dynamic-href.html
<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>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Use descriptive link text — Write link labels that explain the destination (e.g. “Read our HTML tutorials”) instead of vague phrases like “click here” or “read more” with no context.
  • Make link purpose clear out of context — Screen reader users often navigate by links alone. Each link text should make sense without surrounding paragraph text.
  • Distinguish links from buttons — Use <a href> for navigation and <button> for actions. Do not use href="#" as a fake button.
  • Indicate when links open externally — For target="_blank", consider visible text or an accessible label so users know a new tab opens.
  • Ensure fragment targets exist — Verify href="#id" matches a real id on the page so keyboard and screen reader users reach the intended section.
  • Do not rely on URL alone — Visible link text should describe content, not expose raw URLs unless that is intentional (e.g. citation links).

🧠 How href Works

1

Author sets href value

URL, path, or fragment.

Markup
2

Browser resolves URL

Absolute, relative, or hash.

Parse
3

User activates link

Click, Enter, or tap.

Action
=

Navigation to destination

Page, section, email, or call.

Browser Support

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.

HTML · Fully supported

Universal hyperlink support

All major browsers honor href on anchors and expose the link.href property for JavaScript updates.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
href attribute on <a>, <link>, <area> 99% supported

Bottom line: Use href confidently for hyperlinks; add rel="noopener noreferrer" on external target="_blank" links.

💡 Best Practices

✅ Do

  • Use descriptive link text that explains the destination
  • Add rel="noopener noreferrer" with target="_blank"
  • Use id on fragment targets (href="#section-id")
  • Prefer relative paths for same-site navigation
  • Update URLs with link.href = "url" in JavaScript

❌ Don’t

  • Write vague link text like “click here”
  • Use href="#" for non-navigation actions
  • Target deprecated name attributes for fragments
  • Open external tabs without rel="noopener noreferrer"
  • Use <a> without href when you need a real link

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about href

Bookmark these before adding your next hyperlink.

5
Core concepts
🌐 02

Absolute vs relative

Full URL or path.

Values
📍 03

#id fragments

Jump to section.

Navigate
⚙️ 04

link.href = url

Dynamic updates.

Script
05

Descriptive text

Not “click here”.

A11y

❓ Frequently Asked Questions

It specifies the URL or destination for a hyperlink. On <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.
An absolute URL includes the full address with scheme (e.g. 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.
Use 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.
Assign the property: link.href = "https://example.com". You can also call link.setAttribute("href", url). Both update the link destination before the user clicks.
Yes. All modern browsers support href on <a>, <link>, <area>, and <base>. Use rel="noopener noreferrer" with target="_blank" on external links as a security best practice.

Build hyperlinks the right way

Practice the href attribute with external, mixed link type, and JavaScript examples in the Try It editor.

Try external link example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful