如上圖所示對頁面進行佈局html
左右兩部分是固定寬度的,中間黃色的區域能夠隨頁面寬度的不一樣而調整,且與兩邊div有個間距。佈局
方法一:浮動flex
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body,div{ margin:0;padding:0; } .box1{ float:left; width:200px; height:400px; background:blue; } .box3{ height:400px; background:yellow; margin:0 220px;/*margin值是讓3個div之間有個20px的間距*/ } .box2{ float:right; width:200px; height:400px; background:red; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </body> </html>
元素只能影響後面的浮動元素的位置,對前面的沒有影響,因此box一、box2左右浮動後位置就固定了,而後加入的box3由於有高度,因此寬度會自適應。spa
方法二:中間定位code
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body,div{ margin:0;padding:0; } .box1{ float:left; width:200px; height:400px; background:blue; } .box2{ float:right; width:200px; height:400px; background:red; } .box3{ height:400px; background:yellow; position:absolute; left:220px; right:200px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </body> </html>
附:元素居中的方法:{position:absolute;left:0;right:0;top:0;bottom:0;margin:auto;}htm
方法三:兩邊定位blog
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body,div{ margin:0;padding:0; } .box1{ position:absolute; left:0; top:0; width:200px; height:400px; background:blue; } .box2{ position:absolute; right:0; top:0; width:200px; height:400px; background:red; } .box3{ height:400px; background:yellow; margin:0 220px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </body> </html>
方法四:彈性盒模型it
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body,div{ margin:0;padding:0; } body{ width:100vw; display:flex;/*彈性盒*/ flex-flow:row nowrap;/*主軸方向 子元素橫向排列*/ justify-content: space-between;/*主軸上的對齊方式*/ } .box1{ width:200px; height:400px; background:blue; } .box2{ width:200px; height:400px; background:red; } .box3{ flex-grow:1;/*設置元素的擴展比率*/ height:400px; background:yellow; margin:0 20px; } </style> </head> <body> <div class="box1"></div> <div class="box3"></div> <div class="box2"></div> </body> </html>