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?
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.
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.
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!
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.
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