Is there a way to margin an anchor in css?

前端 未结 5 1104
你的背包
你的背包 2021-02-12 12:18

I have a fixed header height 50px. In my body, I have a lot of anchors. The problem is that, when I click on links pointing to anchors, the anchor appears under my fixed header

相关标签:
5条回答
  • 2021-02-12 12:29

    If you use jQuery, you use this function:

    function scrollToAnchor() {
        if($(".jquery-anchor").length > 0 && document.URL.indexOf("#") >= 0){
        var anchor = document.URL.split("#")[1];
        $(".jquery-anchor").each(function() {
            if($(this).attr("name") == anchor) {
                $("html,body").animate({
                        scrollTop: $(this).offset().top - 50},
                    'slow');
                }
            });
        }
    }
    

    then add the class "jquery_anchor" to your anchor

    0 讨论(0)
  • 2021-02-12 12:31

    For Pure CSS solution just use another div with oposited margin :)

    <style>
    .anchor {
         margin-top: -50px; 
         margin-bottom: 50px;
    }
    </style>
    
    <div id="n1" class="anchor"></div>
    <div class="box"></div>
    
    0 讨论(0)
  • 2021-02-12 12:36
    // For modern browsers, just add the CSS3 :target selector to the page. 
    // This will apply to all the anchors automatically.
    // ref: http://stackoverflow.com/a/21707103/6220029
    
    :target {
        display: block;
        position: relative;
        top: -120px; 
        visibility: hidden;}
    
    0 讨论(0)
  • 2021-02-12 12:39

    It is an old thread, i know - but maybe other people has the same question.

    An Opportunity is setting a top padding to each anchored div like this:

    .box{ padding-top: 50px; ... }

    Now your anchor scroll will reach the box without any empty containers before, and the padding you add in CSS will give you the space to show the content directly under your header.

    0 讨论(0)
  • 2021-02-12 12:46

    If the header is truly fixed then place your anchors in a scrollable div. Then the div containing the anchor will scroll instead of the entire page. Visit the fiddle and click on anchor1. It goes to anchor2. And so forth.

    http://jsfiddle.net/mrtsherman/CsJ3Y/3/

    css - set overflow hidden on body to prevent default scrolling. Use position absolute on the new content area with top and bottom set. This forces it to stretch to fit the remaining viewport window.

    body { overflow: hidden; }
    #header { position: fixed; top: 0; left: 0; width: 100%; height: 50px; background: gray; border: 1px solid black; }
    #content { 
        overflow: scroll; 
        position: absolute;
        top: 50px;
        bottom: 0px;
        width: 100%;
        border: 1px solid blue; 
    }
    

    html

    <div id="header">header</div>
    <div id="content">
        <div>
            Page Content <br />
            <a id="a1" href="#anchor2" name="anchor1">Anchor 1</a>                
            <a id="a2" href="#anchor1" name="anchor2">Anchor 2</a>   
        </div>
    </div>​
    
    0 讨论(0)
提交回复
热议问题