position fixed header in html

前端 未结 6 1500
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 01:51

I have a header (dynamic height) with a fixed position.

I need to place the container div right below the header. As the header height is dynamic, I can\'t use the

相关标签:
6条回答
  • 2020-12-01 02:22
    body{
      margin:0;
      padding:0 0 0 0;
    }
    div#header{
      position:absolute;
      top:0;
      left:0;
      width:100%;
      height:25;
    }
    @media screen{
     body>div#header{
       position: fixed;
     }
    }
    * html body{
      overflow:hidden;
    } 
    * html div#content{
      height:100%;
      overflow:auto;
    }
    
    0 讨论(0)
  • 2020-12-01 02:22

    I assume your header is fixed because you want it to stay at the top of the page even when the user scrolls down, but you dont want it covering the container. Setting position: fixed removes the element from the linear layout of the page however, so you would need to either set the top margin of the "next" element to be the same as the height of the header, or (if for whatever reason you don't want to do that), put a placeholder element which takes up space in the page flow, but would appear underneath where the header shows up.

    0 讨论(0)
  • 2020-12-01 02:27

    The position :fixed is differ from the other layout. Once you fixed the position for your header, keep in mind that you have to set the margin-top for the content div.

    0 讨论(0)
  • 2020-12-01 02:34

    set #container div top to zero

    #container{ 
    
    
     top: 0;
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-01 02:41

    Well! As I saw my question now, I realized that I didn't want to mention fixed margin value because of the dynamic height of header.

    Here is what I have been using for such scenarios.

    Calculate the header height using jQuery and apply that as a top margin value.

    var divHeight = $('#header-wrap').height(); 
    $('#container').css('margin-top', divHeight+'px');
    

    Demo

    0 讨论(0)
  • 2020-12-01 02:42

    Your #container should be outside of the #header-wrap, then specify a fixed height for #header-wrap, after, specify margin-top for #container equal to the #header-wrap's height. Something like this:

    #header-wrap {
        position: fixed;
        height: 200px;
        top: 0;
        width: 100%;
        z-index: 100;
    }
    #container{ 
        margin-top: 200px;
    }
    

    Hope this is what you need: http://jsfiddle.net/KTgrS/

    0 讨论(0)
提交回复
热议问题