Fixed page header overlaps in-page anchors

前端 未结 30 3633
逝去的感伤
逝去的感伤 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:35

    I've got it working easily with CSS and HTML, using the "anchor:before" method mentioned above. I think it works the best, because it doesn't create massive padding between your divs.

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

    It doesn't seem to work for the first div on the page, but you can counter that by adding padding to that first div.

    #anchor-one{padding-top: 60px;}
    

    Here's a working fiddle: http://jsfiddle.net/FRpHE/24/

提交回复
热议问题