2012-04-27 39 views

回答

1

這裏是DEMO

HTML是:

<div id="header" class="fixed" style="display: block; top: 0px; "> 
     <div class="inner"> 
     <!-- .inner --> 
     </div> 
     <div class="shadow" style="display:block;"><span></span></div> 
</div> 

<div style="height:600px;"><a href="http://imgur.com/k9hz3"><img src="http://i.imgur.com/k9hz3.jpg" title="Hosted by imgur.com" alt="" /></a></div> 
<div style="height:600px;"><a href="http://imgur.com/TXuFQ"><img src="http://i.imgur.com/TXuFQ.jpg" title="Hosted by imgur.com" alt="" /></a></div> ​ 

CSS是:

#header{height: 48px;width: 100%;z-index: 1000;} 

.fixed {background: url(http://static.tumblr.com/njty47g/Omoleufdi/topbar_bg.png) left top repeat-x; 
position: fixed;left: 0; 
-webkit-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12); 
-moz-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12); 
box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);} 

.inner {width: 940px;margin: 0 auto;height: 48px;} 


.shadow{width: 100%;height: 5px;position: absolute;left: 0;bottom: -5px;z-index: 500;display: none;} 
​ 
3

你可以用CSS做,這個類將用於與「頭」的ID的DIV工作:

#header { 
    position:fixed; 
    top:0; 
    z-index: 1000; 
} 
1

,如果你想使你的頭部寬度爲100%,那麼你可以使用

.headerDiv{ 
position:fixed; 
top:0; 
width:100%; 

}

0

Position: fixed;,將其固定在窗口 Top:0;將確保它在那裏, Left:0;可以添加,以確保它的左太, z-index:1000;將確保它保持在您的網頁

2

繼承人的代碼上一切解釋說:_

#header { 
    position: fixed; /* keeps the element in a fixed position no matter how much you scroll */ 
    top: 0; /* to keep it on the top */ 
    width: 100%; /* to keep its width across the screen no matter how big the screen is */ 
    z-index: 1000; /* to make sure its infront of everything */ 
} 

它總是很好的保持你的代碼註釋,以便如果你忘記了什麼,你可以看看評論。 :)