在写正文以前css
固然要注意:咱们这里所说的中间部分宽度自适应就是随着屏幕大小的改变而本身适应的过程。这也是三栏布局产生的缘由所在html
HTML代码浏览器
<div class="werppar"> <div class="left"></div> <div class="right"></div> <div class="container"></div> </div>
下面是 css 代码部分dom
.werppar{ /* 造成bfc块级做用域上下文 ,目的为了清除浮动*/ overflow: hidden; border: 5px solid red; } .left{ float: left; width: 200px; height: 200px; background-color: lawngreen; } .right{ float: right; width: 200px; height: 200px; background-color: lightcoral; } .container{ margin: 0 200px; height: 200px; background-color: rebeccapurple; }
html布局
<div class="werppar"> <div class="container"></div> <div class="left"></div> <div class="right"></div> </div>
下面是cssflex
.werppar{ position: relative; } .left{ position: absolute; top: 0; left: 0; width: 200px; height: 200px; background-color: lawngreen; } .right{ position: absolute; top: 0; right: 0; width: 200px; height: 200px; background-color: lightcoral; } .container{ margin: 0 200px; height: 200px; background-color: rebeccapurple; }
<div class="werppar"> <div class="left"></div> <div class="right"></div> <div class="container"></div> </div>
下面是css代码3d
.werppar{ width: 100%; } .left{ float: left; width: 200px; height: 200px; background-color: gold; } .right{ float: right; width: 200px; height: 200px; background-color: greenyellow; } .container{ /*这里造成bfc区域,不会与浮动的元素发生重叠*/ overflow: hidden; height: 200px; background-color: palevioletred; }
效果以下:
code
效果仍是同样,此时的container区域就是一个bfc区域了,那么它就不会被浮动元素盖住,container的宽度就是减去left的宽度 + right的宽度,剩下的就是本身的区域,这样不用像margin : 0 200px;左右撑开边距,因此也就造成的宽度自适应,不过它的缺点也是显而易见的,不能优先渲染container区域htm
<div class="werppar"> <div class="container"></div> <div class="left"></div> <div class="right"></div> </div>
cssblog
*{ margin: 0; padding: 0; } .werppar{ background-color: pink; /*左右栏经过添加负的margin放到正确的位置了,此段代码是为了摆正中间栏的位置*/ padding:0 200px; } .container,.left,.right{ float: left; height: 200px; } .container{ width:100%; height:200px; background:blue; } .left{ width:200px; height:200px; /*margin负值,让移动父级容器的宽度*/ margin-left:-100%; background:#f40; /*中间栏的位置摆正以后,左栏的位置也相应右移,经过相对定位的left恢复到正确位置*/ position:relative; left:-200px; } .right{ width:200px; height:200px; margin-left:-200px; background:#0c9; /*中间栏的位置摆正以后,右栏的位置也相应左移,经过相对定位的right恢复到正确位置*/ position:relative; right:-200px; }
能够看下效果图:
这就是所谓的圣杯布局,那么这个原理是什么呢? 其实就是基于两条 1.浮动2.margin
负值,我来解释解释
container
元素宽度100%,宽度全给了container
了,哪left和right天然会掉下去了margin-left
负值到一个界限,它就会贴回到上一行,由于他们是一块儿浮动的,因此咱们须要负一个它父容器的总宽度,这时候它回到的上一行,此时咱们还须要,使用定位将这个left元素,位移到正确的位置,这样就很简单left:-自身宽度便可
,margin
一个负的自身宽度,在使用定位right:-200px
将其回归到正确的位置,🆗这样就搞定了<div class="werppar"> <div class="box"> <div class="container"></div> </div> <div class="left"></div> <div class="right"></div> <p style="clear: both;"></p> </div>
css
.werppar{ width:100%; } .box,.left,.right{ float: left; height: 200px; } .box{ width:100%; height:200px; } .container{ margin : 0 200px; height: 200px; background:blue; } .left{ width:200px; height:200px; margin-left:-100%; background:#f40; } .right{ width:200px; height:200px; margin-left:-200px; background:#0c9; }
效果以下:
这里咱们能够看出,这里面和圣杯布局其实差异不是很大,
.box
的元素包裹起来了,并且浮动的不再是.container
元素了,这也就是说,container元素不会被外面的浮动元素所影响了left:-100%
就能回到想要的位置?这是为何呢,须要注意的一点是,在这里。left父级盒子是宽度100%的,再也不是圣杯布局中留出来左右padding值的父级自适应宽度的盒子margin-left:-200px
正好贴到父级盒子最右边,就能获得想要的位置了<div class="flex-box"> <div class="flex-content flex-item">我是内容</div> <div class="flex-left flex-item">我是左边栏</div> <div class="flex-right flex-item">我是右边栏</div> </div>
css
.flex-box{ display: flex; } .flex-left{ width: 200px; height: 200px; background-color: lime; order: 0; } .flex-content{ order: 1; width: 100%; background-color: aquamarine; } .flex-right{ width: 200px; height: 200px; background-color: gold; order: 2; }
这里就很简单了,利用弹性盒子的手法,给子元素添加的属性order,这个属性意思为在主轴方向的排列中显示的优先级,值越小,优先级越高,放在最前的container最早渲染
笔记总结 🆗,就到这里了,我还要继续奋战