为什么左列“推”右列的内容?

时间:2015-05-07 17:28:56

标签: html css

我实现了一个固定的,单流体的列布局,我不知道为什么左列(关于高度)“推”我的部分内容。

.profile-left-column {
    width: 105px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    padding: 0 15px;
    float: left;
    height: 500px;
    background: #ddd;
}
.profile-right-column {
    margin-left: 105px;
    padding: 0 15px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.container {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    max-width: 1265px;
    margin: 0 auto;
    padding-left: 20px;
    padding-right: 20px;
}
.clearfix:after {
    content:"";
    display: block;
    height: 0;
    width: 100%;
    clear: both;
}
.gc:after {
    content:"";
    display: block;
    height: 0;
    clear: both;
}
.gcpad {
    margin-left: -10px;
    margin-right: -10px;
}
.g1_4 {
    width: 25%;
    float: left;
    margin-left: 0;
    margin-right: 0;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    min-height: 20px;
    background: #66f;
}
.gpad {
    padding: 0 10px;
}
<div class="profile-left-column"></div>

<div class="profile-right-column">
    <div>      
        <div class="gc gcpad">
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
        </div>
        <div class="gc gcpad">
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
            <div class="g1_4 gpad"></div>
        </div>
    </div>
</div>

这是一个JSFiddle:http://jsfiddle.net/v3gma4d2/1/(这两个蓝条应该直接在它们自己之下)

有人能解释我为什么会这样吗?以及如何解决问题?

1 个答案:

答案 0 :(得分:2)

当您将元素设置为float时,浮动元素之后的元素将围绕它流动。想象一下,我们通常浮动图像,图像旁边的文本(足够多行)甚至在图像下方。

您有类似的情况,将左列设置为浮动,右列实际上在其周围流动,并且只要 clearfix 是补充说,这一切都立即破裂。请参阅以下简单演示。

&#13;
&#13;
.left {
    float: left;
    width: 100px;
    height: 100px;
    border: 1px solid red;
}
.right {
    border: 1px solid green;
}
.inner {
    border: 1px solid blue;
}
.inner:after {
    content: "";
    display: block;
    clear: both;
}
&#13;
<div class="left">1</div>
<div class="right">
    2
    <div class="inner">3</div>
    4
</div>
&#13;
&#13;
&#13;

解决问题。您可以将右栏设置为overflow:autohidden,以防止它包裹在左栏下方。

&#13;
&#13;
.left {
    float: left;
    width: 100px;
    height: 100px;
    border: 1px solid red;
}
.right {
    border: 1px solid green;
    overflow: auto; /*ADDED*/
}
.inner {
    border: 1px solid blue;
}
.inner:after {
    content: "";
    display: block;
    clear: both;
}
&#13;
<div class="left">1</div>
<div class="right">
    2
    <div class="inner">3</div>
    4
</div>
&#13;
&#13;
&#13;

,将右列设置为浮动(您还需要给它一个固定的或百分比widthcalc() CSS函数可能非常有用在这种情况下。)。

&#13;
&#13;
* { box-sizing: border-box; }
.left {
    float: left;
    width: 100px;
    height: 100px;
    border: 1px solid red;
}
.right {
    float: left; /*ADDED*/
    width: calc(100% - 100px); /*ADDED*/
    border: 1px solid green;
}
.inner {
    border: 1px solid blue;
}
.inner:after {
    content: "";
    display: block;
    clear: both;
}
&#13;
<div class="left">1</div>
<div class="right">
    2
    <div class="inner">3</div>
    4
</div>
&#13;
&#13;
&#13;