2016-03-06 76 views
0

我在我的網站上有一個div,不會在頂部,它總是在頂部下方一點點,所以我正在尋找一種方法使它在頂部沒有空間在div和頂部之間,但我還沒有運氣。Div不會在頁面頂部

這是我試過的css代碼。

.usertopbar { 
    display: inline-block; 
    background: url(images/counterbar_bg.jpg) repeat-x #111; 
    height: 32px; 
    bottom: 600px; 
    width: 1100px; 
    line-height: 30px; 
    text-align: left; 
    padding-left: 10px; 
    box-shadow: 0px 0px 16px #000; 
    border-bottom-right-radius: 8px; 
    border-bottom-left-radius: 8px; 
} 

回答

1

您的代碼看起來不錯(雖然我傾向於避免明確不惜一切代價設置的東西的高度)。我建議在你自己的CSS之前包括normalize.css。它會重置幾個瀏覽器的默認值,這是我相信在這裏遇到的。它也是Bootstrap和Foundation框架中使用的默認值,所以你可以打賭它是最好的重置樣式表之一。

+0

Normalize.css不需要使用提供的代碼來實現他想要的內容。 – Roy

+0

確定它不是*必需*,但如果您想要更好的跨瀏覽器功能,它將成爲開發人員工具箱中相當普遍的主食。也許更重要的是,OP應該知道每個瀏覽器都有自己的內置默認樣式,它要應用於html元素。類似normalize.css的解決方案旨在重置所有這些樣式,以便您有一個更清晰的空白畫布開始工作。 – hellojason

1

設置margin和padding或者您<html><body>爲0。具體而言,我認爲這是做它的<body>的保證金,但養成習慣通過明確它們都設置爲0默認。

+1

除了@憲兵的答案,你可以看看這些重置:http://cssreset.com/並選擇一個最適合你未來項目的。它會爲你節省很多麻煩:) –

+0

我同意@NikiMihaylov,雖然我會特別提出[normalize.css](https://necolas.github.io/normalize.css/)(這是默認使用的Bootstrap和Foundation框架)。 – hellojason

1

你忘了給元素添加一個點(或散列)。請務必將marginpaddinghtml & body設置爲0.請參閱下面的答案和清除的代碼。如果你想在頂部粘貼div,請在片段中添加position: fixed;top: 0; left: 0;

html,body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.usertopbar { 
 
    display: inline-block; 
 
    background: url(images/counterbar_bg.jpg) repeat-x #111; 
 
    height: 32px; 
 
    width: 1100px; 
 
    line-height: 30px; 
 
    text-align: left; 
 
    padding-left: 10px; 
 
    box-shadow: 0px 0px 16px #000; 
 
    border-bottom-right-radius: 8px; 
 
    border-bottom-left-radius: 8px; 
 
}
<div class="usertopbar"></div>