Search code examples
cssflexbox

Using margin on flex items


I was under the impression that a margin can be added to flex items/children, and flexbox should automatically take that into account and calculate the correct spacing between the items.

I can't seem to get this working as I would like though.

Fiddle here: https://jsfiddle.net/dba5ehcw/1/

.flex-item{
    border: 1px solid blue;
    box-sizing: border-box;
    height: 160px;
    width: 50%;
}

So each flex item at the moment is half the width of the container, and they flow nicely next to each other.

I would like to be able to add a margin of say, 1em to the flex-items in order to give them some breathing room, but in doing so, they become larger than the 50% and no longer stack next to each other on the same line because they are too wide.

Is there a way to use margin on the flex-items and have the flexbox container take this into account and adjust (decrease) their widths accordingly?


Solution

  • You need to do it with padding - which, when in border-box mode does not make the container larger than its specified width - not margin, and a nested flex div. This is how all flexbox-based grid systems work. Code below:

    .flex-container{
        border: 1px solid red;
        box-sizing: border-box;
        display: flex;
        flex-wrap: wrap;
        width: 320px;
    }
    
    .flex-item{
        padding:1em;
        box-sizing: border-box;
        height: 160px;
        width: 50%;
        display:flex;
    }
    
    .flex-item>div {
        border: 1px solid blue;
        flex: 1 1 auto;
    }
    <div class="flex-container">
        <div class="flex-item"><div></div></div>
        <div class="flex-item"><div></div></div>
        <div class="flex-item"><div></div></div>
        <div class="flex-item"><div></div></div>
        <div class="flex-item"><div></div></div>
        <div class="flex-item"><div></div></div>
    </div>