Fixed page header overlaps in-page anchors

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

    I needed something that works for inbound links, links on page, AND that can be targeted by JS so the page can respond to changes in the header height

    HTML

    
    ...
    

    Who?

    ... Can I be clicked?

    What?

    ...

    When?

    CSS

    .fragment-target {
        display: block;
        margin-top: -HEADER_HEIGHTpx;
        padding-top: HEADER_HEIGHTpx;
        z-index: -1;
    }
    

    The z-index: -1 allows links in the 'padding area' above a fragment-target to still be clickable, as commented by @MuttenXd on his answer

    I haven't found an issue yet in IE 11, Edge 15+, Chrome 38+, FF 52+, or Safari 9.1+

提交回复
热议问题