JT Nolan JT Nolan - 3 months ago 10
CSS Question

Why won't my Div center itself in its parent?

I am making a header bar for a website. This header bar consists of 3 parts: The Back Button, The School Information, and The Login Button. The problem is that the center div, the School Information, will not center itself. I've tried everything I know how to do and nothing works.

Here is my HTML and CSS:



/* -- Button Bar -- */

.buttonbarframe {
width: 100%;
background-color: #F1F1F1;
border-bottom: 1px solid #D5D5D5;
box-shadow: 1px 1px 2px #D5D5D5;
position: fixed;
left: 0;
right: 0;
top: 0;
/*height: 65px;*/
height: 45px;

padding: 10px 0px 10px 0px;
}

/*#buttonbar {
width: 100%;
height: 45px;
padding: 10px 0px 10px 0px;
}*/

.buttonbarspan {
height: 100%;
}

.buttonbarspanindex {
width: inherit;
height: 100%;
margin: 0px;
}

#buttonbarschoolimage {
display: block;
float: left;
vertical-align: middle;
height: 44px;
border: 1px solid #D5D5D5;
}

#buttonbarschoolname {
display: block;
padding: 0px;
margin-top: 4px;
text-align: left;
vertical-align: middle;
float: left;
}

.buttonbarrightbutton {
display: block;
float: right;
}

.buttonbarsycamorelogo {
margin: 0 auto;
display: block;
text-align: center;
vertical-align: middle;
height: 44px;
}

.schoolstuff {
display: inline-block;
margin: 0 auto;
text-align: center;
vertical-align: middle;
width: 341px;
}

.rightbutton {
float: right;
}

.leftbutton {
float: left;
}

<div class='buttonbarframe'>
<div class='leftbutton buttonbarspan'>
<a class='backbutton btn btn-large btn-danger' href=''><i class='icon-white icon-arrow-left'></i> Logout</a>
</div>
<div class='schoolstuff buttonbarspan'>
<img id='buttonbarschoolimage' src='/Schools/$EntityValue/logo'>
<h2 id='buttonbarschoolname'>Tri-County School</h2>
</div>
<div class='rightbutton buttonbarspan'>
<input type='submit' class='buttonbarrightbutton btn btn-large btn-primary' href='parentcheckin.php' value='Login'>
</div>
</div>





Here is an example of the problem.

Image of the problem.

Answer

Putting text-align: center on the parent div will fix this issue. What you have right now centers the text in the div which should already be centered.

see: http://jsfiddle.net/AJmSw/

Inline-block elements will be positioned based on the parent containers text align. With both your side divs floating this will work fine.

Alternatively removing the display: inline-block rule from the center div will allow the margin: 0 auto; to take effect also centering the div see: http://jsfiddle.net/b4mWU/

Comments