Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center child divs inside parent div

Tags:

html

css

I have a parent div that must stay at 100% with 3 child divs inside. I need to center the 3 child divs, but don't know how.

.parent {     width: 100%;         border: 1px solid blue; }      .child {     float: left;         border: 1px solid red;     margin: 2px; }
<div class="parent">     <div class="child">child1</div>     <div class="child">child2 - center us child divs! :)</div>     <div class="child">child3</div>     <div style="clear: both"></div> </div>

Here is a JSFIddle:

http://jsfiddle.net/qdHH3/2/

like image 953
flying227 Avatar asked Oct 26 '12 17:10

flying227


People also ask

How do you center an element within its parent element?

How to Center an Image Using Text Align: Center. An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it.

How do you center an image relative to the parent element?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

What does it mean to center a div?

Setting the width of the element will prevent it from stretching out to the edges of its container. The element will then take up the specified width, and the remaining space will be split equally between the two margins: This div element is centered.


1 Answers

Try using display: inline-block and text-align: center

.parent {     width: 100%;     border: 1px solid blue;     text-align: center; }  .child {     display: inline-block;       border: 1px solid red;     margin: 2px; } 

jsFiddle: http://jsfiddle.net/qdHH3/3/

like image 93
Christian Avatar answered Oct 14 '22 01:10

Christian