I went through a couple of similar questions but the answers didn't work for me.
I have several divs, and each of them has a fixed width of 210px.
The container they're in can resize depending on the screen size of the user. The divs have to be evenly spaced horizontally at all time and it should also break the line of divs into another line if there is no space.
To clear things up more, refer the figure below.
This JS fiddle has achieved the outcome I want. But I can't see how it'll work for my divs which must have a fixed width.
width: calc(100% / 6);
EDIT:
The problem with the JS Fiddle is that when the screen size is where it has space, but not enough space to fit another div. In which case, the last div should be closer to the right.
The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.
Pure HTML: <div></div> Note that represents a single space, so keep adding them until you have enough.
Flexbox can do what you want by setting justify-content
to space-around
(or space-between
, depending on your presentational needs). Here's a quick example:
body{
display:flex;
flex-wrap:wrap;
justify-content:space-around;
margin:0;
padding:5px;
}
div{
background:#000;
flex:0 0 210px;
height:210px;
margin:5px;
width:210px;
}
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
Check caniuse.com for details on browser support and prefixing.
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