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.
Definition and Usage Tip: You can use href="#top" or href="#" to link to the top of the current page!
Next, add the top of page link using the # symbol and "top" anchor name/id as the value of the href attribute, as shown below. If you scrolled down on this page, clicking this link takes you back to the top of the page. All modern browsers understand the "#top" value.
Using #top or #bottom The following examples use #top and #bottom with the <a> tag and href attribute to link to that section of the page. This method is similar to using "id," but you don't have to pick a specific element. Click "Top" or "Bottom" in the Results section to see what happens.
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.
You can try using javascript
<a onclick="scroll(0,0)">
Or you can use jQuery
$("#Top").click(function(){ scroll(0,0); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With