【问题标题】:How to make 3 "li" columns with variable height content the same height如何使具有可变高度内容的 3 个“li”列具有相同的高度
【发布时间】:2025-12-26 23:40:07
【问题描述】:

HTML:

<ul>
  <li>
    <div class="one">Variable Height Title</div>
    <div class="two">Fixed height middle block</div>
    <div class="three">Variable height middle block<br />more content<br /> more contentmore content<br /> more content<br /> more content</div>
    <div class="four">Fixed height footer</div>
  </li>
  <li>
    <div class="one">Variable Height Title Might be two lines long</div>
    <div class="two">Fixed height middle block</div>
    <div class="three">Variable height middle block</div>
    <div class="four">Fixed height footer</div>
  </li>
  <li>
    <div class="one">Variable Height Title</div>
    <div class="two">Fixed height middle block</div>
    <div class="three">Variable height middle block</div>
    <div class="four">Fixed height footer</div>
  </li>  
</ul>

CSS:

li {
  float:left;
  width:33%;
}

.one, .three {
  background-color:blue;
}
.two, .four {
  background-color:green;
}

请看这个例子:http://jsfiddle.net/WffHD/

有没有办法仅使用 css 使“一个” div 的高度相等(必须是动态的),然后还根据最高的一列使所有三列的高度相等?另一种说法:我希望所有“一个” div 的高度相同,然后通过拉伸“三个” div 的高度,所有列的高度也应该相同。不幸的是,由于我正在使用一个插件,它们必须保留为 li 项目。我认为这可以用 javascript 很容易地完成,但如果可能的话,我正在寻找一个 css 解决方案。 (警告,需要在 IE7 中工作)希望这是有道理的,谢谢!

【问题讨论】:

  • 开始认为仅使用 CSS 可能无法做到这一点,可能不得不求助于 javascript。
  • IE7?您可以尝试使用 IE 的 css 行为。
  • @biziclop:IE 中的行为实际上是后门 javascript。所以它真的不是“纯 css”。
  • 我们能否提供一种解决方案来保留 li 的但结构不同?

标签: html css internet-explorer-7


【解决方案1】:

难度很大,还是用 JavaScript。

这实际上是 Flex Box Layout 的设计目的之一。所以你会有这样的东西:

#mylist {
    display: flex;
    flex-flow: row wrap;
}
#mylist>li {
    flex: 1 1 100%;
}

并且它应该给所有元素相同的高度。请参阅full Flex Box Layout specification 了解更多选项。

只要确保你有合适的供应商前缀,你就可以开始了。

【讨论】:

  • 人们低估了 CSS3 表格!他们在保持结构的同时创建流畅的布局非常棒。
  • @Saju IE10+、Opera、Chrome。 Firefox 目前使用旧版本的 flexbox 规范,但标准版本应该会在下一个版本中提供。
【解决方案2】:

如果没有一些 HTML 更改,您需要的东西是不可能的。

