Search code examples
htmlanchor

Create a Link to the Top of a Web Page Without Using Anchors


I have an HTML document that currently has some links to the top of the page using an anchor

<body id="topofpage">
   ...
   <a href="#topofpage">Go to top</a>

However, I don't like needing to create a useless id and how the "#topofpage" shows up in the URL after I click the link. Is there something else I can do to link to the top of the page without using Javascript? I tried removing the anchor completely with <a href=""> but that causes the page to refresh.


Solution

  • According to the HTML5 spec the empty fragment # and the special fragment #top will link to the top of the page.

    <a href="#">Go to top</a>
    
    <a href="#top">Go to top</a>
    

    There is no need to create a matching anchor if you use these special fragment names.