[html] Equal height rows in a flex container

As you can see, the list-items in the first row have same height. But items in the second row have different heights. I want all items to have a uniform height.

Is there any way to achieve this without giving fixed-height and only using flexbox?

enter image description here

Here is my code

_x000D_
_x000D_
.list {_x000D_
  display: flex;_x000D_
  flex-wrap: wrap;_x000D_
  max-width: 500px;_x000D_
}_x000D_
.list-item {_x000D_
  background-color: #ccc;_x000D_
  display: flex;_x000D_
  padding: 0.5em;_x000D_
  width: 25%;_x000D_
  margin-right: 1%;_x000D_
  margin-bottom: 20px;_x000D_
}_x000D_
.list-content {_x000D_
  width: 100%;_x000D_
}
_x000D_
<ul class="list">_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h2>box 1</h2>_x000D_
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h1>h1</h1>_x000D_
    </div>_x000D_
  </li>_x000D_
</ul>
_x000D_
_x000D_
_x000D_

This question is related to html css flexbox

The answer is


You can accomplish that now with display: grid:

_x000D_
_x000D_
.list {_x000D_
  display: grid;_x000D_
  overflow: hidden;_x000D_
  grid-template-columns: repeat(3, 1fr);_x000D_
  grid-auto-rows: 1fr;_x000D_
  grid-column-gap: 5px;_x000D_
  grid-row-gap: 5px;_x000D_
  max-width: 500px;_x000D_
}_x000D_
.list-item {_x000D_
  background-color: #ccc;_x000D_
  display: flex;_x000D_
  padding: 0.5em;_x000D_
  margin-bottom: 20px;_x000D_
}_x000D_
.list-content {_x000D_
  width: 100%;_x000D_
}
_x000D_
<ul class="list">_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h2>box 1</h2>_x000D_
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h3>box 2</h3>_x000D_
      <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h1>h1</h1>_x000D_
    </div>_x000D_
  </li>_x000D_
</ul>
_x000D_
_x000D_
_x000D_

Although the grid itself is not flexbox, it behaves very similar to a flexbox container, and the items inside the grid can be flex.

The grid layout is also very handy in the case you want responsive grids. That is, if you want the grid to have a different number of columns per row you can then just change grid-template-columns:

grid-template-columns: repeat(1, 1fr); // 1 column
grid-template-columns: repeat(2, 1fr); // 2 columns
grid-template-columns: repeat(3, 1fr); // 3 columns

and so on...

You can mix it with media queries and change according to the size of the page.

Sadly there is still no support for container queries / element queries in the browsers (out of the box) to make it work well with changing the number of columns according to the container size, not to the page size (this would be great to use with reusable webcomponents).

More information about the grid layout:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

Support of the Grid Layout accross browsers:

https://caniuse.com/#feat=css-grid


You can with flexbox:

_x000D_
_x000D_
ul.list {
    padding: 0;
    list-style: none;
    display: flex;
    align-items: stretch;
    justify-items: center;
    flex-wrap: wrap;
    justify-content: center;
}
li {
    width: 100px;
    padding: .5rem;
    border-radius: 1rem;
    background: yellow;
    margin: 0 5px;
}
_x000D_
<ul class="list">
  <li>title 1</li>
  <li>title 2<br>new line</li>
  <li>title 3<br>new<br>line</li>
</ul>
_x000D_
_x000D_
_x000D_


for same height you should chage your css "display" Properties. For more detail see given example.

_x000D_
_x000D_
.list{_x000D_
  display: table;_x000D_
  flex-wrap: wrap;_x000D_
  max-width: 500px;_x000D_
}_x000D_
_x000D_
.list-item{_x000D_
  background-color: #ccc;_x000D_
  display: table-cell;_x000D_
  padding: 0.5em;_x000D_
  width: 25%;_x000D_
  margin-right: 1%;_x000D_
  margin-bottom: 20px;_x000D_
}_x000D_
.list-content{_x000D_
  width: 100%;_x000D_
}
_x000D_
<ul class="list">_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h2>box 1</h2>_x000D_
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  _x000D_
    <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  _x000D_
    <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h1>h1</h1>_x000D_
    </div>_x000D_
  </li>
_x000D_
_x000D_
_x000D_


In your case height will be calculated automatically, so you have to provide the height
use this

.list-content{
  width: 100%;
  height:150px;
}

If you know the items you are mapping through, you can accomplish this by doing one row at a time. I know it's a workaround, but it works.

For me I had 4 items per row, so I broke it up into two rows of 4 with each row height: 50%, get rid of flex-grow, have <RowOne /> and <RowTwo /> in a <div> with flex-column. This will do the trick

<div class='flexbox flex-column height-100-percent'>
   <RowOne class='flex height-50-percent' />
   <RowTwo class='flex height-50-percent' />
</div>

you can do it by fixing the height of "P" tag or fixing the height of "list-item".

I have done by fixing the height of "P"

and overflow should be hidden.

_x000D_
_x000D_
.list{_x000D_
  display: flex;_x000D_
  flex-wrap: wrap;_x000D_
  max-width: 500px;_x000D_
}_x000D_
_x000D_
.list-item{_x000D_
  background-color: #ccc;_x000D_
  display: flex;_x000D_
  padding: 0.5em;_x000D_
  width: 25%;_x000D_
  margin-right: 1%;_x000D_
  margin-bottom: 20px;_x000D_
}_x000D_
.list-content{_x000D_
  width: 100%;_x000D_
}_x000D_
p{height:100px;overflow:hidden;}
_x000D_
<ul class="list">_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h2>box 1</h2>_x000D_
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  _x000D_
    <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  _x000D_
    <li class="list-item">_x000D_
    <div class="list-content">_x000D_
    <h3>box 2</h3>_x000D_
    <p>Lorem ipsum dolor</p>_x000D_
    </div>_x000D_
  </li>_x000D_
  <li class="list-item">_x000D_
    <div class="list-content">_x000D_
      <h1>h1</h1>_x000D_
    </div>_x000D_
  </li>_x000D_
</ul>
_x000D_
_x000D_
_x000D_


This seems to be working in cases with fixed parent height (tested in Chrome and Firefox):

.child {
        height    : 100%;
        overflow  : hidden;
}

.parent {
        display: flex;
        flex-direction: column;
        height: 70vh; // ! won't work unless parent container height is set
        position: relative;
}

If it's not possible to use grids for some reason, maybe it's the solution.


No, you can't achieve that without setting a fixed height (or using a script).


Here are 2 answers of mine, showing how to use a script to achieve something like that:


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to flexbox

Force flex item to span full row width vuetify center items into v-flex Equal height rows in CSS Grid Layout display: flex not working on Internet Explorer Center the content inside a column in Bootstrap 4 Vertical Align Center in Bootstrap 4 How to use zIndex in react-native Bootstrap 4 align navbar items to the right Does 'position: absolute' conflict with Flexbox? Make flex items take content width, not width of parent container