2014-05-02 70 views
0

我有兩個div。如何根據另一個div的邊距來定位div?

<div class="header"></div> 

<div class="container"> 
<p>This is the container.</p> 
</div> 

這是css代碼。

.header{ 
    width:100%; 
    height:100px; 
    position:fixed; 
    background-color:#eee; 
} 
.container{ 
    margin:0 auto; 
    margin-top:100px; 
    margin-bottom:100px; 
} 

爲什麼標題會下降,儘管我在容器上放了一個邊距頂部?有人可以建議我有一個固定的標題以及其他容器中的東西,就像任何其他常用頁面一樣嗎?

+1

'位置:fixed'取頭了公文流轉,因此利潤率不知道它的存在。要了解更多關於CSS中的佈局,請查看[這個有用的網站](http://learnlayout.com/) –

+0

讓我們成爲一個小提琴或codepen,所以我們得到你的問題的全貌?不知何故,你的CSS似乎是連貫的,但....你有一個問題:) –

回答

1

您需要添加top:0;left: 0;

.header{ 
width:100%; 
height:100px; 
position:fixed; 
background-color:#eee; 
top: 0; 
left: 0; 

} 

看一看here

+0

左邊是缺少:) –

+0

@GCyrillus好點補充:) – user3507121