设置margin-top影响到父容器 / 子DIV设置margin-top影响父DIV位置

父级并没有完全保住子集,父级没有包含子集的margin-top部分, 原因:CSS2.1的盒模型中规定: In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin. 所有毗邻的两个或更多盒元素的margin将会合并为一个margin共享之。毗邻的定义为:同级或者嵌套的盒元素,并且它们之间没有非空内容、 Padding或Border分隔。 CSS2.1规定浮动元素和绝对定位元素不参与Margin折叠 所以解决办法为:

1.给.parent设置padding 2.给parent设置float或position:absolute( 别忘记设置宽度,他们已经脱离文档流了) 3.给parent设置overflow:hidden/auto/overlay/scroll 建议使用hidden(scroll会出现滚动条)

4.让父级具有“包裹性”

5.将父级display设为inline-block

6.改变父级的结构

7.给父元素设置透明border

猜你喜欢

转载自blog.csdn.net/lingdian1614114082/article/details/86506785
今日推荐