本篇文章,我將採用上述幾種不一樣的方式,來實現下面的六種佈局css
<style> body{margin:0; padding: 0;} .left{ width: 300px; height: 500px; float: left; background-color: cornflowerblue; } .right{ height: 500px; margin-left: 300px; background-color: coral; } </style>
<body>
<div class="main">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
</body>
複製代碼
<style> body{margin: 0;padding: 0;} .left{ width: 200px; height: 500px; float: left; background-color: cornflowerblue; } .main{ height: 500px; margin: 0 300px 0 200px; background-color: darkgoldenrod; } .right{ width: 300px; height: 500px; position: absolute; right: 0; top: 0; background-color: darkseagreen; } </style>
<body>
<div class="left">Left</div>
<div class="main">Main</div>
<div class="right">Right</div>
</body>
複製代碼
<style> .top{ height: 100px; background-color: yellow; } .main{ margin-top: 10px; } .left{ float: left; width: 100px; height: 500px; background-color: yellowGreen; } .right{ height: 500px; margin-left: 110px; background-color: red; } </style>
<body>
<div class="top">Top</div>
<div class="main">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
</body>
複製代碼
練習一: float實現上左右下佈局html
練習二: float實現上左中右下佈局git
<style> .container{ display: flex; } .left{ width: 300px; height: 500px; background-color:cornflowerblue; } .right{ height: 500px; flex: 1; background-color:darkgoldenrod; } </style>
<body>
<div class="container">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
</body>
複製代碼
<style> .container{ display: flex; } .left{ width: 200px; height: 500px; background-color:darksalmon; } .main{ height: 500px; flex: 1; margin: 0 10px; background-color:darkorange; } .right{ width: 200px; height: 500px; background-color:darkmagenta; } </style>
<body>
<div class="container">
<div class="left">Left</div>
<div class="main">Main</div>
<div class="right">Right</div>
</div>
</body>
複製代碼
<style> .top{ height: 200px; background-color:darkorange; } .container{ display: flex; } .left{ width: 300px; height: 500px; background-color: tomato; } .right{ height: 500px; flex: 1; background-color: yellowGreen; } </style>
<body>
<div class="top">Top</div>
<div class="container">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
</body>
複製代碼
練習一: flex實現上左右下佈局github
練習二: flex實現上左中右下佈局瀏覽器
優勢: 簡單,兼容性好佈局
缺點: 脫離文檔流,須要手動清除浮動flex
優勢: 很完美的一種佈局方案spa
缺點: 不兼容IE8如下的瀏覽器3d
優勢: 快捷,方便code
缺點: 脫離文檔流,維護成本高,不利於拓展
常見的清楚浮動主要有以下幾種方法
- 直接在最下面添加空元素塊,併爲空元素塊添加屬性 style="clear:both"
- 使用僞類,
:after