Navbar Blocks Content When I Click On An Anchor To Target An Element
The navbar blocks the content when I navigate to an element by using the anchor to target an element. I've tried adding margin-top: 80px but it doesn't work. The content is still b
Solution 1:
Using an anchor
to target an id
will always move the :target
element to the top of the viewport (or as close to the top as technical possible). As such your navbar will "cut" the content. The easiest solution is to use: *:target { padding-top: value; }
to add a padding-top to the targeted element that is higher then the navbar itself.
*:target {
padding-top: 40px;
}
/* for styling pupose only */body {
margin: 0;
}
section {
padding: 10px;
}
section:last-of-type {
margin-bottom: 100vh;
}
nav {
position: sticky;
top: 0;
background-color: darkblue;
padding: 10px;
}
nava {
color: white;
text-decoration: none;
}
<nav><ahref="#section-1">Section 1</a><ahref="#section-2">Section 2</a><ahref="#section-3">Section 3</a><ahref="#section-4">Section 4</a></nav><sectionid="section-1"><h1>Section 1</h1><p>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.</p></section><sectionid="section-2"><h1>Section 2</h1><p>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.</p></section><sectionid="section-3"><h1>Section 3</h1><p>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.</p></section><sectionid="section-4"><h1>Section 4</h1><p>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.</p></section>
Post a Comment for "Navbar Blocks Content When I Click On An Anchor To Target An Element"