Flexible packaging edge

I am trying to add some spacing between divs with display: flexand flex-wrap: wrap.

The problem is that when I apply margin-rightto the second element, the line breaks. How to add a gap between elements without breaking them into two lines?

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>
Run code
+4
source share
3 answers

You set the foundation on 50%, and then when you add margin, it presses the next element, since it can no longer be side by side. You can let the elements grow and avoid the basics:

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

Jsfiddle demo

+3
source

width ( a min-width a min-height), : flex:1;, calc() .

3, : https://jsfiddle.net/ja6820vu/10/

* {
  box-sizing: border-box;
}

.collage {
  position: relative;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 32px;
}

.collage-item {
  min-height: 17.5vw;
  min-width: 30%;
  margin: 1em 1em 0 0;
  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: 1;
}
.collage-item:nth-child(1) {
  margin: 0;
  margin-bottom: 16px;
}
.collage-item:nth-child(2), .collage-item:nth-child(3) {
  margin: 0;
  height: 25vw;
}
.collage-item:nth-child(2) {
  margin: 0;
  margin-right: 16px;
}
.collage-item:nth-child(4) {
  margin: 0;
  margin-top: 16px;
}
.collage-item:nth-child(1), .collage-item:nth-child(4) {
  flex: 0 0 100%;
  height: 50vw;
}
.collage-item:nth-last-child(2) {
  margin: 1em 0 0 0;
  background:url(http://www.comedywildlifephoto.com/images/gallery/0/00000150_t.jpg);
  background-size: cover;
}

.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="collage-item"></div>
  <div class="collage-item"></div>
  <div class="collage-item"></div>
  <div class="btn">View all 11 photos</div>
</div>
+2

You have images of the second line with a width of 50%:

flex: 0 0 50%;

When you add a 16x horizontal margin, you exceed the total line width by forcing a wrapper:

50% + 50% + 16px = overflow

Try dividing the field space by the image width:

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

revised script

+1
source

Source: https://habr.com/ru/post/1662859/


All Articles