To efficiently address this issue, a 2-column grid layout can be utilized. Within the grid container (in this scenario, the body element), an aside
and a main
element can be used as primary content.
Inside the aside
, you can incorporate a nav
element and apply position: sticky; top: 0;
to ensure that the navbar remains within the viewport.
* {
box-sizing: border-box;
}
body {
margin: 0;
display: grid;
grid-template-columns: repeat(2, 1fr);
}
nav {
position: sticky;
top: 0;
min-height: 100vh;
}
/* for visualisation purpose only */
main {
min-height: 5000vh;
border: 2px dashed red;
}
nav {
border: 2px dashed blue;
}
<aside>
<nav>This is the navbar</nav>
</aside>
<main>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</main>