標籤:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>混合布局編程挑戰</title>
<style type="text/css">
/*
body{ margin:0; padding:0; font-size:30px; color:#fff}
.top{width:100%;background-color:#ccc;}
.main{width:100%;height:350px;overflow:hidden;background-color:#f90;}
.left{ width:200px;height:inherit;background-color:#6495ED;float: left;}
.right{width:55%;height:inherit;float:right;background-color:lightgreen;}
.foot{width:100%;background-color:#DC143C;}
*
* 1-1:我打算是這麼寫的,很不進階,尤其是右邊,根據頁面的變化,他會向左擠掉left;看慕課兄的代碼如下:*/
.top{width:100%; height:50px;background:#ccc;}
.main{width: 100%;/*width:1000px;*/ position:relative;height:300px;background:#f90;}
.foot{width:100%;height:100px;background-color:#DC143C;}
/*.left{width:200px;height:300px;background:#6495ED;position:absolute;}*/
/*第二種也有問題*/
/*2-2:為什麼非要absolute呢?他是相對於body呀在這裡,當main有定值時還通用嗎?*/
/*2-3:實驗證明是不可以的*/
/*2-4:既是他這中方法,也是讓right盒子一直超出,還有水平捲軸。*/
/*.right{margin-left:210px;width:100%;height:300px;background:lightgreen;position:absolute;}*/
/*2-1:通過position:absolute和定值margin-left,這樣width就可以設定成100%,進而成了響應式,不管視窗多大都不會擠掉left的效果*/
/*1-1:其實一開始的讓右邊自適應,我想到的是width100%,但是會把left覆蓋住.我這就沒想到margin-left呢!*/
/*第三種方法是可以*/
.left{width: 200px;height: 300px;background:#6495ED;position: absolute;left:0;top: 0;}
/*用left:0,top:0,固定left的位置。然後用right的margin-left把左邊的位置給讓出來*/
.right{height:300px;/*width:100%;*/background:lightgreen;margin-left: 210px;}
/*right不設定寬度,如果設定了寬度100%,就會出現水平條*/
/*第四種也可以*/
/*.main{width:100%;height:300px;background-color:red;}
.left{ width:200px;height:300px;background-color:blue;float:left;}
.right{height:300px;background-color:green;position:absolute;left:210px;right:0px;}*/
/*方法是利用左側浮動固定寬度,右側通過絕對位置,*//*right不設定寬度,如果設定了寬度100%,就會出現水平條*/
/*
總結
* 普遍就是通過position的相對,絕對位置,++top,right,left;的相互配合,或通過margin的移動,實現效果。這裡foot處沒有清除浮動。看上去沒什麼影響就不清楚了。
*
* */
</style>
</head>
<body>
<div class="top">top</div>
<div class="main">
<div class="right">right</div><!--實現右側先載入-->
<div class="left">left</div>
</div>
<div class="foot">foot</div>
</body>
</html>
CSS-混合布局的幾種方法(正確的方法和錯誤的原因)