这里有一份简洁的前端知识体系等待你查收,看看吧,会有惊喜哦~若是以为不错,恳求star哈~前端
盒子模型包括:content、padding、margin、bordergit
dom.style.width/height:内联样式的宽高
dom.currentStyle.width/height:渲染后的最终宽高(IE)
window.getComputedStyle(dom).width/height:DOM标准,不支持IE
dom.getBoundingClientRect().width/height:计算元素的绝对位置(视窗左顶点为起点,含left/right/height/width)
复制代码
块级格式化上下文github
<style>
body {
width: 300px;
position: relative;
}
.aside {
width: 100px;
height: 150px;
float: left;
background: #f66;
}
.main {
height: 200px;
background: #fcc;
}
</style>
<body>
<div class="aside"></div>
<div class="main"></div>
</body>
复制代码
BFC的区域不会与float box重叠。dom
咱们能够经过经过触发main生成BFC, 来实现自适应两栏布局。ide
.main {
overflow: hidden;
}
复制代码
当触发main生成BFC后,这个新的BFC不会与浮动的aside重叠。所以会根据包含块的宽度,和aside的宽度,自动变窄。效果以下:布局
<style>
.par {
border: 5px solid #fcc;
width: 300px;
}
.child {
border: 5px solid #f66;
width:100px;
height: 100px;
float: left;
}
</style>
<body>
<div class="par">
<div class="child"></div>
<div class="child"></div>
</div>
</body>
复制代码
计算BFC的高度时,浮动元素也参与计算spa
为达到清除内部浮动,咱们能够触发par生成BFC,那么par在计算高度时,par内部的浮动元素child也会参与计算。code
.par {
overflow: hidden;
}
复制代码
<style>
p {
color: #f55;
background: #fcc;
width: 200px;
line-height: 100px;
text-align:center;
margin: 100px;
}
</style>
<body>
<p>Haha</p>
<p>Hehe</p>
</body>
复制代码
Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠cdn
咱们能够在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了。blog
<style>
.wrap {
overflow: hidden;
}
p {
color: #f55;
background: #fcc;
width: 200px;
line-height: 100px;
text-align:center;
margin: 100px;
}
</style>
<body>
<p>Haha</p>
<div class="wrap">
<p>Hehe</p>
</div>
</body>
复制代码
其实以上的几个例子都体现了BFC布局规则第五条:
BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。