Dani Springer Dani Springer - 5 months ago 12
HTML Question

href="#top" Works By Itself

I added this:

<a href="#top">text</a>
to a web page.

I then clicked on it without adding
id="top"
anywhere, and it worked (brought me to top of page).

How come?

Answer

This is built into HTML5 according to the spec:

If target is the top of the document, then scroll to the beginning of the document for the Document

In other words, it's a special case and you don't need to specify an ID for the top of the page.

See also MDN:

Note: You can use the special fragment "top" to create a link back to the top of the page; for example <a href="#top">Return to top</a>