tags:

views:

501

answers:

5

How do I center a div horizontally inside its parent div with CSS?

<div id='parent' style='width: 100%;'>
 <div id='child' style='width: 50px; height: 100px;'>Text</div>
</div>
+1  A: 
<div id='child' style='width: 50px; height: 100px; margin:0 auto;'>Text</div>
Soufiane Hassou
+3  A: 

I am assuming the parent div has no width or a wide width, and the child div has a smaller width. The following will set the margin for the top and bottom to zero, and the sides to automatically fit. This centers the div.

div#child {
    margin: 0 auto;
}
Splash
thanks for your explanations
Eagle
+1  A: 
<div id='parent' style='width: 100%;text-align:center;'>
 <div id='child' style='width:50px; height:100px;margin:0px auto;'>Text</div>
</div>
James
This solution supports IE 6, but don't forget to add `text-align:left;` to the #child div
Moak
+1  A: 

You can use the "auto" value for the left and right margins to let the browser distribute the available space equally at both sides of the inner div:

<div id='parent' style='width: 100%;'>
   <div id='child' style='width: 50px; height: 100px; margin-left: auto; margin-right: auto'>Text</div>
</div>
x4u
A: 

Here is the solution to horizontally center div

http://www.templatespoint.com/blog/2009/06/how-to-center-main-div/

Eswar