Fixed page header overlaps in-page anchors

前端 未结 30 3344
逝去的感伤
逝去的感伤 2020-11-22 00:25

If I have a non-scrolling header in an HTML page, fixed to the top, having a defined height:

Is there a way to use the URL anchor (the #fragment part) t

30条回答
  •  时光取名叫无心
    2020-11-22 00:58

    If you can’t or don’t want to set a new class, add a fixed-height ::before pseudo-element to the :target pseudo-class in CSS:

    :target::before {
      content: "";
      display: block;
      height: 60px; /* fixed header height*/
      margin: -60px 0 0; /* negative fixed header height */
    }
    

    Or scroll the page relative to :target with jQuery:

    var offset = $(':target').offset();
    var scrollto = offset.top - 60; // minus fixed header height
    $('html, body').animate({scrollTop:scrollto}, 0);
    

提交回复
热议问题