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/
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With