You have a technique, which is not very elegant :
Consists of creating a transparent 1px element that will act as a hidden anchor . With position:absolute
you will remove this element from the page flow and it will not interfere with the other elements, but the anchor will be in it giving the distance from the top you need and without "pushing" or opening spaces between the other elements. p>
Here's a practical example for you to understand better:
OBS: I left the comments in the code to facilitate
html {
overflow-y: scroll;
scroll-behavior: smooth;
}
body {
min-height: 2000px;
padding-top: 70px;
}
div#header{
position: fixed;
top:0px;
width:100%;
height:80px;
background: red;
z-index: 999;
}
div#target{
font-size:40px;
border-top:1px solid red;
height: 80px;
}
/* Ancora Oculta - Classe do elemento que vai ser */
#ancora1, #ancora2 {
position: absolute;
width: 1px;
height: 1px;
left: 0;
margin-top: -100px; /* esse valor varia de acordo com a altura do seu Header, se ele tiver 200px de altura coloque aqui -220px por exemplo */
background-color: transparent;
z-index: -1;
}
<div id="header">
<a href="#ancora1"># Ancora 1 #</a>
<a href="#ancora2"># Ancora 2 #</a>
</div>
<div id="target" style="position: relative;">content!</div>
<div id="target">content!</div>
<div id="target" style="position: relative;">content!</div>
<div id="target">content!</div>
<!-- Elemento com o ID para fazer a Ancora 1 -->
<div id="ancora1"></div>
<div id="target"># Ancora 1 #</div>
<div id="target">content!</div>
<div id="target">content!</div>
<!-- Elemento com o ID para fazer a Ancora 1 -->
<div id="ancora2"></div>
<div id="target"># Ancora 2 #</div>
As I said, it's not a very elegant technique, but it can help you without needing JavaScript