<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{padding:0;margin: 0;} header{ background-color: darkcyan; height: 50px; } .container{ background-color:cyan; overflow: hidden; /*4. 因为文字会被left元素挡住,所以使用padding将内容显示出来,同时需要知道left的宽度*/ padding-left:200px; padding-right: 300px; } .middle,.left, .right{ float: left; /* 1. 三者都设置为float left,且三者都有高度*/ height: 500px; /*5. 此时left也会跟着回来,所以使用相对定位*/ position: relative; word-wrap: break-word;/*解决文字换行*/ } .middle{ background-color: palevioletred; width: 100%; /* 2. middle 宽度使用100% */ } .left{ width: 200px; background-color: tomato; margin-left: -100%; /*3. 使用margin-left: -100% 将left元素拉回来*/ /*5.1 对left进行设置*/ left: -200px; } .right{ background-color: purple; width: 300px; /*5.2 将right盒子拉回*/ margin-left: -300px; right: -300px; } footer{ background-color: yellowgreen; height: 60px; } </style> </head> <body> <header> <h4>header</h4> </header> <div class="container"> <div class="middle"> <h4>middle</h4> <p>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p> </div> <div class="left"> <h4>left</h4> <p>llllllllllllllllllllllllllllllllllll</p> </div> <div class="right"> <h4>right</h4> <p>rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr</p> </div> </div> <footer> <h4>footer</h4> <p>pppppppppppppppppp</p> </footer> </body> </html>
注意:从图片上看感觉布局一样
但还是要看代码哟,双飞翼布局没有使用position,而且container只包括了middle元素哦
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>双飞翼布局</title> <style> * { padding: 0; margin: 0; text-align: center; word-wrap: break-word; } header { background-color: darkcyan; height: 50px; } .container { width: 100%;/*2. width:100%*/ } .container,.left,.right{ float: left;/*1.float*/ height: 500px; } .middle { background-color: palevioletred; margin-left: 300px;/*4.使用margin 将左右两边留白*/ margin-right: 200px; height: 500px; } .left { background-color: tomato; width: 300px; margin-left: -100%; /*3.1 将left拉回来*/ } .right { background-color: purple; width: 200px; margin-left: -200px; /*3.2 将right拉回来*/ } footer { background-color: yellowgreen; height: 60px; clear: both;/*5.使底部恢复 clear 属性规定元素的哪一侧不允许其他浮动元素。*/ } </style> </head> <body> <header> <h4>header</h4> </header> <div class="container"> <div class="middle"> <h4>middle</h4> <p>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p> </div> </div> <div class="left"> <h4>left</h4> <p>llllllllllllllllllllllllllllllllllll</p> </div> <div class="right"> <h4>right</h4> <p>rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr</p> </div> <footer> <h4>footer</h4> <p>pppppppppppppppppp</p> </footer> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>flex弹性布局</title> <style> * { padding: 0; margin: 0; text-align: center; } header { background-color: darkcyan; height: 50px; } .container { background-color: cyan; display: flex; } .middle, .left, .right { height: 500px; word-wrap: break-word; /*解决文字换行*/ } .middle { background-color: palevioletred; } .left { width: 200px; } .right { background-color: purple; } footer { background-color: yellowgreen; height: 60px; } </style> </head> <body> <header> <h4>header</h4> </header> <div class="container"> <div class="left"> <h4>left</h4> <p>llllllllllllllllllllllllllllllllllll</p> </div> <div class="middle"> <h4>middle</h4> <p>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p> </div> <div class="right"> <h4>right</h4> <p>rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr</p> </div> </div> <footer> <h4>footer</h4> <p>pppppppppppppppppp</p> </footer> </body> </html>
注意:
我发现虽然设置了left 和 right 的宽度 但是随着,middle 里内容的文字增加,right的宽度会变化
我对上面的代码进行了修改,如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>flex弹性布局</title> <style> * { padding: 0; margin: 0; text-align: center; word-wrap: break-word; } header { background-color: darkcyan; height: 50px; } .container { background-color: cyan; display: flex;/*1.设置flex属性*/ } .middle, .left, .right { height: 500px; word-wrap: break-word; /*解决文字换行*/ } .middle { background-color: palevioletred; flex-grow: 1; } .left { order: -1; /*2.设置布局,定义项目的排列顺序,越小越靠前,默认为0*/ flex-basis:200px; } .right { background-color: purple; flex-basis:300px; } footer { background-color: yellowgreen; height: 60px; } </style> </head> <body> <header> <h4>header</h4> </header> <div class="container"> <div class="middle"> <h4>middle</h4> <p>mmmmmmmmmmmmmmmmmmmmmmmmm</p> </div> <div class="left"> <h4>left</h4> <p>lllllllllllllllllllllllll</p> </div> <div class="right"> <h4>right</h4> <p>rrrrrrrrrrrrrrrrrrrr</p> </div> </div> <footer> <h4>footer</h4> <p>pppppppppppppppppp</p> </footer> </body> </html>
加入了flex-grow:使其剩下的填满
flex-basis:设置规定值
得到了下面的布局
但是还是会随着每个内容的长度进行比例切换
比如当m增加时,会这样,对于left 和right里内容也是这样
我又重新查了一下flex的属性
例
.box { display: flex; justify-content: center; align-items: flex-end; }
使用sapce-between
.box { display: flex; justify-content: space-between; }
.box { display: flex; flex-direction: column; justify-content: space-between; }
align-self
.box { display: flex; } .item:nth-child(2) { align-self: center; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .HolyGrail { display: flex; min-height: 100vh; flex-direction: column; } header, footer { flex: 1; background-color: darkcyan; } .HolyGrail-body { display: flex; flex: 1; background-color: blueviolet; } .HolyGrail-content { flex: 1; background-color: red; } .HolyGrail-nav, .HolyGrail-ads { /* 两个边栏的宽度设为12em */ flex: 0 0 12em; background-color: yellow; } .HolyGrail-nav { /* 导航放到最左边 */ order: -1; } </style> </head> <body class="HolyGrail"> <header>...</header> <div class="HolyGrail-body"> <main class="HolyGrail-content">cccccccccccccccccccccccccccccccccccccccccccccccccccccccccc</main> <nav class="HolyGrail-nav">lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll</nav> <aside class="HolyGrail-ads">...</aside> </div> <footer>...</footer> </body> </html>
我在对body中加入了文字换行,但是结果布局的左边还是会被内容撑到变形