Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: Making a link lead to the anchor centered in the middle of the page

I have a link to an anchor on my html page. When the link is clicked it causes the page to scroll to the anchor so that the anchor is at the very top of the visible part of the page. How can I make the page scroll so that the anchor will be in the middle of the page?

like image 470
olamundo Avatar asked Sep 13 '09 21:09

olamundo


People also ask

How do I add an anchor point to a URL?

If the anchor is in the same section (page), you can insert a link to that anchor point using "Insert/edit external link" and in the URL field type in the pound sign or hashtag and then the name of the anchor with no space between them; the URL should look something like: #name-of-anchor.

How do I link to an anchor to another page in HTML?

You need to use the href attribute to link to another page. The value of the href attribute is usually a URL pointing to a web page (like the one above). You can also link another HTML element or a protocol (for example, sending email), and you can execute JavaScript using the href attribute.


2 Answers

I found a solution Anchor Links With A Fixed Header posted by Phillip, he is a web designer. Phillip added a new EMPTY span as the anchor position.

<span class="anchor" id="section1"></span> <div class="section"></div> 

then put the following css code

.anchor{   display: block;   height: 115px; /*same height as header*/   margin-top: -115px; /*same height as header*/   visibility: hidden; } 

Thanks, Phillip!

like image 138
charles.cc.hsu Avatar answered Sep 25 '22 11:09

charles.cc.hsu


Place a <span class="anchor" id="X"> then style the anchor class like:

.anchor {   position: absolute;   transform: translateY(-50vh); } 

With -50vh the anchor will scroll in the middle of the screen.

like image 25
Gianluca Pietrobon Avatar answered Sep 26 '22 11:09

Gianluca Pietrobon