How to keep wrapped flex-items the same width as the elements on the previous row?

前端 未结 13 1011
一整个雨季
一整个雨季 2020-11-29 01:24

I have a

    that is a flex-box and a bunch of
  • s in it which are the flex-items.

    I am trying to get the

相关标签:
13条回答
  • 2020-11-29 01:43

    Alternative - grid

    its not the answer of what you want, but it nice to use:

    display: grid
    grid-template-columns: repeat(auto-fill, minmax(70px, 1fr))
    

    https://codepen.io/omergal/pen/povzJrz

    ul {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
    }
    
    li {
      min-width: 40px;
      max-width: 100px;
    }
    
    ul,
    li {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    ul {
      background-color: tomato;
    }
    
    li {
      margin: 0.5em;
      background-color: darkgreen;
    }
    
    img {
      width: 100%;
      opacity: 0.5;
    }
    figure,
    img {
      margin: 0;
      padding: 0;
    }
    <ul>
          <li>
            <figure>
              <img src="http://placekitten.com/g/250/250">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/101/101">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/201/201">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/150/150">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/80/80">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/111/111">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/40/40">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/110/110">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/75/75">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/89/89">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/150/150">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/32/32">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/61/61">
            </figure>
          </li>
          <li>
            <figure>
              <img src="http://placekitten.com/g/320/320">
            </figure>
          </li>
        </ul>

    0 讨论(0)
  • 2020-11-29 01:47

    Its not working very well, but you can achieve something in this direction with columns.

    column-gap: 10px;
    column-width: 150px;
    

    https://codepen.io/hobbeshunter/pen/OgByNX

    0 讨论(0)
  • 2020-11-29 01:48

    I have been experimenting with your codepen and I set flex: 0 1 10%

    Of course you can set the flex-basis whatever you want, I just wanted to prove the point, that if you set flex basis and allow it to shrink, it will behave much nicer in this case.

    I think this is what you needed. Here's the preview: http://codepen.io/anon/pen/bwqNEZ

    Cheers

    0 讨论(0)
  • 2020-11-29 01:48

    My solution is not ideal, as it leans on JQuery: http://codepen.io/BigWillie/pen/WwyEXX

    CSS

    ul {
      display: flex;
      flex-wrap: wrap;
    }
    
    li {
      min-width: 40px;
      max-width: 100px;
      flex: 1;
    }
    

    JavaScript

    var eqRowFlexItem = function(elem) {
        var w;
        var $elem = $(elem);
        // Clear out max-width on elements
        $elem.css('max-width', '');
        w = $(elem).eq(0).width();
        $(elem).css('max-width', w);
    }
    
    eqRowFlexItem('li');
    
    $(window).resize(function() {
        eqRowFlexItem('li');
    });
    

    I'm not entirely sure if it answers the problem. I found this post, as the title matched the problem I was having. I was after an effect whereby I would have a greater number of elements per row on larger screens - and fewer elements per row on smaller screens. In both cases, these elements also needed to scale to fit... across devices.

    However, those stray elements on the last row would always expand to fill the remaining space.

    The JQuery gets the width of the first element - and applies it as a max-width to every element. The OP wanted the blocks to fall within a min-width / max-width rage. Setting the max-width in CSS seems to work. We clear the max-width off the element, so it defaults to the max-width in the CSS... then gets the actual width.

    0 讨论(0)
  • 2020-11-29 01:50

    For Stylus you can use this mixin: https://codepen.io/anon/pen/Epjwjq

    
       flex-wrap-fix($basis, $max) {
         flex-grow: 1
         flex-basis: $basis
         max-width: 100%
    
         $multiplier = ceil($max / $basis)
    
         for i in (1..$multiplier) {
           @media(min-width: ($basis * i)) {
             max-width: percentage(1/i)
           }
         }
       }
    
    0 讨论(0)
  • 2020-11-29 01:52

    This isn't perfect but worth try. http://codepen.io/anon/pen/MKmXpV

    The changes are

    li
      width: 7.1428%
      flex: 0 0 auto
    
    figure
      margin: 0 auto
      background-color: darkgreen
      min-width: 40px
      max-width: 100px
    
    • changed li flex to "0 0 auto" and the li width to a percentage based on the number of lis.
    • moved the min and max widths from the li to the figure
    • moved the green background to the figure and centered the figures in the lis
    0 讨论(0)
提交回复
热议问题