When using #id in an href to navigate to sections of a website, the page should not reload unless there is some issue with your setup or additional JavaScript that causes the reload.
Here are steps to troubleshoot and solve the problem:
Your links should look like this:
<a href="#section1">Go to Section 1</a>
<div id="section1">This is Section 1</div>
If the id attribute matches the value in the href, the browser should smoothly scroll to that section without reloading.
JavaScript can interfere with default behavior. For example:
- If you have a JavaScript
onclick event attached to the link that includes event.preventDefault() but doesn’t handle the scroll, the page won’t navigate correctly.
- Or, some JavaScript might explicitly force a page reload on clicks.
Check your JavaScript code and look for issues like this:
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', (event) => {
// If you're preventing the default action, ensure you're manually scrolling:
const target = document.querySelector(link.getAttribute('href'));
target.scrollIntoView({ behavior: 'smooth' });
Make sure you don’t have any conflicting JavaScript that unintentionally triggers a reload.
Ensure the href values are valid and point to the correct id. If your href is malformed, the browser might interpret it as a new page load.
Examples of valid and invalid href:
<a href="#about">About</a>
<div id="about">About Section</div>
<a href="about">About</a> <!-- This will reload the page if "about" is not a separate file -->
<div id="about">About Section</div>
If your website has a <base> tag in the <head> like this:
<base href="https://example.com/">
It might cause relative #id links to misbehave and reload the page. Try removing the <base> tag or adjusting it.
If you’re using a framework like React, Angular, or Vue, some built-in page transition libraries may cause this issue. They often override the default browser behavior.
In React, for instance, use Link from react-router-dom or disable page transitions explicitly for #id navigation.
Example for React:
<a href="#about" onClick={(e) => {
document.querySelector('#about').scrollIntoView({ behavior: 'smooth' });
Sometimes external libraries (e.g., smooth scrolling libraries, single-page application frameworks) can interfere with native browser behavior.
- Temporarily disable such libraries and test.
- If the problem goes away, review the library’s documentation for proper integration.
Look for other causes of page reload, such as:
- Forms submitting unintentionally.
- Buttons with
type="submit" instead of type="button".
- JavaScript code explicitly calling
window.location.
By following these steps, you should be able to identify and fix the issue causing the page to reload when navigating with #id. Let me know if you need help debugging further!