Frontend interview practice question

What is the href attribute used for?

HighEasyHtml

Interview quick answer

href stands for hypertext reference: it is the attribute that tells the browser where a link or linked resource points, whether that is an absolute or relative URL, a #fragment on the same page, a mailto: or tel: address, or a file. Without a valid href, an <a> is not a real link, so keyboard focus, middle-click, open in new tab, and crawling all break.

Interview focus

This HTML interview question tests whether you can explain HTML href Attribute: Definition, Uses & Examples, connect it to production trade-offs, and handle common follow-up questions.

  • HTML href Attribute: Definition, Uses & Examples explanation without falling back to memorized definitions
  • Links and Attributes reasoning, edge cases, and production failure modes
  • How you would answer the most likely HTML interview follow-up
Practice more HTML interview questions
Interview answer drill

Use this HTML interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

What does href stand for?

href stands for hypertext reference. It is the HTML attribute that holds the destination of a link or a linked resource, and the browser reads it to decide where to navigate or what to load. On an <a> element it is what turns the text into a hyperlink. The same idea powers anchors, stylesheets, base URLs, and image-map areas.

The href meaning in HTML, in one line: the attribute that says where a link or linked resource points. Its value is a URL, absolute or relative, a #fragment on the same page, a mailto: or tel: address, or a file.

In production, a missing or fake href (for example href="#" on an element that only runs JavaScript) breaks keyboard access, open in new tab, browser history, and crawling. That is why the attribute matters beyond decoration.

href examples

Each value below changes what the browser does when the link is activated:

HTML
<!-- Absolute URL: another site -->
<a href="https://developer.mozilla.org/">MDN</a>

<!-- Root-relative URL: same site -->
<a href="/coding">Coding practice</a>

<!-- Relative path: resolved against the current page -->
<a href="guides/intro.html">Intro</a>

<!-- Fragment: scroll to the element with id="contact" -->
<a href="#contact">Contact</a>

<!-- Email and phone -->
<a href="mailto:support@example.com">Email support</a>
<a href="tel:+15551234567">Call us</a>

<!-- File download -->
<a href="/files/report.pdf" download>Download report</a>

<!-- Not only anchors: a stylesheet uses href too -->
<link rel="stylesheet" href="styles.css">
                  

Supported Elements

  • <a> (Anchor Tag): Defines hyperlinks that connect to other web pages or resources.
  • <link>: Defines external resources such as stylesheets or preloaded assets.
  • <base>: Specifies a base URL for all relative links in a document.
  • <area>: Used in image maps to define clickable regions linked to URLs.

Element

Usage Example

Purpose

<a>

<a href="https://example.com">Go</a>

Creates a clickable hyperlink

<link>

<link href="styles.css" rel="stylesheet">

Links an external CSS file

<base>

<base href="https://mysite.com/">

Sets a base URL for relative paths

<area>

<area href="map.html" shape="circle">

Defines a linkable area in an image map

Different HTML elements that use the href attribute.

Absolute vs. Relative URLs

The href value can be absolute or relative:

  • Absolute URL: Points to an external website with the full address.
`__INLINE_CODE_0__ - **Relative URL:** Points to a resource relative to the current page’s location. __INLINE_CODE_1__`

Anchor Links (In-Page Navigation)

You can also use the href attribute to link to specific sections of the same page by using fragment identifiers:

`__INLINE_CODE_0__`

This is often used for smooth scrolling or table-of-contents navigation.

What does href="#" mean?

href="#" points at an empty fragment of the current page. Activating it loads nothing: the browser scrolls to the top of the document and adds a trailing # to the URL, which the Back button then has to step through. Developers use it as a placeholder on elements that only run JavaScript, and that is where it goes wrong: the page still jumps to the top unless the handler calls event.preventDefault(), and screen readers announce a link that behaves like a button.

  • href="#": an empty fragment. Valid, scrolls to the top, changes the URL.
  • href="#!": a fragment that matches no element id, so the page does not scroll, but the URL still changes.
  • href="javascript:void(0)": a javascript: URL that evaluates to undefined, so nothing navigates. A Content Security Policy without unsafe-inline blocks javascript: URLs, linters flag them, and sanitizing an href URL exists to strip them from user input.
  • An element that only runs JavaScript should be a <button> with type="button"; keep <a> for a real destination.
HTML
<!-- Placeholder link: jumps to the top and is announced as a link -->
<a href="#" onclick="openMenu()">Menu</a>

<!-- Same behavior without a destination: a button is the right element -->
<button type="button" onclick="openMenu()">Menu</button>

<!-- A link that stays a link: a real destination, enhanced once JavaScript runs -->
<a href="/menu" onclick="openMenu(event)">Menu</a>
<!-- openMenu(event) calls event.preventDefault() before it opens the panel -->
                  

Other Common Uses

  • Email links: Opens the user’s default email client.
`__INLINE_CODE_0__ - **Phone links:** Opens a phone dialer on mobile devices. __INLINE_CODE_1__ - **Download links:** When combined with the download attribute, triggers file downloads. __INLINE_CODE_2__`

Security and Best Practices

  • Always use absolute URLs when linking to external websites.
  • Add the attribute rel="noopener noreferrer" when using target="_blank" to prevent malicious access to the parent window.
  • Avoid broken or incomplete URLs; they hurt SEO and accessibility.
  • For internal navigation, prefer relative links for flexibility during site deployment.

Still so complicated?

Think of href as the address label on a hyperlink. Without it, the browser has nowhere to go — it’s like clicking on a button with no destination. Once you add href, you’re telling the browser exactly where that link should lead, whether it’s another webpage, a document, or even a specific section on the same page.

Summary

The href attribute specifies the destination or resource reference for hyperlinks and linked content in HTML. It transforms text or other elements into interactive links that guide users to external pages, internal sections, downloadable files, or other resources. Mastering href usage is fundamental for creating a connected, accessible, and user-friendly web experience.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub