Why href="#" Jumps to the Top—and What to Use Instead

Learn why an href hash jumps to the top and when to replace it with a real link, an in-page anchor, or an accessible button.

Published
Reading time
5 min read
Why href="#" Jumps to the Top—and What to Use Instead
On this page

If href="#" jumps to the top when you click a link, the browser is following the link—not treating it as a placeholder. The # introduces a URL fragment, and an empty fragment normally navigates to the top of the current document. The right replacement depends on whether the control should navigate or perform an action.

Key Takeaways

  • Use a link with a real URL to navigate to another page.
  • Use a link such as href="#shipping" to navigate to an element with id="shipping" on the current page.
  • Use a <button type="button"> for an action that does not navigate.
  • Check the replacement with a keyboard and, where possible, a screen reader.

Why an href hash jumps to the top

An anchor with an href is a link. In <a href="#">, the fragment after # is empty. Following it normally takes the user to the top of the document, so a long page may visibly scroll upward. The URL may also change, and fragment navigation can affect browser history.

The effect may not be obvious if the page is already at the top or has little content to scroll. Existing JavaScript that cancels the link's default behavior can also prevent the jump.

href="#" is therefore not a neutral way to mark unfinished navigation. Decide what the control is supposed to do before replacing it.

Advertisement

Use a descriptive link with the actual destination:

<a href="/about">About our company</a>

Replace /about with a page that exists on your site. This preserves normal link behavior, including opening the destination in a new tab or copying its address.

Give the destination a unique id and use that value after #:

<a href="#shipping">Jump to shipping information</a>

<!-- Other page content can appear here. -->

<h2 id="shipping">Shipping information</h2>
<p>Orders are prepared for delivery after payment is confirmed.</p>

Clicking the link navigates to the heading's fragment. The address can be shared so someone else can navigate directly to that section. Check that the ID is spelled identically in both places and appears only once on the page.

A fixed header can cover the destination after scrolling. If that happens, a CSS offset can help:

#shipping {
  scroll-margin-top: 5rem;
}

Adjust the value to fit your header. The heading's exact position can also depend on the available space at the end of the page.

Perform an action without navigating

Use a button when the control changes something on the current page instead of taking the user to a destination. This example displays a short message:

<button type="button" id="show-tip">Show tip</button>
<p id="tip" role="status"></p>

<script>
  const button = document.getElementById('show-tip');
  const tip = document.getElementById('tip');

  button.addEventListener('click', () => {
    tip.textContent = 'Use a descriptive label for each link.';
  });
</script>

Place the script after the button and paragraph, as shown. JavaScript implements the action; it is not needed merely to stop a link from navigating. type="button" also prevents this button from submitting a surrounding form. The status region is intended to announce the new message to assistive technology, but announcement behavior should be checked in the combinations you support.

This example is illustrative and was not executed for this article.

Advertisement

Check the replacement with a keyboard and screen reader

These checks follow the distinction between link and button semantics. The W3C WAI guidance requested for this topic was not available to consult for this article; check its current link and button guidance when validating your implementation.

  1. Use only the keyboard. Press Tab until the control has a visible focus indicator. Press Enter on a link to follow it. For a button, check that both Enter and Space activate its action.
  2. Check the destination or result. A navigation link should reach the intended page or section. An action button should perform the stated action without an unwanted page jump. For an in-page link, check the visible scroll position; focus behavior can vary by browser and target.
  3. Listen with a screen reader. Navigate to the control and check that its name describes its purpose and its role is announced as a link or button as appropriate. For the message example, activate the button and check whether the new status is announced.
  4. Check the focus indicator. If custom CSS removes or obscures the browser's indicator, provide a clearly visible replacement.

Test in the browsers and assistive technologies your audience uses. A screen reader check complements keyboard testing; it does not replace it.

Common Mistakes

  • Replacing # with an empty href. href="" still represents a link and can navigate to the current page; it is not a button.
  • Adding a click handler solely to cancel the jump. If the control performs an action, use a button with an action handler instead of disguising a link as a button.
  • Using javascript: as the destination. It obscures the control's purpose and is not a substitute for an appropriate link or button.
  • Linking to a missing or misspelled ID. Verify the fragment and destination together, especially after editing page headings.
  • Using vague labels. A label such as “Show tip” or “Shipping information” explains the result better than “Click here.”

Best Practices

Choose the element based on what happens: navigation uses an <a href="…">; an in-page action uses a <button>. Keep link destinations and element IDs meaningful, and check that controls remain usable without a mouse. If a button changes content, make the result clear visually and test whether assistive technology can perceive it.

Conclusion

href="#" is an empty-fragment link, so a jump to the top is normal navigation behavior. Replace it with a real destination, a link to an existing section, or a button for an action.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.