三栏浮动布局
浮动的方式有很多种,比如像下面这个例子,是把左右两个板块固定大小和位置,中间自适应。
先看效果
窗口最大化

缩小化

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>三栏浮动布局</title> <style type="text/css"> .wrapper{ width: 100%; height: calc(100vh); background-color: bisque; box-sizing: border-box; } .wrapper .left { width: 200px; height: 300px; background: #faa; float: left; } .wrapper .right { width: 200px; height: 300px; background: #afa; float: right; } .wrapper .content { height: 300px; background-color: #aaf; margin:0 200px; } </style> </head> <body> <div class="wrapper"> <div class="left"></div> <div class="right"></div> <div class="content"></div> </div> </body> </html>
|
在缩小窗口后,左边和右边的板块就会把中间的板块挤掉,或者覆盖重叠
(窗口缩小后就只能看到左右栏,这感觉就很像某些网页侧边广告,不信你试试)。

缩小后

inline-block方式
使用外部div添加align-center属性,内部三个板块添加inline-block属性来实现
先看效果吧
窗口最大化

缩小后

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>三栏inline-block布局</title> <style type="text/css"> .main-container{ position: absolute; background-color: darkgrey; text-align: center; margin-left: 0; margin-right: 0; left: 0; right: 0; } .part-left{ background-color: #0ac276; display: inline-block; } .part-right{ background-color: aqua; display: inline-block; } .part-center{ background-color: coral; display: inline-block; } </style> </head> <body> <div class="main-container"> <div class="part-left"> <div style="width: 200px; height: 200px;background-color: blue;"> </div> </div> <div class="part-right"> <div style="width: 200px; height: 200px;background-color: brown;"> </div> </div> <div class="part-center"> <div style="width: 200px; height: 200px;background-color: rgb(51, 30, 30);"> </div> </div> </div> </body> </html>
|
当然如果你想让这几个板块之间保持间距,可以为目标div添加上margin属性,比如margin-left或者margin-right这样。浮动和非浮动这两种方法各有特点(也许可以做到一样的效果呢,可能是我太菜了没想到而已)
使用Flex实现
关于Flex介绍可以看看阮老师的blog
先看效果
窗口最大化

缩小化

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .wrapper{ background-color: bisque; width: 100%; height: calc(100vh); display: flex; justify-content: center; align-items: center; } .wrapper .part-left{ background-color: rgb(161, 59, 59); width: 200px; height: 200px; } .wrapper .part-center{ background-color: rgb(58, 156, 58); width: 200px; height: 200px; } .wrapper .part-right{ background-color: darkturquoise; width: 200px; height: 200px; } </style> </head> <body> <div class="wrapper"> <div class="part-left"></div> <div class="part-center"></div> <div class="part-right"></div> </div> </body> </html>
|
目前个人比较喜欢用inline-block和flex方法吧。float其实也行,不过的话要注意清除浮动,否则会出现意料之外的情况.