2012-12-28 47 views
2

我想中心我的div元素的類名稱toolbar和我已經試過在div元素上使用inline-block屬性,在標題標籤上使用text-align: center但沒有運氣。在html標題標記中居中的div元素

下面是HTML

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

和相應的CSS花式

header{ 
    width: 100%; 
    height: 51px !important; 
    position: fixed; 
    background-color: white; 
    margin: 0 auto; 
    box-shadow: 0 1px 3px rgba(0,0,0,0.25); 
    -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.25); 
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.25); 
    z-index:100; 
    top: 0; 
    text-align: center; 
} 

.toolbar { 
    background: #fff; 
    position: fixed; 
    z-index: 1000; 
    top: 0; 
    width: 910px; 
    margin: 0 auto; 
    padding: 10px 25px; 
    display: inline-block; 
} 

有沒有運氣中心內div元素,它它移動一直到右側和當前設置如果我刪除div上的顯示,它將它帶到左邊

+3

'.toolbar {左:50%;保證金左:-455px; ... etc}' – Shmiddty

+0

您遇到過哪些瀏覽器?這是你正在經歷的:http://jsfiddle.net/LQfJY/? –

+0

所有的瀏覽器,但Shmiddty的解決方案現在都很好。謝謝先生們 – Warz

回答

4

刪除position: fixedtop: 0,因爲它不需要 ....

並更改爲display:block。工具欄類。

下面是一個簡單示例:

http://jsfiddle.net/Riskbreaker/QePqL/

,如果你不想刪除position: fixed則:

http://jsfiddle.net/QePqL/1/

+0

我需要固定的位置,因爲工具欄有一些javascript,允許用戶滾動回頂端,所以我需要工具欄始終存在。否則你的解決方案就可以工 – Warz

+0

我看到** sadaf **爲你和我添加了答案:) http://jsfiddle.net/Riskbreaker/Zunhz/ – Riskbreaker