【问题标题】:Making the height of a div depend on the height of another div?使div的高度取决于另一个div的高度?
【发布时间】:2014-06-22 19:59:32
【问题描述】:

我有一个“导航”div,它是一个浮动到页面左侧的链接列表。在它旁边的右边是一个“内容”的文本 div 也向左浮动。自然,它们的高度取决于里面的内容。

因此,只有 5 个链接的“导航”div 比多段“内容”div 短得多。 (这些是白色背景上的彩色框,这就是我尝试将它们排成一行的原因)

我想要的是使它们具有相同的高度,以便它们排成一列。当然我可以手动为它们指定一个像素高度,但是我希望这个 CSS 文件可以在多个 HTML 页面中使用,其中文本内容从 2-5 段不等,手动指定是行不通的。

【问题讨论】:

标签: html css


【解决方案1】:

请检查以下答案。

HTML

<div class="container clearfix">
<div class="sidebar">
    <ul>
        <li>link1</li>
        <li>link1</li>
        <li>link1</li>
        <li>link1</li>
    </ul>
</div>
<div class="content">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>

CSS

.container{border:1px red solid; position:relative;}
.sidebar {float:left;width:150px;border:1px green solid;position:absolute;top:0;bottom:0;left:0;}
.sidebar ul{list-style:none;padding:0;margin:0;}
.content{float:left;width:300px;border:1px orange solid;margin-left:160px;}
.clearfix:before, .clearfix:after { content: " "; display: table; }
.clearfix:after { clear: both; }
.clearfix { *zoom: 1;}

下面是我的代码的小提琴。你可以通过我刚才提到的一种方法来解决这个问题。

http://jsfiddle.net/murli2308/htZhG/

【讨论】:

  • 做得很好。为什么需要 .clearfix { *zoom: 1;}?
  • *缩放:1;这个有什么用。
  • 这是 IE7 的 hack。如果你在谷歌上搜索 clearfix,你就会知道它。有很多 clearfix 可用,我更喜欢使用它。
  • 你为什么不直接使用display: table-cell
  • 我们也可以使用它。我说有很多方法可以做到。我刚刚提到了一个。
猜你喜欢
  • 2012-12-03
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2011-03-17
  • 2018-03-03
相关资源
最近更新 更多