前提条件:
图中实线部分可理解为可视尺寸,虚线部分可理解为占据尺寸。
应用
<img width="150" style="float:left;">
<p>...........p>
对 p 元素设置 margin-left 值
<img width="150" style="float:left;">
<p style="margin-left:170px">...........p>
适用条件:
应用
<div style="height:100px;padding:50px 0;">
<img height="300">
div>
<div style="height:200px;">
<img height="300" style="margin :50px 0;" >
div>
应用:
宽高比 2:1 自适应矩形
.box{background-color:olive;overflow:hidden;}
.box > div{ margin:50%;}
margin 重叠的三种情景
p{ line-height:1em margin 1em 0;background:red;}
<p>第一行p>
<p>第二行p>
.father{background:red;}
<div class="father">
<div class="son" style="margin-top:80px">嘿呦呦div>
div>
注意,以下三种情况效果一致
<div class="father">
<div class="son" style="margin-top:80px">嘿呦呦div>
div>
<div class="father" style="margin-top:80px">
<div class="son">嘿呦呦div>
div>
<div class="father" style="margin-top:80px">
<div class="son" style="margin-top:80px">嘿呦呦div>
div>
避免 margin-top/margin-bottom 重叠可让上述情况不成立
如第四个条件:破坏父元素和第一个子元素之间没有 inline 元素分隔
<div class="father" style="">
<div class="son" style="margin-top:80px">嘿呦呦div>
div>
.father{background:red; overflow:hidden;}
.son{margin:1em 0;}
<div class="father">
<div class="son" style="margin-top:80px">div>
div>
margin 重叠产生的意义
元素,不会影响原来的阅读排版;
善用 margin 重叠
同时设置 margin-top 和 margin-bottom ,更具健壮性,最后一个元素移除或位置调换,均不会破坏原来的布局。
元素有时候,就算没有设置 width 或 height,也会自动填满整个容器;如果元素为绝对定位元素,那么它的宽度会自动填满第一个父级定位元素。
如果设置了 width 或者 height,自动填充特性就会被覆盖。
img{width:150px;margin:0 auto;}
明明设置了 margin:0 auto;
为何图片布局中?
因为此时图片是 inline 水平,就算没有 width,它也不会占据整个容器
若对图片 display:block
就算没有设置 width 的值,也会占据整个容器,所以可以居中。
解决方法:
.father { height:200px;width:100%;writing-mode:verical-lr;}
.son{ height:100px;width:500px;margin:auto;}
原因:更换流的方向为垂直方向,实现垂直方向的 margin:auto 居中。
.father { height:200px;position:relative;}
.son{position:absolute;top:0;right:0;bottom:0; left:0;}
此时,没有设定 width/height,absolute 元素自动填满了容器。
若在此时,设置元素的宽高,并 margin:auto 即:
.father { height:200px;position:relative;}
.son{position:absolute;top:0;right:0;bottom:0; left:0; width:500px;height:100px;margin :auto;}
margin:auto 自动平分被变更的尺寸空间。注意,此时垂直和水平方向均居中。适用于IE8+浏览器
例子:
.box{width:1200px;margin:auto;background:orage;}
.ul{overflow:hidden;}
.li{width:380x;height:300px;margin-right:20px;background:green;float:left;}
<div class="box">
<div class="ul">
<div class="li">列表1div>
<div class="li">列表2div>
<div class="li">列表3div>
div>
div>
.box{width:1200px;margin:auto;background:orage;}
.ul{overflow:hidden;margin-right:-20px;}
.li{width:386.66px;height:300px;margin-right:20px;background:green;float:left;}
<div class="box">
<div class="ul">
<div class="li">列表1div>
<div class="li">列表2div>
<div class="li">列表3div>
div>
div>
.box{overflow:hidden;resize:vertical;}
.child-orange{float:left;background:orange;}
.child-green{float:left;background:green}
.box{overflow:hidden;resize:vertical;}
.child-orange,
.child-green{margin-bottom:-600px;padding-bottom:-600px;}
.child-orange{float:left;background:orange;}
.child-green{float:left;background:green}
margin-bottom的作用是改变容器的大小,用 padding-bottom 把消失的部分补回来。
父级元素要设置overflow:hidden
例子:
<img width="150" style="float:right;">
<p style="margin-right:170px">.....p>
不足:DOM 顺序和最终视觉顺序不符
解决方法:用 margin 负值实现
<div style="float:left;width:100%;">
<p style="margin-right:170px;">.....p>
div>
<img width="150" style="float:left;margin-left:-150px;">
img 标签设置的 margin-left 的绝对值,是 img 标签的宽度。
两个前提:
实际上绝对定位的 margin 值一直有效,只是不像普通元素那样,可以影响兄弟元素。
<div style="background-color:red;">
<img src="xx.jpg" style="float:left;">
<div style="overflow:hidden;margin-left:XX px">XXXXXXXX
div>
当 margin-left 的值小于 img 宽度时,margin 无效果。
<div style="height:200px;background:#f0f3f9;">
<img src="xx.jpg" style="margin-top:-200px">
div>
此时,margin-top 的绝对值继续增大,img 标签也不会继续向上移动。
在 img 标签后添加一个 “x” 做辅助,可以看出 img 与 “x” 的底部对齐。原因是内联元素默认基线对齐。
只有在 webkit 下才兼容。如下:
img {
-webkit-margin-before:100px;
}
默认流向的情况,margin-before 等同于 margin-top,margin-after 等同于 margin-bottom。
margin-collapse:控制 margin 重叠
-webkit-margin-collapse:<collapse> |<discard> | <separate>
其中:
详细参考慕课网