【发布时间】:2015-04-24 20:50:57
【问题描述】:
在以下场景中,我不明白为什么元素 wrapper 和 content 的高度设置不正确,即使它们设置为 height: auto,这意味着没有显示带有类 wrap 的 2 个 div在包装器和内容 div 中。
我在这个 JSfiddle 中重现了这个问题:
正如您所见,两个橙色 div 并未显示在包装 div 内,即使包装高度设置为自动。是什么导致了这个问题,我该如何解决?
HTML 代码:
<div id="wrapper">
<div id="content">
<div id="top">
</div>
<div class="dash"></div>
<p id="header">Header</p>
<div class="wrap">
</div>
<div class="wrap">
</div>
</div>
</div
CSS 代码:
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#wrapper {
background-color: black;
margin-top: 2%;
width: 100%;
height: auto;
}
#content {
background-color: green;
width: 1224px;
height: auto;
margin: auto;
text-align: center;
}
#top {
background-color: pink;
height: 400px;
width: 60%;
margin: auto;
}
.dash {
width: 80%;
margin: auto;
margin-bottom: 1%;
height: 2px;
background-color: black;
}
p#header {
margin: 0;
text-align: center;
}
.wrap {
background-color: orange;
margin: 1%;
float:left;
width: 48%;
height: 400px;
}
【问题讨论】:
-
啊,
float和clearfix的魔力!您可能有兴趣阅读其他问题:stackoverflow.com/questions/8554043/what-is-clearfix -
您不需要将高度设置为 auto 这是默认值....作为@Boldewyn,您只需要学习如何使用浮动和清除。