Sobes.tech
Junior

What is an 'anchor' in web development?

sobes.tech AI

Answer from AI

An anchor is a marker inside an HTML document that allows creating a direct link to a specific location on the page. Anchors are most commonly used for navigation within a single web page.

An anchor is implemented using the id attribute on an HTML element and a link (<a>) with an href attribute starting with the # symbol followed by the anchor's name (id).

<a href="#section2">Go to the second section</a>

<section id="section1">
  <h2>First Section</h2>
  <p>Content of the first section.</p>
</section>

<section id="section2">
  <h2>Second Section</h2>
  <p>Content of the second section.</p>
</section>

Clicking on a link with href="#section2" will automatically scroll the browser to the element with id="section2".

Comparison table of using anchors and regular links:

Characteristic Anchor (href="#id") Regular link (href="/page")
Where the link leads To a specific place on the current page To another page or resource
Symbol in href Starts with # Usually starts with / or a protocol (http://)
Function Navigation within the page Navigation between pages