margins and flex-wrap in a flexbox

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


margins and flex-wrap in a flexbox



I have a problem with flex-wrap property of a flexbox.


flex-wrap



Here is my code:




.container {
display: flex;
flex-wrap: wrap;
}

.item {
width: 50%;
margin: 0 10px;
}


<div class="container">
<div class="item"></div>
<div class="item"></div>
</div>



What I want to do is to let some space margin between items but without wrapping them. I want them horizontally two by two and every item with width of 50% margin included to prevent flex-wrap.


margin


flex-wrap





so remove the wrap ?
– Temani Afif
yesterday




3 Answers
3




.container {
display: flex;
flex-wrap: wrap;
}

.item {
flex: 1 0 34%; /* fg, fs, fb */
margin: 10px;
height: 50px;
background-color: gray;
}


<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>



With flex-grow: 1 defined in the flex shorthand, there's no need for flex-basis (width) to be 50%, which results in one item per row due to the margins.


flex-grow: 1


flex


flex-basis


width



Since flex-grow will consume free space on the row, flex-basis only needs to be large enough to enforce a wrap.


flex-grow


flex-basis



In this case, with flex-basis: 34%, there's plenty of space for the margins, but not enough space for a third item on each line.


flex-basis: 34%





And here is an answer that actual uses flexbox itself to solve the problem ;), love the way we can use the flex-grow:1 along with flex-basis, saved me already some "trouble"
– dippas
yesterday




flex-grow:1


flex-basis





I would probably add more detail about the min value and the max value of the flex-basis we can use ... in this case we should choose something between calc(100%/3 - 20px) and calc(50% - 20px)
– Temani Afif
yesterday


calc(100%/3 - 20px)


calc(50% - 20px)



You can use calc for this, so for example you can set flex-basis: calc(50% - margin x 2).


calc


flex-basis: calc(50% - margin x 2)




.container {
display: flex;
flex-wrap: wrap;
}

.item {
flex-basis: calc(50% - 20px);
margin: 10px;
background: lightblue;
height: 50px;
}


<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>



You may use calc to exclude margin from the width:


calc




.container {
display: flex;
flex-wrap: wrap;
}

.item {
width: calc(50% - 20px);
margin: 5px 10px;
height:20px;
background:red;
}


<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

OQu7k,EZcO HHgPwVi TpbcAvdtl0,kZVwT2 GcX,GKJtNPRxz2Tz4xLvt a2HDQrP,QF rt2z8GqFR52IbcPZgqamWdpjfaJSN,LW,aTAwBe
gks6N,OQ0LXtJ0wkn91erqGXL1HV56JhTa8z VBb0MW YP4duxpXO,ewLQvq SZ4j1rRBAjZ40TND891 P

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

Using generate_series in ecto and passing a value