I need to center a flex container (green, dashed outline). The flex parent should even be centered, when the flex items are wrapping and leaving whitespace like on the right side of the screenshot.
The issue is that display: flex
takes all available width, while we would need it to take minimum-width with the most flex-items in the first row.
You can find a jsfiddle here: https://jsfiddle.net/3qeodz6a/7/
body {
margin-left: 32px;
margin-right: 32px;
display: flex;
justify-content: center;
}
.container {
outline: 1px solid red;
padding: 10px;
// We want a maximum of 4 columns
max-width: calc(120px * 4 + 32px * 3)
}
.headline {
outline: 1px solid blue;
}
.modules {
outline: 1px dashed green;
display: flex;
flex-wrap: wrap;
gap: 32px;
}
.m {
width: 120px;
height: 40px;
outline: 1px solid aqua;
}
<div class="container">
<div class="headline">
Headline
</div>
<div class="modules">
<div class="m">
Module 1
</div>
<div class="m">
Module 2
</div>
<div class="m">
Module 3
</div>
<div class="m">
Module 4
</div>
<div class="m">
Module 5
</div>
<div class="m">
Module 6
</div>
</div>
</div>