我有两个div元素并排。我希望它们的高度是相同的,并保持不变,如果其中一个调整大小。如果其中一个增长是因为放入了文本,那么另一个也应该增长以匹配高度。不过我还是搞不懂这个。什么好主意吗?

<div style="overflow: hidden"> <div style=" border: 1px solid #cccccc; float: left; padding-bottom: 1000px; margin-bottom: -1000px; "> Some content!<br /> Some content!<br /> Some content!<br /> Some content!<br /> Some content!<br /> </div> <div style=" border: 1px solid #cccccc; float: left; padding-bottom: 1000px; margin-bottom: -1000px; "> Some content! </div> </div>


当前回答

你可以使用Jquery的等高插件来完成,这个插件使所有的div的高度完全相同。如果其中一个生长了,其他的也会生长。

下面是一个实现示例

Usage: $(object).equalHeights([minHeight], [maxHeight]);

Example 1: $(".cols").equalHeights(); 
           Sets all columns to the same height.

Example 2: $(".cols").equalHeights(400); 
           Sets all cols to at least 400px tall.

Example 3: $(".cols").equalHeights(100,300); 
           Cols are at least 100 but no more than 300 pixels tall. Elements with too much content will gain a scrollbar.

这是链接

http://www.cssnewbie.com/equalheights-jquery-plugin/

其他回答

Flexbox

对于flexbox,它是一个单一的声明:

.row { 显示:flex;/*儿童身高相等*/ } .col { flex: 1;/*另外,等于宽度*/ 填充:1 em; 边界:固体; } < div class = "行" > <div class="col">我与你同在,我与你同在</div> <div class="col">我爱你,我爱你,我爱你。</div> .</div> .</div> .</div> < / div >

旧浏览器可能需要前缀,请参阅浏览器支持。

你可以使用伪列。

基本上,它使用包含DIV的背景图像来模拟两个等高DIV。使用这种技术还允许您添加阴影,圆角,自定义边界或其他时髦的模式到您的容器。

不过只适用于固定宽度的盒子。

经过良好的测试,在每个浏览器都能正常工作。

我有同样的问题,所以我创建了这个小函数使用jquery jquery是每个web应用程序的一部分。

function fEqualizeHeight(sSelector) {
    var sObjects = $(sSelector);

    var iCount = sObjects.length;

    var iHeights = [];

    if (iCount > 0) {
        $(sObjects).each(function () {
            var sHeight = $(this).css('height');
            var iHeight = parseInt(sHeight.replace(/px/i,''));
            iHeights.push(iHeight);
        });

        iHeights.sort(function (a, b) {
            return a - b
        });

        var iMaxHeight = iHeights.pop();

        $(sSelector).each(function () {
            $(this).css({
                'height': iMaxHeight + 'px'
            });
        });
    }
}

您可以在页面就绪事件时调用此函数

$(document).ready(function(){
   fEqualizeHeight('.columns');
});

我希望这对你有用。

你可以使用Jquery的等高插件来完成,这个插件使所有的div的高度完全相同。如果其中一个生长了,其他的也会生长。

下面是一个实现示例

Usage: $(object).equalHeights([minHeight], [maxHeight]);

Example 1: $(".cols").equalHeights(); 
           Sets all columns to the same height.

Example 2: $(".cols").equalHeights(400); 
           Sets all cols to at least 400px tall.

Example 3: $(".cols").equalHeights(100,300); 
           Cols are at least 100 but no more than 300 pixels tall. Elements with too much content will gain a scrollbar.

这是链接

http://www.cssnewbie.com/equalheights-jquery-plugin/

我很惊讶没有人提到(非常古老但可靠的)绝对列技术: http://24ways.org/2008/absolute-columns/

在我看来,它远远优于假列和一个真正的布局的技术。

一般的思想是一个具有位置的元素:绝对;将对最近的父元素进行定位,该父元素具有position: relative;然后通过赋值top: 0px;底部:0px;(或任何你实际需要的像素/百分比。)这里有一个例子:

<!DOCTYPE html>
<html>
  <head>
    <style>
      #container
      {
        position: relative;
      }

      #left-column
      {
        width: 50%;
        background-color: pink;
      }

      #right-column
      {
        position: absolute;
        top: 0px;
        right: 0px;
        bottom: 0px;
        width: 50%;
        background-color: teal;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left-column">
        <ul>
          <li>Foo</li>
          <li>Bar</li>
          <li>Baz</li>
        </ul>
      </div>
      <div id="right-column">
        Lorem ipsum
      </div>
    </div>
  </body>
</html>