javascript - 子元素设置margin-top为什么会作用在父元素上?
黄舟
黄舟 2017-04-11 11:31:55
[JavaScript讨论组]

如图,为什么我给p标签元素设置的margin-top会作用在p上面?

this is a para

* {
    padding: 0;
    margin: 0;
}
.test {
    position: relative;
    height: 400px;
    width: 1000px;
    margin: 0 auto;
    margin-top: 0;
    margin-bottom: 0;
    background-color: #DCF1B8;
}
.para {
    width: 20%;
    margin-left: auto;
    margin-right: auto;
    background-color: #734E4E;
    height: 50%;
    margin-top: 100px;
    margin-bottom: 100px;
}
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(2)
怪我咯

CSS外边距合并

http://www.w3school.com.cn/css/css_margin_collapsing.asp

高洛峰

因此推荐尽量优先使用padding来控制间距

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号