【发布时间】:2011-04-25 19:50:39
【问题描述】:
如何创建两个相同高度的盒子(并排浮动)。 我想创建高度为容器/窗口 40% 的框?
【问题讨论】:
如何创建两个相同高度的盒子(并排浮动)。 我想创建高度为容器/窗口 40% 的框?
【问题讨论】:
使用 % 作为高度是相对于父容器的高度而言的。因此,您需要声明父容器的高度。看看这个教程:Equal Height Columns。
【讨论】:
如果这是您要找的,这里还有更多:
CSS:
#parent{
width:205px;
height:200px;
border:1px solid #000000;
overflow:auto;
}
#child1{
height:40%;
background:#00ff00;
float:left;
}
#child2{
height:40%;
background:#0000ff;
float:left;
}
要点:
float:left 用于并排对齐两个框height 在 % 中为两个子框指定,以便它们从父框继承。HTML:
<div id="parent">
<div id="child1">
This is first box
</div>
<div id="child2">
This is second box
</div>
</div>
【讨论】:
这对您来说应该是一个简单的解决方案。这是我的例子:
HTML:
<div class="wrap">
<div class="left">
Content
</div>
<div class="right">
More content
</div>
</div>
CSS:
.wrap
{
width: 400px;
height: 500px;
border: 1px solid #000;
}
.left, .right
{
float: left;
width: 45%;a
height: 40%;
margin: 2%;
}
.left
{
border: 1px solid #f00;
}
.right
{
border: 1px solid #00f;
}
【讨论】:
这个问题特别提到了浮动,对此有几个很好的答案,但我认为可能值得发布an answer that doesn't use floats,以防意外提及浮动:
.wrapper {
width: 400px;
height: 400px;
outline: 1px solid #000;
}
.wrapper div {
display: inline-block;
width: 198px;
height: 40%;
background: #66c;
}
.wrapper div:first-child {
background: #6c6;
}
<div class="wrapper">
<div>This is the first box</div>
<div>This is the second box</div>
<p>Some other content</p>
</div>
它目前在 WebKit 中不起作用,但我认为这是一个错误并且会有解决方法,我正在调查。如果您需要它在 IE
<!--[if lt IE 8]>
<style>
.wrapper div { zoom:1; *display:inline;}
</style>
<![endif]-->
【讨论】: