1

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;
}
myom
  • 135
  • 2
  • 16
  • 1
    This is called "fixed navbar covering up anchor links" and there's a number of solutions. I use this one: http://stackoverflow.com/a/20320919/1004312 – Christina Oct 15 '14 at 03:52

4 Answers4

2

I think that you can't do it with css. However you can use jQuery to do it. Quite easy:

HTML (add classes to li elements to identify them in your jQuery code):

<div id="fixedNavWrapper">
    <div id="navLinks">
        <ul>
            <li class="about"><a href="#about">ABOUT ME</a></li>
            <li class="portfolio"><a href="#portfolio">PORTFOLIO</a></li>
        </ul>
    </div>
</div>

<section id="about" class="section">
    <h2>ABOUT ME</h2>
</section>

JS (Use event .click to move your "body" to the correct position )

$('li.about').click(function(){
 positionabout = $('#about').offset().top - $('#fixedNavWrapper').height(); // Position of #about - nav height = correct position
 $("html, body").animate({scrollTop:positionabout}, '500', 'swing');
})
$('li.portfolio').click(function(){
 positionport = $('#portfolio').offset().top - $('#fixedNavWrapper').height();
 $("html, body").animate({scrollTop:positionport}, '500', 'swing');
})
Héctor
  • 509
  • 2
  • 7
0

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;
});
Samuel Vicent
  • 991
  • 10
  • 16
0

My problem : link redirect to other page anchor - when I click on it Im redirect to the other page with my navbar of 10 rem.. so the problem is I could not see the full article because the navbar was above the article.

scroll-padding-top: 10rem;

Was the best answer for me. Thanks @Becki6799

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33689408) – ahuemmer Jan 27 '23 at 13:37
-1

just add this to your .css file:

html {scroll-padding-top: 5rem;}