2012-06-11 15 views
43

固定位置のヘッダー(動的高さ)があります。htmlの位置固定ヘッダー

ヘッダーのすぐ下にコンテナdivを配置する必要があります。ヘッダーの高さは動的なので、固定値を上余白に使用することはできません。

どうすればいいですか?

ここに私の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*/ 
} 

...と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> 
+5

固定ヘッダは、レイアウトの一部ではありません。それは浮いている。ヘッダーがあるかのように動作するように、コンテンツに「margin-top」を与える必要があります。 – Blender

+3

これを読んでくださいhttp://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/ – sandeep

+0

[固定ヘッダーを持つCSS専用のスクロール可能なテーブル]の可能な複製(http:// stackoverflow。 com/questions/11891065/css-only-scrollable-table-with-fixed-headers) –

答えて

6

まあへ!今私の質問を見て、私はヘッダーの動的な高さのために固定マージン値は言及したくないことに気付きました。

ここでは、このようなシナリオで私が使用しているものを示します。

jQueryを使用してヘッダーの高さを計算し、それを上余白の値として適用します。

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

Demo

53

あなた#containerは#header-のための固定の高さを指定し、その後、#ヘッダラップの外にする必要があります折り返し、#header-wrapの高さに等しい#containerのmargin-topを指定します。このような何か:

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

希望、これは何が必要です:あなたが場合でも、ユーザーがスクロールダウンし、それがページの上部に滞在したいので、私はあなたのヘッダが固定されていると仮定し http://jsfiddle.net/KTgrS/

+0

はい、それは外側です。ヘッダーの内容がページごとに動的に変化するため、ヘッダーとコンテナー間のギャップが少なくなるように、余白を修正できます。 – Sowmya

+0

追加されたJsFiddle:http:// jsfiddle。net/KTgrS/ – micnic

+0

あなたは 'position:absolute'を試しましたか? – poepje

1

が、あなたはコンテナをカバーしたくない。しかし、position: fixedは、ページの線形レイアウトから要素を削除するので、 "next"要素の上マージンをヘッダーの高さと同じにするか、(何らかの理由で 'それをやりたがっている)、ページフローにスペースを占めるプレースホルダ要素を配置しますが、ヘッダーが表示される場所の下に表示されます。

1

position :fixedは、他のレイアウトと異なります。 headerfixedpositionになったら、contentのdivにmargin-topを設定する必要があることに注意してください。

0

セット#containerのdivのトップゼロ

#container{ 


top: 0; 



} 
3
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; 
} 
+1

リファレンス - http://limpid.nl/lab/css/fixed/header – Sorter