Reputation: 5169
I've got a wrapper and some items within. I would like to place two items next to other in each row. I used flexbox
to solve this.
My wrapper has the property flex-wrap: wrap;
and my items have the property: flex: 0 0 45%;
. So every of my items use 50% of the row (included the margins). I use margin
to make a gap on the right side and a gap on the bottom between the items. This works almost as I expected.
Now my question: Is there a way to ignore the margin-right
on the second item in each row (where it wraps), so the second item is also aligned to the right side of the wrapper same as the left item is. At the moment, there is also a gap at the end of the row because of the margin-right
. I would like to remove this gap. Is this possible with pure CSS
? last-child
just removes the margin-right
on the last item, but this is clear why. Is there a way to remove it on the end of the row, where the items wrap? Hope this is clear enough.
Below my snippet:
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
border: 1px solid black;
width: 768px;
}
.wrapper__item {
flex: 0 0 45%;
width: 330px;
height: 160px;
margin: 0 36px 18px 0;
background-color: lightcoral;
}
<div class="wrapper">
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
</div>
Here is also a screenshot where I would like to remove the margin-right (see arrows):
Upvotes: 8
Views: 8270
Reputation: 3266
It would be simpler using gap
css property:
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
border: 1px solid black;
width: 600px;
justify-content: flex-start;
align-items: flex-start;
gap: 18px;
}
.wrapper__item {
flex: 1;
min-width: calc(300px - 18px);
height: 160px;
background-color: lightcoral;
}
<div class="wrapper">
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
</div>
It will work with important changes on item:
flex: 1
min-width
instead of width
and on wrapper:
gap: 18px
to create a gap between elementsYou can create a different gap between horizontal and vertical with: gap: HR VR
where HR is horizontal gap, and VR is vertical gap; i.e.:
gap: 10px 18px
or using column-gap
and/or row-gap
properties.
Upvotes: 1
Reputation: 12641
Use justify-content: space-between
and height
of wrapper with align-content
optionally
.wrapper {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
border: 1px solid black;
width: 768px;
height: 588px;
}
.wrapper__item {
width: 330px;
height: 160px;
background-color: lightcoral;
}
<div class="wrapper">
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
</div>
Upvotes: 0
Reputation: 16855
Remove margin-right
and use justify-content: space-between
and use width:calc(50%-18px)
. It will give you exactly the 36px gap between your items.
Stack Snippet
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
border: 1px solid black;
width: 768px;
justify-content: space-between;
}
.wrapper__item {
width: calc(50% - 18px);
height: 160px;
margin: 0 0 20px 0;
background-color: lightcoral;
}
<div class="wrapper">
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
</div>
Reference Link
Upvotes: 4
Reputation: 98
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
border: 1px solid black;
width: 768px;
}
.wrapper__item {
flex: 1 0 45%;
/* width: 330px; */
height: 160px;
margin: 0 36px 18px 0;
background-color: lightcoral;
}
.wrapper div:nth-child(2n) {
margin-right: 0;
}
<div class="wrapper">
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
<div class="wrapper__item">Item</div>
</div>
Remove width: 330px;
in .wrapper__item
selector.
Modify flex: 0 0 45%;
to flex: 1 0 45%;
in .wrapper__item
selector.
Add css code:
.wrapper div:nth-child(2n) {
margin-right: 0;
}
Hope helpful :).
Upvotes: 5