Home > Software engineering >  anchor element linking to another section of the same page jumps too far down
anchor element linking to another section of the same page jumps too far down

Time:08-13

HTML:

<nav id="nav-bar">
         <ul>
          <li><a  href="#features">Features</a></li>
          <li> <a  href="#how-made">How They Are Made</a></li>
          <li> <a  href="#pricing">Pricing</a></li>
        </ul>
</nav>

CSS:

 nav > ul {
  display: flex;
  justify-content: space-between;
  align-items: center;

}

li {
  margin: 20px;
  list-style: none;
}

 a {
  color: #000;
  text-decoration: none;
}

When I click on the link in the Nav bar that is fixed on top, it jumps down to the right area but it seems to jump too far down cutting off the top of the section I'm jumping too.

My nav-bar is apart of header that has this code set to it

CSS:

#header-img{
  width: 100%;
  max-width: 200px;
  max-height: 400px;
}

#header{
  display: flex;
  width: 100%;
  top: 0;
  align-items: center;
  margin-bottom: 20px;
  justify-content: space-between;
  Position: fixed;
  background-color: white;
}

CodePudding user response:

Try 'scroll-padding-top' CSS property:

html {
  scroll-padding-top: 100px; <-- replace with height of your position: fixed navbar
}

https://getpublii.com/blog/one-line-css-solution-to-prevent-anchor-links-from-scrolling-behind-a-sticky-header.html

  • Related