Flex-box: Align last row to grid

后端 未结 29 3088
不知归路
不知归路 2020-11-22 02:54

I have a simple flex-box layout with a container like:

.grid {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

29条回答
  •  清歌不尽
    2020-11-22 03:16

    Oh boy, I think I found a good solution with minimal CSS and no JS. Check it out:

    img {width:100%;}
    li {
      display: inline-block;
      width:8em;
      list-style:none;
    }
    ul {text-align: justify;}

    The key here is to remember that what we are trying to achieve is exactly what text-align: justify does!

    The empty elements in the HTML are there to make the last row display perfectly without changing the appearance, but might not be needed given what you are trying to achieve. For perfect balance in every situation, you need at least x-4 empty elements, x being the number of elements to display, or n-2, n being the number of column you want to display.

提交回复
热议问题