How do I limit height of some divs to other divs in the same parent div with display flex row?

Advertisements

I have a parent div with display:flex; flex-direction: row; flex-wrap: wrap;
Inside it there are child divs, lets call them a b c d

Each div has <ul></ul> with multiple items inside it.
So it’s something like this:

<style>
  .flex-container {
    display: flex;
    flex-direction: row;
  }

  .list-container {
    padding: 10px;
    border: 1px solid black;
  }

  .list-container h2 {
    margin-top: 0;
  }

  .list-container ul {
    margin-top: 0;
    margin-bottom: 0;
    padding-left: 20px;
  }
</style>

<div class="flex-container">
  <div class="list-container">
    <h2>List A</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </div>
  <div class="list-container">
    <h2>List B</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
      <li>Item 6</li>
      <li>Item 7</li>
      <li>Item 8</li>
      <li>Item 9</li>
      <li>Item 10</li>
      <li>Item 11</li>
      <li>Item 12</li>
      <li>Item 13</li>
      <li>Item 14</li>
      <li>Item 15</li>
    </ul>
  </div>
  <div class="list-container">
    <h2>List C</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
      <li>Item 6</li>
      <li>Item 7</li>
      <li>Item 8</li>
      <li>Item 9</li>
      <li>Item 10</li>
      <li>Item 11</li>
      <li>Item 12</li>
      <li>Item 13</li>
      <li>Item 14</li>
      <li>Item 15</li>
      <li>Item 16</li>
      <li>Item 17</li>
      <li>Item 18</li>
      <li>Item 19</li>
    </ul>
  </div>
  <div class="list-container">
    <h2>List D</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
    </ul>
  </div>
</div>

Divs b and c can take very huge amount of items, so I want to limit their height.

How do I limit b and c height to max height of all other divs, if they are bigger, making them scrollable in this case? I need a solution without a and d height fixed in css(something like height: 100px;) as some internet solutions suggest because they are dynamic and can change their height too

>Solution :

Assuming we’re allowed to add an extra div inside the height-limited divs, we can do something like this:

.flex-container {
  display: flex;
  flex-direction: row;
}

.list-container {
  padding: 10px;
  border: 1px solid black;
}

.list-container h2 {
  margin-top: 0;
}

.list-container ul {
  margin-top: 0;
  margin-bottom: 0;
  padding-left: 20px;
}

.flexible {
  display: flex;
  flex-direction: column;
}

.flexible .flexible-child {
  flex-basis: 0px;
  overflow: auto;
  flex-grow: 1;
}
<div class="flex-container">
  <div class="list-container">
    <h2>List A</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </div>
  <div class="list-container flexible">
    <div class="flexible-child">
      <h2>List B</h2>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
        <li>Item 7</li>
        <li>Item 8</li>
        <li>Item 9</li>
        <li>Item 10</li>
        <li>Item 11</li>
        <li>Item 12</li>
        <li>Item 13</li>
        <li>Item 14</li>
        <li>Item 15</li>
      </ul>
    </div>
  </div>
  <div class="list-container flexible">
    <div class="flexible-child">
      <h2>List C</h2>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
        <li>Item 7</li>
        <li>Item 8</li>
        <li>Item 9</li>
        <li>Item 10</li>
        <li>Item 11</li>
        <li>Item 12</li>
        <li>Item 13</li>
        <li>Item 14</li>
        <li>Item 15</li>
        <li>Item 16</li>
        <li>Item 17</li>
        <li>Item 18</li>
        <li>Item 19</li>
      </ul>
    </div>
  </div>
  <div class="list-container">
    <h2>List D</h2>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
    </ul>
  </div>
</div>

Leave a ReplyCancel reply