Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive div's floating and centered

Now I have put float:left on my green divs but that makes them stick to the left (makes sense). Now how can I keep my divs centered while I resize the blue container?

As in this image:

enter image description here

Blue: Parent container Green: Divs

like image 327
Dieter Avatar asked Feb 18 '26 17:02

Dieter


1 Answers

You can use display: inline-block on your child items and text-align: center on parent item

<div class="container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
.container {
    background-color: blue;
    width:500px;
    text-align: center;
}

.item {
    display: inline-block;
    background-color: green;
    width: 100px;
    height: 100px;
}

https://jsfiddle.net/g3vp2fyf/

like image 147
Krab Avatar answered Feb 20 '26 07:02

Krab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!