css - 如何使两个 div 的高度相同,而不管其内容如何?

标签 css height

我有一个与 1060 像素宽度对齐的中心。在此范围内,我还有两个 div,分别具有不同的背景颜色/图像和 260px 和 800px 的宽度。 现在我需要使两个子 div 的高度相同,而不管它们里面的内容如何。如果“child1”的内容很大,我们需要向下滚动浏览器才能看到它,而“child2”的内容较少,那么“child2”也应该扩展高度以与“child1”匹配。另一方面,如果“child1”和“child2”的内容都较少并且不产生浏览器滚动,那么两者都应该占据浏览器窗口的高度。下面给出了代码片段。

<html>
    <head>
    <style type="text/css">
        * { margin:0; padding:0; }
        .parent { clear:both; margin:auto; width:1060px; }
        .child1 { background-color:#999999; float:left; width:260px; }
        .child2 { background-color:#99CC00; float:left; width:800px; }
        .child1a, child2a { float:left; width:100%; }
        .child2a { border-bottom:1px solid #000000; height:500px; }
    </style>
    </head>

    <body>
    <div class="parent">
        <div class="child1">
            <div class="child1a">1</div>
            <div class="child1a">2</div>
            <div class="child1a">3</div>
        </div>
        <div class="child2">
            <div class="child2a">1</div>
            <div class="child2a">2</div>
            <div class="child2a">3</div>
        </div>
    </div>
    </body>
</html>

是否可以只使用 CSS 来解决,还是需要使用 JavaScript?

最佳答案

您可以使用 'display:table-cell' 。

http://jsfiddle.net/sWHKs/

关于css - 如何使两个 div 的高度相同,而不管其内容如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511860/

相关文章:

javascript - 如何制作 100% 宽度垂直居中对齐的 html5 视频

javascript - 如何使用javascript扩展和关闭导航栏移动

html - Firefox 边框半径不显示

html - CSS - 使文本保持在响应图像上的同一位置

html - 显示 flex,使图像与另一列高度相同

html - 浏览器设置多少高度?

html - 为什么这个 IFrame 没有占用完整的页面高度?

css - 表格、td、div heigth 100% 不起作用

html - 在CSS中将大图像的高度限制为小图像的高度

html - 如何在不影响其他行宽度的情况下使 colspan 工作