水平对齐多个无序列表(UL)列

问题描述 投票:0回答:4

我有多个无序列表,具有不同数量的元素,并且列标题中包含不同数量的文本。

我正在使用flexbox将它们水平对齐,但是在使列彼此对齐时很挣扎。

当前问题:Current Problem预期结果:Desired Outcome

编辑:注意:列标题类是可选的,可以删除,以便该项目只是普通列表项。

当激活列标题时,我需要列标题以使列标题li的高度与最多的文本匹配。我让flexbox可以水平对齐列表,但各列没有对齐。

这里是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>

无论该列中有多少文本,我都希望看到无序列表列标题居中并水平排列。帮助!

html css css3 flexbox
4个回答
2
投票

[考虑多个选项,例如表,网格等。但是如果您想使用flex但又不想将其限制为固定的列数,那可能会很好。

[如果可以重新排列HTML,以使标头位于具有display flex的单独容器中,并且列表也位于具有display flex的容器中,这将确保所有标头具有相同的高度,例如:

.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>

请记住要加上flex-grow: 1;flex-basis: 1px;,以使宽度也相同


5
投票

如果您可以调整标记,则可以将column-header从列表中移出。这样,您可以在同一父级flexbox中显示页眉和列表。

注意:从语义上讲,列表的标题本身就是列表中的列表项并没有多大意义。

.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>

编辑

看起来您可能实际上正在寻找的是不起眼的HTML表。

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
投票

您可以使用这样的函数来找到最高的标头元素,并将其余元素设置为该大小。

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>

0
投票

只需将身高增加到.column-header

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

这将正确对齐

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

© www.soinside.com 2019 - 2024. All rights reserved.