CodeNewbie Community 🌱

Cover image for How to not cut off the top of your section when using anchor tags
𝕁𝕦𝕝𝕚𝕒 👩🏻‍💻
𝕁𝕦𝕝𝕚𝕒 👩🏻‍💻

Posted on • Edited on

How to not cut off the top of your section when using anchor tags

When I created my personal portfolio, I used anchor tags in my sticky navigation bar to make it easy for readers to jump directly to the section they were interested in. But when working with a sticky navigation bar, the top of each section was cut off by the height of the navigation bar.

Preview of the problem

NEW APPROACH

Here's how I solved this problem

Add the scroll-margin-top property to the section and set the value equal to the height of the header.

style.css

.section-id {
  scroll-margin-top: 20vh;
}
Enter fullscreen mode Exit fullscreen mode

index.html

<section id="section-id">
  <div>
    CONTENT
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

OLD APPROACH
Create an anchor tag directly under each section, give it a class, and add the following properties to it:

style.css

.anchor {
  display: block;
  position: relative;
  top: -90px; /* this is your offset */
  visibility: hidden;
}
Enter fullscreen mode Exit fullscreen mode

The offset depends on the height of your navigation bar.

Your sections in your HTML file should look like this:

index.html

<section id="section-id">
  <a class="anchor" id="anchor-id"></a>
  <div>
    CONTENT
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

If you want to take a look at my website 👉 https://julia-undeutsch.netlify.app/


Thank you

Thanks for your reading and time. I really appreciate it!

Top comments (9)

Collapse
 
ciara_veum profile image
Ciara Veum

Anchor tags are a great way to add text and images to your blog posts. They allow you to tag certain words or phrases, so that when someone searches for those words in their search engine, the article will show up in the results list. You can download beautiful templates about graphic design from this MasterBundles site. You can also use anchor tags for internal links, which is what I did in this post. When you use anchor tags, be sure not to cut off the top of your section. This will cause your site to look messy and unprofessional, since it won't look like it belongs on a blog.

Collapse
 
niaernes profile image
niaernes • Edited

I've been having trouble with Geometry Dash anchor tags lately. Whenever I click on a link to jump to a specific section, the top part of the section gets cut off. I've tried adding padding and using scrollIntoView(), but it's still not perfect.

Collapse
 
keydotepsa profile image
Keydotepsa

Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. Popcorn game online

Collapse
 
squiedcpu profile image
Squied-cpu • Edited

When using anchor tags, cutting off the top of your section is a common issue. A simple fix is adding padding or margin to the element you're linking to. This ensures the content stays visible. While solving this, it’s important to consider user experience, just like how poki games offers smooth gameplay. A clear view of content, much like a well-structured game interface, makes all the difference!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Useful tip.

Collapse
 
chickenworst profile image
chickenworst • Edited

To prevent the top of a section from being cut off when using anchor tags, you can use CSS to add padding or margin to the top of the section. geometry dash

Collapse
 
anna profile image
truong

This is the wisdom I spent my thirties searching for! the henry stickmin collection If only I could have learned it sooner!

Collapse
 
keydotepsa profile image
Keydotepsa

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. block blast game

Collapse
 
mickdollentas profile image
Mickey

Just what I was looking for, thank you so much!