简体   繁体   English

如何偏移锚链接以清除固定标题?

[英]How to offset a anchor link to clear fixed header?

As the title describes.正如标题所描述的那样。 I am trying to offset the anchor link, so it appears 100px from the top of the viewport.我试图偏移锚链接,因此它从视口顶部出现 100 像素。

This is what I have tried so far.这是我到目前为止所尝试的。

<style>
   :target {
      display: block;
      position: relative;
      top: 100px; 
      visibility: hidden;
      }
 </style>

<a href="#01"> Go to link 01</a> 
  
<h2 id="01"> link 01</h2>

edit:: I think I miss read the online tutorial I was following.编辑:: 我想我想念我正在关注的在线教程。 I have also tried this, but still can't get it to work.我也试过这个,但仍然无法让它工作。

<style>
   :target {
      display: block;
      position: relative;
      top: 100px; 
      margin: -100px 0;
      }
 </style>

<a href="#01"> Go to link 01</a> 

<h2 id="01"> link 01</h2>

The CSS applies on the h2 after you click on the anchor.单击锚点后,CSS 将应用于 h2。 That is how :target css works.这就是 :target css 的工作原理。 Your code will hide the element once you click on the anchor.单击锚点后,您的代码将隐藏该元素。 If that is not required then remove it.如果不需要,则将其删除。

Now your problem of making H2 appear below the header.现在您使 H2 出现在标题下方的问题。 For this you need to either add position absolute or fixed (depends upon your final HTML), instead of relative.为此,您需要添加绝对位置或固定位置(取决于您的最终 HTML),而不是相对位置。

This is the updated code这是更新后的代码

<style>

   :target{
      display: block;
      position: absolute;
      top: 100px; 
      }
 </style>

This seems to work.这似乎有效。

:target:before {
    content: "";
    display: block;
    height: 100px;
    margin: -100px 0 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM