Align multiple unordered list (UL) columns horizontally

时间:2019-01-18 18:36:38

标签: html css css3 flexbox

I have multiple unordered lists, with a different number of elements and with a column header that has a varied amount of text in it.

I am using a flexbox to horizontally align them but am struggling with the columns aligning with each other.

Current Problem: Current Problem Desired Outcome: Desired Outcome

Edit: A caveat is that the column header class is optional and can be removed so that the item is just a normal list item.

When the column header is activated, I need the column header to match the height of the column header li with the most text. I have the flexbox working to align the lists horizontally but the columns are not lining up.

Here is the JsFiddle: (https://jsfiddle.net/xk04m7g1/)

.column-stack {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 30px
}

.column-stack ul {
  -ms-flex: none;
  flex: none;
  width: calc(33.333% - 10px);
}

.column-stack ul .column-header {
  font-size: 12px;
  text-transform: uppercase;
  color: #006a4d;
  font-weight: 500;
  padding-bottom: 5px;
  border-bottom: 1px solid #3a3a3a;
  display: flex;
  align-items: stretch;
  /* Default */
  justify-content: space-between;
}
<div class="column-stack">
  <ul>
    <li class="column-header">Header 1 One Line</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
  <ul>
    <li class="column-header">Header 2 Many Lines of Text this is many lines of text and is the longest column wow it keeps going and going and going like the energizer rabbit</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
  <ul>
    <li class="column-header">Header 3 Two Line at most on most use cases</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
</div>

I expect to see the unordered list column headers to line up centered and horizontally no matter how much text is in that column. Help!

4 个答案:

答案 0 :(得分:5)

If you can adjust your markup, I would move the column-header out of the list. That way you can flex the header and the list within the same parent flexbox.

Note: Semantically speaking, it doesn't really make a lot of sense that the header of the list is itself a list-item within the list.

.column-stack {
  display: flex;
  justify-content: space-between;
}

.column {
  padding: 10px;
  flex: 1 1 33%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.column-stack .column-header {
  font-size: 12px;
  text-transform: uppercase;
  color: #006a4d;
  font-weight: 500;
  padding-bottom: 5px;
  border-bottom: 1px solid #3a3a3a;
  flex: 1;
  display: flex;
  justify-content: center;
  flex-direction: column;
  
}
<div class="column-stack">
  <div class="column">
    <h5 class="column-header">Header 1 One Line</h5>
    <ul class="column-list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>

  <div class="column">
    <h5 class="column-header">Header 2 Many Lines of Text this is many lines of text and is the longest column wow it keeps going and going and going like the energizer rabbit</h5>
    <ul class="column-list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>

  <div class="column">
    <h5 class="column-header">Header 3 Two Line at most on most use cases</h5>
    <ul class="column-list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>

</div>

Edit

It looks like what you might actually be looking for is the humble HTML table.

th {
  font-size: 12px;
  text-transform: uppercase;
  color: #006a4d;
  font-weight: 500;
  padding-bottom: 5px;
  border-bottom: 1px solid #3a3a3a;
}

td {
  width: 33%;
}
<table>
  <thead>
    <th>Header 1 One line</th>
    <th>Header 2 Many Lines of Text this is many lines of text and is the longest column wow it keeps going and going and going like the energizer rabbit</th>
    <th>Header 3 Two Line at most on most use cases</th>
  </thead>
  <tbody>
    <tr>
      <td>Item 1</td>
      <td>Item 1</td>
      <td>Item 1</td>
    </tr>
    <tr>
      <td>Item 2</td>
      <td>Item 2</td>
      <td>Item 2</td>
    </tr>
    <tr>
      <td>Item 3</td>
      <td>Item 3</td>
      <td>Item 3</td>
    </tr>
    <tr>
      <td>Item 4</td>
      <td>Item 4</td>
      <td>Item 4</td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:2)

Thinking of multiple options e.g. table, grid, etc..., but one that might be good if you want to stay with flex but don't want to be limited to a fixed amount of columns.

If you can rearrange your HTML so that the headers are in a separate container with display flex and the lists are also in a container with display flex that would make sure all headers are the same height, like:

    .column-stack {
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: justify;
        justify-content: space-between;
    }

    .column-stack ul {
        flex-grow: 1;
        flex-basis: 1px;
    }

.headers {
  display:flex;
  
}
 .column-header {
        font-size: 12px;
        text-transform: uppercase;
        color: #006a4d;
        font-weight: 500;
        padding-bottom: 5px;
        border-bottom: 1px solid #3a3a3a;
        display: flex;
        align-items: stretch; /* Default */
        justify-content: space-between;
        flex-grow: 1;
        flex-basis: 1px;
        margin: 10px;
 }
<div class="headers">
  <div class="column-header">Header 1 One Line</div>
  <div class="column-header">Header 2 Many Lines of Text this is many lines of text and is the longest column wow it keeps going and going and going like the energizer rabbit</div>
  <div class="column-header">Header 3 Two Line at most on most use cases</div>
</div>

<div class="column-stack">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</div>

just remember to add flex-grow: 1; and flex-basis: 1px; so that the width is the same as well

答案 2 :(得分:1)

You can use a function like this to find the tallest header element and set the rest to that size.

window.onload = resizeHeaders;

function resizeHeaders()
{
  var headers = document.getElementsByClassName("column-header");
  
  var maxHeight = 0;
  for(var i = 0; i < headers.length; i++)
  {
    if(maxHeight < headers[i].offsetHeight)
      maxHeight = headers[i].offsetHeight;
  }
  
  for(var i = 0; i < headers.length; i++)
  {
    headers[i].style.height = maxHeight+"px";
  }
}
.column-stack {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 30px
}

.column-stack ul {
  -ms-flex: none;
  flex: none;
  width: calc(33.333% - 10px);
}

.column-stack ul .column-header {
  font-size: 12px;
  text-transform: uppercase;
  color: #006a4d;
  font-weight: 500;
  padding-bottom: 5px;
  border-bottom: 1px solid #3a3a3a;
  display: flex;
  align-items: stretch;
  /* Default */
  justify-content: space-between;
}
<div class="column-stack">
  <ul>
    <li class="column-header">Header 1 One Line</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
  <ul>
    <li class="column-header">Header 2 Many Lines of Text this is many lines of text and is the longest column wow it keeps going and going and going like the energizer rabbit</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
  <ul>
    <li class="column-header">Header 3 Two Line at most on most use cases</li>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
</div>

答案 3 :(得分:0)

Just add height to your .column-header

.column-stack ul .column-header {
height:80px;
}

and this will align properly

https://jsfiddle.net/ot6qsu4w/20/