Equal height sections within flexbox columns?

前端 未结 2 1008
不思量自难忘°
不思量自难忘° 2021-01-14 08:48

I\'m using flexbox columns for a pretty basic layout. There is a header, image and a list below. List must have a border and can have varying contents. Is it possible to hav

相关标签:
2条回答
  • 2021-01-14 09:30

    Since both columns are inside the same flex container, yes, the list boxes can be the same height (based on the tallest content) using only CSS.

    .columns {
      display: flex;
    }
    
    .col {  
      display: flex;          /* new */
      flex-direction: column; /* new */
      margin-left: 20px;
    }
    
    .list {
      flex: 1;                /* new */
      padding: 24px;
      border: 1px solid blue;
    }
    
    ul {
      margin: 0;
    }
    <div class="columns">
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
    </div>

    Here are a few more examples:

    jsFiddle demo

    .columns {
      display: flex;
    }
    
    .col {
      display: flex;           /* new */
      flex-direction: column;  /* new */
      margin-left: 20px;
    }
    
    .list {
      flex: 1;                 /* new */
      padding: 24px;
      border: 1px solid blue;
    }
    
    ul {
      margin: 0;
    }
    <div class="columns">
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
    </div>
    
    <br>
    
    <div class="columns">
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
    </div>
    
    <br>
    
    <div class="columns">
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>
            <li>List item</li>        
          </ul>
        </div>
      </div>
      <div class="col">
        <h3>Header</h3>
        <div class="list">
          <ul>
            <li>List item</li>
            <li>List item</li>
          </ul>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2021-01-14 09:36

    You should set .col as flex too

    .col{ display:flex; }

    and your class columns is better named row

    0 讨论(0)
提交回复
热议问题