Skip to main content

How do you jump to an element by ID via a link in HTML?

To jump to an element by ID via a link in HTML, you need to:

  1. Assign the element a unique identifier (id).
  2. In the <a> link, specify this identifier after the # character in the href attribute.

Example:

html
<a href="#section1">Go to section 1</a> <h2 id="section1">Section 1</h2> <p>This is the text of the needed section...</p>

When the link is clicked, the browser scrolls the page to the element with id="section1".


html
<a href="about.html#team">Our team</a>

The about.html page must have this element:

html
<h2 id="team">Team</h2>

After the click, it navigates to the about.html page and scrolls to the #team block.


You can add smooth scrolling:

To make the transition look smooth, add CSS:

css
html { scroll-behavior: smooth; }

Conclusion:

To jump to an element by ID, you need to:

  1. Assign the element an id, for example id="contact".
  2. Create the link <a href="#contact">To contacts</a>. When clicked, the browser automatically scrolls the page to the element with this ID.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.