Looking to create a fluid responsive grid layout using flexbox, without the need for media queries. The number of elements in the grid can vary and each item should have a fixed, equal width with left alignment. The entire group should have equal left and right margins.
Here's an example of what I'm aiming for:
https://i.sstatic.net/z99HV.png
This is how I attempted to achieve it:
.container {
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
margin: auto;
}
.item {
height: 200px;
width: 200px;
background-color: purple;
padding: 10px;
margin: 10px;
}
<div class="container">
<div class="item">Flex item 1</div>
<div class="item">Flex item 2</div>
<div class="item">Flex item 3</div>
<div class="item">Flex item 4</div>
<div class="item">Flex item 5</div>
</div>
Unfortunately, this approach did not yield the desired results:
https://i.sstatic.net/XLJdk.png
I had hoped that by setting margin: auto
on the container, it would size itself to accommodate an optimal number of items per row.
While solutions like Bootstrap or Foundation would make this task easier, I am curious if it can be achieved using flexbox alone.