Fixed page header overlaps in-page anchors

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

    It feels somewhat hacky to my purist mind but as a css-only solution you can add padding to the active anchored element using the :target selector:

    html, body {height:100%; min-height:100%; margin:0;}
    body {min-height:200%;}
    header {display:inline-block; position:fixed; font-size:1.5em; height:100px; top:0; left:0; right:0; line-height:100px; background:black; text-align:center;}
    header a {color:#fff;}
    section {padding:30px; margin:20px;}
    section:first-of-type, section:target {padding-top:130px;}
    #One #two #three

    One

    Aenean lacinia bibendum nulla sed consectetur. Nullam id dolor id nibh ultricies vehicula ut id elit. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.

    Two

    Aenean lacinia bibendum nulla sed consectetur. Nullam id dolor id nibh ultricies vehicula ut id elit. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.

    Three

    Aenean lacinia bibendum nulla sed consectetur. Nullam id dolor id nibh ultricies vehicula ut id elit. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.

提交回复
热议问题