Reputation: 11
I am trying to get the right position of an anchor with a fix header but it won't work. I've tried solutions provided at stackoverflow and many other sites. I couldn't make any solution to work properly.
I don't know if the reason is that I am using grid and viewport units.
My goal is to understand how to make it work just using Html and css only, and why I haven't been able to make it work following the solutions given.
You can see the html and css:
https://codepen.io/sevaro/pen/GPLeyE
/* css*/
#container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 8vh 92vh 92vh 92vh;
grid-template-areas:
"m"
"a"
"w"
"c";
}
nav {
grid-area: m;
background-color: grey;
display: flex;
flex-flow: row;
justify-content: flex-end;
align-items: center;
position: -webkit-sticky;
position: sticky;
top: 0;
}
.about {
grid-area: a;
background-color: blue;
padding-right: 10px;
}
.work {
grid-area: w;
background-color: green;
display: flex;
flex-flow: row wrap;
justify-content: space-evenly;
align-items: center;
}
.contact {
grid-area: c;
background-color: yellow;
}
/* html */
<div id="container">
<nav>
<div class="ab"><a href="#whoami">About</a></div>
<div class="wo"><a href="#whativedone">Work</a></div>
<div class="co"><a href="#contactme">Contact</a></div>
</nav>
<article class="about" id="whoami">
</article>
<article class="work" id="whativedone">
<div class="project">
<img height="50px" width="50px" alt="Project 1">
<p>Project 1</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 2">
<p>Project 2</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 3">
<p>Project 3</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 4">
<p>Project 4</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 5">
<p>Project 5</p>
</div>
</article>
<article class="contact" id="contactme">
</article>
I really appreciate any help.
Thanks
I've tried these: http://nicolasgallagher.com/jump-links-and-viewport-positioning/demo/
These too: https://www.wikitechy.com/technology/css-offsetting-html-anchor-adjust-fixed-header/
and many others.
Upvotes: 1
Views: 8718
Reputation: 1928
Try using scroll-margin-top, it's pretty widely adopted.
Simply add the following CSS to the element you want to scroll to:
.element {
scroll-margin-top: 2em;
}
Upvotes: 15
Reputation: 11
Here it is what worked for me:
CSS
h2{
position:relative;
}
h2 > a{
position:absolute;
top: -12.5vh;
}
HTML
<nav>
<div><a href="#whoami">About</a></div> <!––Link-->
</nav>
<article>
<h2><a id="whoami"></a>ABOUT</h2> <!––Anchor-->
</article>
Hope this helps someone in the future.
Thanks
Upvotes: 0