Responsive div, floating and centered

Now I 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?

Like this picture:

enter image description here

Blue: parent container Green: Divs

+3


source to share


2 answers


You can use display: inline-block

for children and text-align: center

parent

<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/

+7


source


Using

display: inline-block;
text-align: center;

      



instead

float: left;

      

+1


source







All Articles