Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position fixed header in html

Tags:

html

jquery

css

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 fixed value for top margin.

How can this be done?

Here's my CSS:

#header-wrap {     position: fixed;     height: auto;     width: 100%;     z-index: 100 } #container{      /*Need to write css to start this div below the fixed header without mentioning top margin/paading*/ } 

...and HTML:

<div id="header-wrap">   <div id="header">     <div id="menu">       <ul>       <li><a href="#" class="active">test 0</a></li>       <li><a href="#">Give Me <br />test</a></li>       <li><a href="#">My <br />test 2</a></li>       <li><a href="#">test 4</a></li>        </ul>     </div>     <div class="clear">     </div>   </div> </div><!-- End of header -->  <div id="container"> </div> 
like image 826
Sowmya Avatar asked Jun 11 '12 06:06

Sowmya


People also ask

How do you make a header constant in HTML?

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, ...

How do I lock a header in HTML?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.


2 Answers

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/

like image 174
micnic Avatar answered Oct 25 '22 16:10

micnic


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

like image 39
Sowmya Avatar answered Oct 25 '22 14:10

Sowmya