昨天不知道因爲網速的原因還是怎麼會回事,該題的圖片沒有顯示,但是感覺是要實現兩欄式佈局,不要求自適應,應該利用BFC的知識就好了,只要兩個子元素分別形成BFC即可。
形成BFC的方式:
(1)float:Left、right
(2)position: absolute、fixed
(3)display:inline-block、table-caption、inline-table
(4)overflow:hidden
只要兩個元素利用如上的方式分別形成BFC則可以實現兩欄式佈局
如利用inline-block以及float 實現效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>兩列式佈局</title>
<style type="text/css">
.container{
}
.left{
/* float:left;*/
width:100px;
height:100px;
background: red;
display:inline-block;
}
.right{
float:left;
width:100px;
height:100px;
background: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
同理可以同時使用float實現上面的效果(藍色的box和紅色的box的位置會不一樣),但是就是這樣將會導致父元素的塌陷,即父元素沒有高度,解決辦法是清除浮動或者是使父元素也形成BFC,給父元素添加某一屬性。
當然之前提到的多欄式佈局的方式也適用於此,利用flex的方式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>兩列式佈局</title>
<style type="text/css">
.container{
display :flex
}
.left{
width:100px;
height:100px;
background: red;
}
.right{
width:100px;
height:100px;
background: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
感覺這題主要是在考察BFC,但是因爲圖片沒有看到,所以很多都是猜測的。。。。。
在實現多欄式佈局時在沒有嚴格要求的情況下,除了這種方式以外還可以考慮利用BFC來實現效果。同時如果是定寬的也可以考慮CSS3的多列布局的方式:
{
column-count
column-rule
column-gap
}