I've got an problem. I have the html code like this:
<div id="main_content" >
<div id="container">
</div>
</div>
The css like this:
#main_content {
top: 160px;
left: 160px;
width: 800px;
min-height: 500px;
height: auto;
background-color: #2185C5;
position: relative;
}
#container {
width: auto;
height: auto;
margin: 0 auto;
padding: 10px;
position: relative;
}
I suppose that the #container will be centered within #main_content. However, it is not. I just want to find out the reason and how to make it centered.
This does solve the problem very well (tested in all new browsers), where the parent div has class="parent" and the child div has id="kid"
That style centers both horizontally and vertically. Vertical center can only be done using complicated tricks--or by making the parent div function as a table-cell, which is one of the only elements in HTML that properly supports vertical alignment. Simply set the height of the kid, margin auto, and middle vertical alignment, and it will work. It's the easiest solution that I know.
You can center float div with this little code:
everyone said it but i guess it won't hurt saying again. you need to set the
width
to some value. here is something simpler to understand.http://jsfiddle.net/XUxEC/
It is because your width is set to auto. You have to specify the width for it to be visibly centered. Your #container spans the whole width of the #main_content. That's why it seems not centered,
try this one if this is the output you want:
You need to set the width of the container. (
auto
won't work)The CSS reference by MDN explains it all.
Check out these links
auto
value in a CSS property - StackOverflowUpdate - In action @ jsFiddle