Make all LI to the height of the largest using CSS

Asked

Viewed 2,299 times

2

I have some elements <li> that I guide to stay inline-block. However, as the content of each <li> it’s different, they end up getting DIFFERENT HEIGHT. Kind of like this: inserir a descrição da imagem aqui

However, the correct thing is that the <li> with less content take the same height as the <li> bigger, and stay aligned, like this: inserir a descrição da imagem aqui

How to solve this?

  • 1

    place your css and html code

  • LI CSS: width: 200px; position: relative; height: 100%; border: 1px Solid #ccc; padding: 5px; text-align: center; display: inline-block;

  • As far as I know height: 100% only works if the upper parts have defined height.

  • put your code so we can help

  • Of all CSS-only lines I don’t know if it’s possible, but leaving the same size per line you can achieve using flex.

2 answers

5


Let ALL blocks of the same size using only CSS do not know if it is possible (unknown) since each line becomes independent, but each element of a line has a certain connection and can be used as parameter to keep all others (of the same line) of the same size.

The easiest way to achieve this result is by using Flex:

ul{
  list-style: none;
  padding: 0;margin: 0;
  display: flex;
  flex-wrap: wrap;
}

li{
  width: 25%;
  border: 5px solid black;
  margin: 5px;
  padding: 3px; 
}
<ul>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi rerum doloremque laborum? </li>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et quasi corporis sed fugit quibusdam, possimus ipsam animi ab molestias. Eos qui id sint mollitia amet officiis, commodi natus officia libero.</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
</ul>

the display: flex by default will already leave all elements with the same size, but you lose control over their width it will try to leave everything in the same line, disregarding the defined width or limits of the page or parent element, which can be solved using the flex-wrap, that allows you to break the line as soon as the elements pass the screen.

There is an alternative, but it does not solve 100%.

0

You can set the property min-height a value according to the content. Ex.: if your major li has 200px, set:

   li {
       min-height: 200px;
    }
  • Elements with a lot of content can go up to 200px in height, while elements with little content would be 200px. This would only solve the problem if the set height is certainly greater than the height of the largest element and that there is no guarantee.

  • The value of 200px was just one example. If it is for all elements to be equal, anyway elements with little content would be empty space. I believe that even with dynamic content, there is a limit to the element, otherwise it would not look good visually.

  • But if all the elements have little content, it will not be legal, because everyone will have the blank.

  • But that’s what the user wants, he wants the size of the <li> to be equal.

  • You want them to be the same height as the largest, but if none exceeds 200px, they will all be 200px, that is, the same height, but there is a blank space on the page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.