微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – 给子div一个相对于父div的边距[复制]

参见英文答案 > Why does this CSS margin-top style not work?                                    11个
这是我的HTML:

这是我的CSS:

.parentDiv {
    margin-top: 200px;
    width: 700px;
    height: 800px;
    background-color: red;
}

.childDiv {
    background-color: green;
    width: 50px;
    height: 50px;
    margin-top: 22px;
}

小提琴:http://jsfiddle.net/1whywvpa/

为什么childDiv没有获得22px的保证金?如果像素大于已经给予parentDiv的200px,则它仅获得上边距.有什么方法可以让childDiv获得相对于parentDiv的父div为22px而不做某种类型的’给父div一个1px填充’hack?

最佳答案
也许这可以帮助:CSS Margins Overlap Problem

向两个元素添加position属性.父母是亲戚,孩子是绝对的……

.parentDiv {
    position: relative;
    margin-top: 200px;
    width: 700px;
    height: 800px;
    background-color: red;
}

.childDiv {
    position: absolute;
    background-color: green;
    width: 50px;
    height: 50px;
    margin-top: 22px;
}

这是你的小提琴:http://jsfiddle.net/algorhythm/1whywvpa/5/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