It appears that I have misunderstood something about flexbox and am struggling to resolve the issue correctly.
In my current setup, I have a flexbox container with four columns as its direct children. I want each .flexbox-item to take up 50% of the container's width when the browser window size reaches 992px or lower.
The challenge:
Everything seems to work fine without using the 'gap' property on the .flexbox-container. However, once I introduce gap: 1em, the percentages no longer function as expected. It seems like the gap value is affecting the overall calculation of widths for the .flex-item elements.
The query:
How can I ensure that each flexbox item indeed occupies 50% of the container's width when the browser viewport is 992px or smaller, even when the 'gap' property is used? While adjusting the 'width' property manually to 45% may work, it doesn't feel like the correct solution to me. I would appreciate guidance on the best approach to maintain accurate percentage sizing when utilizing the 'gap' property.
Below is the code snippet in question:
.flexbox-container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
gap: 1em;
}
.flexbox-item {
width: 25%;
background-color: lightgreen;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
}
@media screen and (max-width: 992px) {
.flexbox-item {
width: 50%;
}
}
<div class="flexbox-container">
<div class="flexbox-item">Item 1</div>
<div class="flexbox-item">Item 2</div>
<div class="flexbox-item">Item 3</div>
<div class="flexbox-item">Item 4</div>
</div>
You can view the output here: https://jsfiddle.net/Erasus/gxtvhuow/12/