If I have a non-scrolling header in an HTML page, fixed to the top, having a defined height:
Is there a way to use the URL anchor (the #fragment
part) t
I created a div with a few line breaks and gave that the id, I then put the code I wanted to show underneath. The link would then take you to the space above the image and the header would no longer be in the way:
<a href="#image">Image</a>
<div id="image"><br><br></div>
<img src="Image.png">
Of course, you can change the number of line breaks to suit your needs. This worked perfectly for me, not sure if there are any problems though, I am still learning HTML.
For Chrome/Safari/Firefox you could add a display: block
and use a negative margin to compensate the offset, like:
a[name] {
display: block;
padding-top: 90px;
margin-top: -90px;
}
See example http://codepen.io/swed/pen/RrZBJo
html {
scroll-padding-top: 70px; /* height of sticky header */
}
from: https://css-tricks.com/fixed-headers-on-page-links-and-overlapping-content-oh-my/
It works for me:
HTML LINK to Anchor:
<a href="#security">SECURITY</a>
HTML Anchor:
<a name="security" class="anchor"></a>
CSS :
.anchor::before {
content: "";
display: block;
margin-top: -50px;
position: absolute;
}
<div style="position:relative; top:-45px;">
<a name="fragment"> </a>
</div>
This code should do the trick. Swap out 45px for the height of your header bar.
EDIT: If using jQuery is an option, I've also been successful using jQuery.localScroll with an offset value set. The offset option is a part of jQuery.scrollTo, which jQuery.localScroll is built upon. A demo is available here: http://demos.flesler.com/jquery/scrollTo/ (second window, under 'offset')
I had the same problem. I solved it by adding a class to the anchor element with the topbar height as the padding-top value.
<h1><a class="anchor" name="barlink">Bar</a></h1>
And then simply the css:
.anchor { padding-top: 90px; }