标签:
一. 两栏布局
1.通过两栏浮动进行布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container {width: 100%; height: 400px; overflow: hidden;} .col {float: left; border: 1px solid #aaa; height: 400px;} .col1 {width:80%; background-color: #f00;} .col2 {width: 19%; background-color: #0f0;} </style> </head> <body> <div class="container"> <div class="col col1"></div> <div class="col col2"></div> </div> </body> </html>
2.为每一列添加容器进行布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container { width: 960px; margin: 0 auto; } .container1 { float: left; width: 100%; overflow: hidden; position: relative; background-color: #f00; } .container2 { float: left; width: 100%; position: relative; right: 500px;/* 值为第二列的宽度 */ background-color: #0f0; } .col1 { float: left; width: 460px; position: relative; left: 500px;/* 值为除第一列外的所有列宽和 */ } .col2 { float: left; width: 500px; position: relative; left: 500px;/* 值为除第一列外的所有列宽和 */ } </style> </head> <body> <div class="container"> <div class="container1"> <div class="container2"> <div class="col1">aaaa</div> <div class="col2">bbbbb</div> </div> </div> </div> </body> </html>
效果展示:
代码思路:
3.利用边框创建两栏布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container { width: 100%; height: 20px; border-left: 400px solid #aaa; } .col1 { float: left; width: 400px; margin-left: -400px; margin-right: -1px; border-right: 1px solid #666; } .col2 { float: left; border-left: 1px solid #666; } </style> </head> <body> <div class="container"> <div class="col1">aaa</div> <div class="col2">bbb</div> </div> </body> </html>
效果展示:
代码思路:
4.利用margin和绝对定位进行布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container { width: 960px; margin: 0 auto; /* overflow: hidden; */ } .col1 { position: absolute; width: 740px; background-color: #ccc; } .col2 { position: absolute; margin-left: 740px;/* 值为第一列的宽度值 */ width: 220px; background-color: #aaa; } </style> </head> <body> <div class="container"> <div class="col1">aaa</div> <div class="col2">bbb</div> </div> </body> </html>
效果展示:
代码思路:
二.三栏布局
1.左浮动布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container { width: 960px; margin: 0 auto; overflow: hidden; } .col { float: left; width: 30%; } .col1 { background-color: #f00; } .col2 { background-color: #0f0; } .col3 { background-color: #00f; } </style> </head> <body> <div class="container"> <div class="col col1">aaa</div> <div class="col col2">bbb</div> <div class="col col3">bbb</div> </div> </body> </html>
效果展示:
2.为每一列添加容器进行布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .container { width: 960px; margin:0 auto; } .container3 { width: 100%; float: left; overflow: hidden; } .container2 { width: 100%; float: left; position: relative; right: 320px;/* 第三列的宽度 */ } .container1 { width: 100%; float: left; position: relative; right: 420px;/* 第二列的宽度 */ } .col1 { width: 220px; float: left; position: relative; left: 740px;/* 除第一列外其他列宽总和 */ background-color: #f00; } .col2 { width: 420px; float: left; position: relative; left: 740px;/* 除第一列外其他列宽总和 */ background-color: #0f0; } .col3 { width: 320px; float: left; position: relative; left: 740px;/* 除第一列外其他列宽总和 */ background-color: #00f; } </style> </head> <body> <div class="container"> <div class="container3"> <div class="container2"> <div class="container1"> <div class="col1">aaa</div> <div class="col2">bbb</div> <div class="col3">ccc</div> </div> </div> </div> </div> </body> </html>
效果展示:
代码思路:
第二步所对应的思路:每一列进行相对定位,到右边的距离为对应列的宽度
第三步对应的思路:每一列进行相对定位,距离左边的为除第一列外的其他列宽的总和
3.通过为给中间列添加边框进行布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .containerOuter { width: 960px; margin: 0 auto; } .container { float: left; width: 520px;/* 为col2容器 */ border-left: 220px solid #0f0;/* 为左边列的宽度,背景色 */ border-right: 220px solid #f00;/* 为右边列的宽度,背景色 */ background-color: #0ff; } .col1 { float: left; /* 值为container容器的边框宽度,也为本列的具体宽度 */ width: 220px; margin-left: -220px; position: relative; } .col2 { float: left; width: 520px; margin-right: -520px; } .col3 { float: right; width: 220px; margin-right: -220px; position: relative; } </style> </head> <body> <div class="containerOuter"> <div class="container"> <div class="col col1">aaa</div> <div class="col col2">sss</div> <div class="col col3">dddd</div> </div> </div> </body> </html>
效果展示:
代码思路:
延伸:相同原理,延伸成流体布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> * { margin:0; padding:0; } .containerOuter { margin: 0 100px; padding: 0 220px; } .container { float: left; width: 100%; border-left: 220px solid #0f0;/* 为左边列的宽度,背景色 */ border-right: 220px solid #f00;/* 为右边列的宽度,背景色 */ margin-left: -220px; margin-right: -220px; display: inline; background-color: #0ff; } .col1 { float: left; /* 值为container容器的边框宽度,也为本列的具体宽度 */ width: 220px; margin-left: -220px; position: relative; } .col2 { float: left; width: 100%; margin-right: -100%; } .col3 { float: right; width: 220px; margin-right: -220px; position: relative; } </style> </head> <body> <div class="containerOuter"> <div class="container"> <div class="col col1">aaa</div> <div class="col col2">sss</div> <div class="col col3">dddd</div> </div> </div> </body> </html>
效果展示:
未完待续
标签:
原文地址:http://www.cnblogs.com/cfWeb/p/5206315.html