2

I've been working on an image gallery using flexbox with a flex-basis transition to grow and shrink the elements on hover. I am having a problem adapting the gallery to use flex-wrap to break to the next row after the 5th element.

The way I'm hoping for it to function is that each row has 5 elements that grow and shrink on the same row. After the a sixth element is placed in the container I want to break to the next row and have the elements continue functioning as expected (transitions included).

  • Elements stretch to fill available space (width of 20% for each of the 5 elements)
  • -> On hover, the other elements shrink 5% for a total of 20%
  • Hovered element grows to 40%
  • Sixth element that moves to next row inherits the size of the available space
  • Seventh element takes up 50% of row, eighth 33.3%...ect.

This is what I currently have: https://codepen.io/TommyBoyLab/pen/YdzGjB

(adapted from: https://codepen.io/joliveras/pen/GpLVKv)

HTML of element:

<div class="container">
  <div class="item" style="background:url() center/cover">

CSS:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
}
.container .item {
  display: grid;
  position: relative;
  flex: 1;
  transition: 500ms;
  min-width: 15%;
  max-width: 20%;
  height: 50vh;
}
.container .item:hover {
  transition: 500ms;
  max-width: 40%;
  flex-grow: 1;
}
.container .content {
  margin: auto;
  font-size: 1.5em;
}
Rob
  • 14,746
  • 28
  • 47
  • 65
Thomas F.
  • 31
  • 4

2 Answers2

1

It seems you only need to increase the value of flex-grow on hover:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
}
.container .item {
  display: grid;
  position: relative;
  flex: 1;
  transition: 500ms;
  min-width: 15%;
  max-width: 20%;
  height: 50vh;
  
}
.container .item:hover {
  transition: 500ms;
  max-width: 40%;
  flex-grow: 5;
}
.container .content {
  margin: auto;
  font-size: 1.5em;
}
<div class="container">
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>

</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Unfortunately the transitions for the top row stop functioning when that is done. I'm looking for a solution that will keep the transitions functioning for both rows and decidedly breaks the row after the 5th element. – Thomas F. Dec 10 '18 at 08:51
1

Addition to CSS:

.item:first-child:nth-last-child(n + 5),
.item:first-child:nth-last-child(n + 5) ~ *{
    min-width: 17%;

}
Thomas F.
  • 31
  • 4