Fixed page header overlaps in-page anchors

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

    I created a div with a few line breaks and gave that the id, I then put the code I wanted to show underneath. The link would then take you to the space above the image and the header would no longer be in the way:

    <a href="#image">Image</a>
    <div id="image"><br><br></div>
    <img src="Image.png">
    

    Of course, you can change the number of line breaks to suit your needs. This worked perfectly for me, not sure if there are any problems though, I am still learning HTML.

    0 讨论(0)
  • 2020-11-22 00:40

    For Chrome/Safari/Firefox you could add a display: block and use a negative margin to compensate the offset, like:

    a[name] {
        display: block;
        padding-top: 90px;
        margin-top: -90px;
    }
    

    See example http://codepen.io/swed/pen/RrZBJo

    0 讨论(0)
  • 2020-11-22 00:43
    html {
      scroll-padding-top: 70px; /* height of sticky header */
    }
    

    from: https://css-tricks.com/fixed-headers-on-page-links-and-overlapping-content-oh-my/

    0 讨论(0)
  • 2020-11-22 00:43

    It works for me:

    HTML LINK to Anchor:

    <a href="#security">SECURITY</a>
    

    HTML Anchor:

    <a name="security" class="anchor"></a>
    

    CSS :

    .anchor::before {
        content: "";
        display: block;
        margin-top: -50px;
        position: absolute;
    }
    
    0 讨论(0)
  • 2020-11-22 00:43
    <div style="position:relative; top:-45px;">
        <a name="fragment"> </a>
    </div>
    

    This code should do the trick. Swap out 45px for the height of your header bar.

    EDIT: If using jQuery is an option, I've also been successful using jQuery.localScroll with an offset value set. The offset option is a part of jQuery.scrollTo, which jQuery.localScroll is built upon. A demo is available here: http://demos.flesler.com/jquery/scrollTo/ (second window, under 'offset')

    0 讨论(0)
  • 2020-11-22 00:45

    I had the same problem. I solved it by adding a class to the anchor element with the topbar height as the padding-top value.

    <h1><a class="anchor" name="barlink">Bar</a></h1>
    

    And then simply the css:

    .anchor { padding-top: 90px; }
    
    0 讨论(0)
提交回复
热议问题