Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center horizontally div inside parent div

Tags:

html

css

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>
like image 387
Eagle Avatar asked Oct 18 '22 05:10

Eagle


1 Answers

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;
}
like image 58
Mark Embling Avatar answered Oct 21 '22 01:10

Mark Embling