Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the anchor landing position

We currently have the problem, that links in the form www.example.com/#section don't jump to the right location, because we have a fixed navigation at the top which covers up the first part of the website.

<div id="anchorpoint">Some content here</div>

How can we tell the browser to jump to the anchor position + 100px?

Thank you.

like image 821
Simon Ferndriger Avatar asked May 10 '14 12:05

Simon Ferndriger


People also ask

How do you prevent anchors from scrolling behind a sticky header?

You could add the scroll-padding-top CSS property to an HTML element with a value of 4rem . Now when you click the anchor link, the browser jumps to the anchor section but leaves padding of 4rem at the top, rather than scrolling the anchor point all the way to the top.

How do I change the position of an anchor tag in CSS?

Anchors are inline elements per default. If you give an element position: absolute , you take it out of so-called normal document flow. It will orientate it starting x and y position from the webbrowser canvas (the document area) or from the nearest parent element having position: relative or absolute assigned to.

How do I center an anchor tag?

To center a text inside a anchor tag, add the text-align:center to the anchor CSS class. “text-align: center” centers the text horizontally. or we can add the inline styles to anchor element using the style attribute in HTML.


2 Answers

HTML (add an additional anchor tag)

 <a id="anchorpoint" class="anchor"></a>
 <div>Some content here</div>

CSS

.anchor {
    display:block;
    padding-top:100px;
    margin-top:-100px;
 }

It's a slight modification of Fixed position navbar obscures anchors. The advantage lies there, that you don't prepopulate padding and margin of the actual container.

like image 149
Simon Ferndriger Avatar answered Oct 25 '22 08:10

Simon Ferndriger


This worked for me:

<a href="#learnmore"><button type="button" class="large button">Click me!</button></a>
<a class="hidden-anchor" name="learnmore"></a>

And the css for the anchor link.

 .hidden-anchor {
    position: absolute;
    top: -100px;
 }
like image 43
Maximus Avatar answered Oct 25 '22 07:10

Maximus