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
This is how I got it to finally go to the proper place when you click on the navigation. I added an event handler for the navigation clicks. Then you can just use "scrollBy" to move up on the offset.
var offset = 90;
$('.navbar li a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
Here is a complete jquery solution that will work in IE:
Suppose the navigation bar elements are something like this:
<ul>
<li><a class="navigation" href="/#contact-us">Contact us</a></li>
<li><a class="navigation" href="/#about-us">About us</a></li>
</ul>
You can use the following jquery snippet to offset the scroll:
$(function() {
$("a.navigation").click(function(event) {
event.preventDefault();
offSetScroll($(this));
});
offSetScrollFromLocation(window.location.href.toLowerCase());
});
function offSetScroll(anchor) {
var href = anchor.attr("href");
offSetScrollFromLocation(href);
}
function offSetScrollFromLocation(href) {
//Change this according to the height of the navigation bar
var fromTop = 250;
if(href.indexOf("#")<=0)
return;
var hash=href.substring(href.indexOf("#"));
var targetOffset = $(hash).offset().top-fromTop;
$('html, body').animate({scrollTop: targetOffset}, 400, function(e) {
});
}
The best way that I found to handle this issue is (replace 65px with your fixed element height):
div:target {
padding-top: 65px;
margin-top: -65px;
}
If you do not like to use the target selector you can also do it in this way:
.my-target {
padding-top: 65px;
margin-top: -65px;
}
Note: this example will not work if the target element have a backgound color that differant from his parent. for example:
<div style="background-color:red;height:100px;"></div>
<div class="my-target" style="background-color:green;height:100px;"></div>
in this case the green color of my-target element will overwrite his parent red element in 65px. I did not find any pure CSS solution to handle this issue but if you do not have another background color this solution is the best.
You can now use scroll-margin-top, which is pretty widely adopted.
Simply add the following CSS to the element you want to scroll to:
.element {
scroll-margin-top: 2em;
}
I've got it working easily with CSS and HTML, using the "anchor:before" method mentioned above. I think it works the best, because it doesn't create massive padding between your divs.
.anchor:before {
content:"";
display:block;
height:60px; /* fixed header height*/
margin:-60px 0 0; /* negative fixed header height */
}
It doesn't seem to work for the first div on the page, but you can counter that by adding padding to that first div.
#anchor-one{padding-top: 60px;}
Here's a working fiddle: http://jsfiddle.net/FRpHE/24/
I wasn't having any luck with the answer listed above and ended up using this solution which worked perfectly...
Create a blank span where you want to set your anchor.
<span class="anchor" id="section1"></span>
<div class="section"></div>
And apply the following class:
.anchor {
display: block;
height: 115px; /* same height as header */
margin-top: -115px; /* same height as header */
visibility: hidden;
}
This solution will work even if the sections have different colored backgrounds! I found the solution at this link.