Centering div element inside html header tag [closed]

孤者浪人 提交于 2019-12-11 03:28:10

问题


I am trying to center my div element with class name toolbar and i have tried using the inline-block property on the div element and text-align: center on the header tag with no luck.

Here is the html

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

and the corresponding css stylings

 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;
}

Having no luck centering the inner div element, with the current settings it moves it all the way to the right and if i remove the display on the div, it brings it to the left


回答1:


remove position: fixed and top: 0 as it is not needed ....

and change to display:block from the .toolbar class.

Here's a quick sample:

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

if you don't want to remove position: fixed then:

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



来源:https://stackoverflow.com/questions/14062991/centering-div-element-inside-html-header-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!