I have multiple smaller fix sized div floated inside a container div. I want them to be equally spaced so that the whole block gives the impression of being center aligned.
Right now they are aligned to the left (this is how float works, I know). I understand this is not readily possible, but just want some hacks so that I can achieve the same.
One way to do that is to calculate the necessary width (let say I need 3 divs per row, each 100px wide with 10px margin. So total width of container should be 100*3+60) and apply that width to the container div and set margin auto.
#container {
width: 360px;
margin: 0px auto;
}
.items { /* these are smaller divs */
width: 100px;
margin: 10px;
float: left;
}
But this won't look nice with different screen size unless I specifically set width for different medias.
Another way is to use the flex display.
#container {
display: flex;
flex-wrap:wrap;
justify-content: space-between;
}
.items { /* these are smaller divs */
flex: 0 0 auto;
width: 100px;
margin: 10px;
}
It almost served my purpose, but the issue is when you have more than one divs in the bottom row, it just put the second div at the rightmost side, rather than aligning under second column (again this is expected behavior I know, not any issue).
So, are there any tricks/hacks for what I want to achieve?
UPDATE (in response to the duplicate reporting): The solution of the other question (that is reported as same as this question) uses fixed container width whereas I mentioned above that I don't want to fix container width. So that solution was not the one I am looking for.
The solution that worked perfectly is mentioned by Josh below, with only change of justify-content: space-around so that contents remain centered even when the window is narrowed.