Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

margin with flex wrap

Tags:

html

css

flexbox

I am trying to add some spacing between the divs using display: flex and flex-wrap: wrap.

The problem is that when I apply margin-right to the second item, the row breaks. How can I add some spacing between the items without breaking them in 2 rows?

jsFiddle

* {
  box-sizing: border-box;
}
// Default
// ----------------------------------------------------
.collage {
  position: relative;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 32px;
}
.collage-item {
  width: 100%;
  height: 25vw;
  background: url("https://www.audi.co.uk/content/dam/audi/production/Models/NewModelsgallery/A5range/A5_Coupe/MY17/1920x1080_A5-Coupe-2016-side.jpg") no-repeat;
  background-size: cover;
  flex: 0 0 50%;
  &: nth-child(1) {
    margin-bottom: 16px;
  }
  &:nth-child(2) {
    margin-right: 16px;
  }
  &:nth-child(4) {
    margin-top: 16px;
  }
  &:nth-child(1),
  &:nth-child(4) {
    flex: 0 0 100%;
    height: 50vw;
  }
}
// Button
// ----------------------------------------------------
 .btn {
  position: absolute;
  border: 2px solid white;
  padding: 10px 18px;
  text-align: center;
  right: 16px;
  bottom: 16px;
  color: white;
  text-decoration: none;
}
<div class="collage">
  <div class="collage-item"></div>
  <div class="collage-item"></div>
  <div class="collage-item"></div>
  <div class="collage-item"></div>
  <div class="btn">View all 11 photos</div>
</div>
like image 502
brunodd Avatar asked Dec 05 '16 16:12

brunodd


People also ask

How do you add margins to flex-wrap?

You can add some margin-top to both flex items, and a negative margin-top of the same amount to the flex container. This way, the negative margin of the flex container will neutralize the margin of the flex items at the first line, but not the margin of the items that wrapped to other lines.

Does margin work with Flex?

Let's get flexible! If you apply auto margins to a flex item, that item will automatically extend its specified margin to occupy the extra space in the flex container, depending on the direction in which the auto-margin is applied.

How does flex-wrap work?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.


2 Answers

You are setting the basis to 50% then when you add the margin it push the next element since can't fit side by side anymore. You may want to let the items grow and avoid the basis:

.collage-item {
  flex: 1 0 auto;
}

Jsfiddle Demo

like image 86
DaniP Avatar answered Oct 11 '22 18:10

DaniP


You have both second row images set to 50% width:

flex: 0 0 50%;

When you add the 16px horizontal margin, you exceed the total width of the row, forcing a wrap:

50% + 50% + 16px = overflow

Try factoring the margin space into the width of the images:

flex: 0 0 calc(50% - 8px);

revised fiddle

like image 2
Michael Benjamin Avatar answered Oct 11 '22 19:10

Michael Benjamin