【问题标题】:Align multiple unordered list (UL) columns horizontally水平对齐多个无序列表 (UL) 列
【发布时间】:2019-06-13 01:22:09
【问题描述】:

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

我正在使用 flexbox 水平对齐它们,但在列相互对齐时遇到了困难。

当前问题: 期望结果:

编辑:需要注意的是,列标题类是可选的,可以删除,因此该项目只是一个普通的列表项。

当列标题被激活时,我需要列标题将列标题 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>

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

【问题讨论】:

  • 编辑:需要注意的是,列标题类是可选的,可以删除,这样该项目就只是一个普通的列表项目。
  • 你描述的是一个表格。
  • 这很可能是一张我应该拥有的表格 我将探索该解决方案 @ksav 您的分析非常有帮助!
  • 你是怎么做到的?
  • @Ksav 最终将列拆分为自己的 div,这成为最明显的解决方案

标签: html css flexbox


【解决方案1】:

考虑多种选择,例如表格、网格等...,但如果您想继续使用 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;这样宽度也一样

【讨论】:

  • 这个答案成为我用例的最佳解决方案,但其他解决方案也很好,但最终与我试图解决的问题不匹配
【解决方案2】:

如果您可以调整标记,我会将column-header 移出列表。这样你就可以在同一个父 flexbox 中伸缩 header 和列表。

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

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

【讨论】:

  • 我会审核感谢您的快速回复@ksav
  • 标题是动态的,它可以是列标题或只是另一个列表项,因此它试图处理这两种情况
  • 这没有多大意义。
【解决方案3】:

只需将高度添加到您的.column-header

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

这将正确对齐

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

【讨论】:

  • 不确定这是否适用于此用例,因为列中的内容是动态的并且可以是任意长度
  • @Vickel 我认为只要屏幕足够宽,您就希望水平对齐,如果屏幕较小,则列应堆叠在另一列之下。
  • 高度只与标题有关,与列内容高度无关。
【解决方案4】:

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

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>

【讨论】:

  • 感谢您的建议;在这种情况下我不能使用 JS,但希望这可以帮助其他人。又来了
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多