Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

anchor

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.

like image 652
hugomg Avatar asked Aug 01 '12 00:08

hugomg


People also ask

How do you make a link go to the top of a web page?

Definition and Usage Tip: You can use href="#top" or href="#" to link to the top of the current page!

How do you make a link go to the top of the page in HTML?

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.

How would you implement the top and bottom link of a website?

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.


2 Answers

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.

like image 152
hugomg Avatar answered Oct 08 '22 21:10

hugomg


You can try using javascript

<a onclick="scroll(0,0)">

Or you can use jQuery

$("#Top").click(function(){  scroll(0,0); }); 
like image 42
user1513192 Avatar answered Oct 08 '22 20:10

user1513192