'How do I link to top of a section on the page minus the height of a fixed nav-bar?
When I click a link on my fixed navigation bar, it jumps to a specific section on the page, but the fixed nav-bar's height spills over. How can I subtract that height? Will this require javascript/jquery?
<div id="fixedNavWrapper">
<div id="navLinks">
<ul>
<li><a href="#about">ABOUT ME</a></li>
<li><a href="#portfolio">PORTFOLIO</a></li>
</ul>
</div>
</div>
<section id="about" class="section">
<h2>ABOUT ME</h2>
</section>
.CSS:
#fixedNavWrapper {
position: fixed;
z-index: 1;
width: 100%;
top: 0;
left: 0;
}
#navLinks {
text-align: center;
width: 100%;
height: 57px;
background: #000000;
}
#navLinks ul {
list-style: none;
margin: 0;
padding: 5px;
display: inline-block;
}
#navLinks ul li {
margin: 0;
padding: 0;
display: inline;
}
#navLinks ul li a {
text-decoration: none;
display: block;
float: left;
padding: 0 65px;
height: 45px;
line-height: 45px;
}
Solution 1:[1]
a bit more general:
$('.navbar li a').click(function () {
var speed = 500;
var easing = 'swing';
var topMargin = 10;
var href = $(this).attr("href");
var name = href.substr(href.indexOf("#") + 1);
var $link = $("a[name='" + name + "']");
positionabout = $link.offset().top - $('.navbar').height() - topMargin;
$("html, body").animate({scrollTop: positionabout}, speed, easing);
return false;
});
Solution 2:[2]
just add this to your .css file:
html {scroll-padding-top: 5rem;}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samuel Vicent |
| Solution 2 | Becki6799 |
