【发布时间】:2016-02-05 21:24:53
【问题描述】:
我的问题是为什么在下面的 html/css 代码中“绿色”文本在绿色框之外。是水平定位,因为它应该在绿色框内,但垂直它位于红色框下方。以及为什么蓝色框垂直位于绿色框下方,而不是红色框下方:
HTML:
<div class="red">Red</div>
<div class="green">Green</div>
<div class="blue">Blue</div>
CSS:
.red {
width: 25%;
height: 50px;
float: left;
border-color: red;
border-width: 1px;
border-style: solid;
color: red;
}
.green {
width: 18%;
height: 40px;
border-color: green;
border-width: 1px;
border-style: solid;
text-align: center;
color: green;
}
.blue {
width: 20%;
height: 50px;
float: left;
border-color: blue;
border-width: 1px;
border-style: solid;
color: blue;
}
https://jsfiddle.net/3tk7Lwbj/1/
据我了解浮动描述,红色和蓝色框取自正常流程,所以应该忽略绿色框为什么要计算它们的位置。
【问题讨论】:
-
为
.green类添加clear: left,因为前一个div 的float正在影响其行为。