一种可能的替代方法是,通过一些 HTML 更改,将元素作为表格来威胁,

  • 作为表格行和表格单元格。您需要将所有元素放在同一行 (li) 中,并且需要一个额外的 li 元素作为页脚

    例如:

    <ul>
        <li>
            <div class="one">Variable Height Title</div>
            <div class="one">Variable Height Title that Might be two lines long</div>
            <div class="one">Variable Height Title</div>
        </li>
        <li>
            <div class="two">Fixed height middle block</div>
            <div class="two">Fixed height middle block</div>
            <div class="two">Fixed height middle block</div>
        </li>
        <li>
            <div class="three">Variable height middle block<br />more content<br /> more contentmore content<br /> more content<br /> more content</div>
            <div class="three">Variable height middle block</div>
            <div class="three">Variable height middle block</div>
        </li>
        <li>
            <div class="four">Fixed height footer</div>
            <div class="four">Fixed height footer</div>
            <div class="four">Fixed height footer</div>
        </li>
    
    </ul>
    

    你可以看这里:http://jsfiddle.net/WffHD/25/

  • 【讨论】:

    • 好主意,但 HTML 可能是静态的。
    【解决方案3】:

    对于 IE7?

    还有纯 CSS?

    并且所有第 1 行(Div “one”)等高?

    所有列都等高?

    答案是……不可能。

    【讨论】:

    • 我感觉有人会否决这个;然而,鉴于各种参数,它提供了唯一真正的答案。研究 IE7 新出现时“等高”列的历史,并将其与希望“一个”div 跨列匹配高度相结合,全部使用纯 CSS,您没有解决方案的可能性。
    • 可能更多的是评论而不是答案,但我当然同意这一点。
    • @Wex:是的,有些人会将其归类为评论。然而,与此同时,当这是对所提出问题的“答案”(给定的条件)时,那么(在我看来)它应该得到比评论更“有力”的东西。重要的是,目前还没有人在回答中遇到所有这些问题。
    【解决方案4】:

    编辑后。而不是使用高度,只需使用垂直对齐。 (在 css 表中工作)

    ul { display: table; } /* Behave as table */
    ul > li { display: table-cell; vertical-align: bottom; }
    

    http://jsfiddle.net/wC4RF/4/

    【讨论】:

    • 这是无效的,因为你需要有table-row级元素。
    • 它不是无效的。表格行是可选的。您会看到它适用于所有浏览器并符合 W3C 标准。 W3C 将不带表格行的表格单元格指定为匿名内联框。
    • 我喜欢这个解决方案的简单性,但我将它插入到我的 jsfiddle 示例中,它似乎没有任何效果。
    • 没有看到结果,你能链接到一个有效的 jsfiddle 例子吗?
    • 您已成功使li 的高度相等。在我看来,问题在于 lia 是“列”,而 divs 单元格
    【解决方案5】:

    这个问题有两个方面需要注意:
    1- li's
    的高度相等 2- 第一个 div 的高度相同

    这是第一步:
    有一个简单但诡计多端的纯 CSS 解决方案,它在 IE6+ 中有效且效果很好(我之前已经测试过),但需要一些勇敢和标记。
    第一:

    ul {
       display: block;
       overflow: hidden;
    }
    ul li {
       float: left;
       margin-bottom: -99999px;
       padding-bottom: 99999px;
    }  
    

    很简单,我们强制列的高度非常高,然后用隐藏的溢出将它们切断,然后我们用大量的底部填充强制列更高,然后我们再次降低高度包装器以等量的负底部边距备份。这给了我们我们需要的效果。
    而对于第二部分,我唯一能实现的方法就是将它们从结构中切断,并将它们放在另一个结构中,就像上面提到的那样,所以用一点 CSS 魔法,它们似乎都是只有一种结构(假设您不想为此使用 js)。
    希望对您有所帮助。

    【讨论】:

    • 有趣,但不明白,你能提供一个活生生的例子吗?
    • 抱歉回复晚了,这是example
    • 谢谢!这技术真的很棒,一定会用它
    【解决方案6】:

    从 William G. Rivera 的答案中获取版本,以便在包括 IE7 在内的所有浏览器中保持一致的外观,display: table; 免费。这有点作弊,并且不会使div的高度相等,但在视觉上这是相同的

    HTML

    <ul>
      <li class="one">
        <div class="one">Variable Height Title</div>
        <div class="one">Variable Height Title that Might be two lines longgggggg</div>
        <div class="one">Variable Height Title</div>
        <div class="clear"></div>
      </li>
      <li class="two">
        <div class="two">Fixed height middle block</div>
        <div class="two">Fixed height middle block</div>
        <div class="two">Fixed height middle block</div>
        <div class="clear"></div>
      </li>
      <li class="three">
        <div class="three">Variable height middle block<br />more content<br /> more contentmore content<br /> more content<br /> more content</div>
        <div class="three">Variable height middle block</div>
        <div class="three">Variable height middle block</div>
        <div class="clear"></div>
      </li>
      <li class="four">
        <div class="four">Fixed height footer</div>
        <div class="four">Fixed height footer</div>
        <div class="four">Fixed height footer</div>
        <div class="clear"></div>
      </li>
    </ul>
    

    CSS

    div {
      float: left;
      width:33%;
    }
    
    .one, .three {
      background-color:blue;
    }
    .two, .four {
      background-color:green;
    }
    
    .clear {
        width: 100%;
        clear: both;
        float: none;
        height: 0;
    }
    

    http://jsfiddle.net/YWtSe/2/

    【讨论】:

      【解决方案7】:

      对于 jQuery 解决方案(这可能只重写为 javascript):

      var liPerRow = 3; // The amount of li items on each row
      var maxHeight = 0;
      
      for (var i = 0; i < $("ul").children().length; i++) {
          if (i % liPerRow == 0) {
              // split the list in the li items for just this 'row'
              var items = $($("ul").children().splice(i, liPerRow));
              // get the highest Height value in this list
              maxHeight = Math.max.apply(null, items.map(function () {
                  return $(this).height();
              }).get());
      
              items.height(maxHeight);
          }
      }
      

      【讨论】:

        【解决方案8】:

        为了使 user1797792 的解决方案更进一步,您需要清除元素上的显式内联高度声明,以便能够在响应式布局中重用该函数。在这种情况下,我在名为“prods”的父 ul 中调整 li 元素的大小。

        function resizeProds(){
            for (var i = 0; i < $("#prods").children().length; i++) {
                if (i % liPerRow == 0) {
                    // split the list in the li items for just this 'row'
                    var items = $($("#prods").children().splice(i, liPerRow));
                    items.height('');
                    // get the highest Height value in this list
                    maxHeight = Math.max.apply(null, items.map(function () {
                        return $(this).height();
                    }).get());
                    items.height(maxHeight);
                }
            }
        }
        

        【讨论】:

          【解决方案9】:

          看看马修詹姆斯泰勒的this post。用 CSS 做这件事有点复杂。但这是唯一的“好”方式。您还可以使用 javascript 并计算最高列并将此高度应用于其他两个。

          你需要几个这样的容器:

          <div id="container3">
            <div id="container2">
              <div id="container1">
                <div id="col1">Column 1</div>
                <div id="col2">Column 2</div>
                <div id="col3">Column 3</div>
              </div>
            </div>
          </div>
          

          比你需要重新定位每一列:

          #container3 {
              float:left;
              width:100%;
              background:green;
              overflow:hidden;
              position:relative;
          }
          #container2 {
              float:left;
              width:100%;
              background:yellow;
              position:relative;
              right:30%;
          }
          #container1 {
              float:left;
              width:100%;
              background:red;
              position:relative;
              right:40%;
          }
          #col1 {
              float:left;
              width:26%;
              position:relative;
              left:72%;
              overflow:hidden;
          }
          #col2 {
              float:left;
              width:36%;
              position:relative;
              left:76%;
              overflow:hidden;
          }
          #col3 {
              float:left;
              width:26%;
              position:relative;
              left:80%;
              overflow:hidden;
          }
          

          【讨论】:

          • 我对此投了反对票,因为您使用额外的 html 来解决问题,而在 CSS 中并不难。
          • 要获得真正等高的列,您需要额外的 html。表行只是无效。
          • 我已经阅读了 CSS W3C 文档。它是允许的,但被视为匿名框。
          • 确实如此。看看我在 cmets 中发布的 jsfiddle。看起来它不起作用,因为单元格没有背景。只是给他们一个背景......你看到的唯一的东西就是内容,这就是为什么你看不到效果