css - 关于margin的问题?小练习

查看:137
本文介绍了css - 关于margin的问题?小练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

直接上代码,麻烦帮我看下哪里错了,为什么到box2的位置,margin就不好使的。
<style type="text/css">

#box{height: 404px;width: 404px;background-color: #fff;border: 1px dashed;}
#box1{height: 334px;width: 334px;margin:27px ; background-color: #f6f6f6;border: 8px solid #d7effe;    }
#box2{height: 300px;width: 300px;margin: 20px; background-color:#ffa0df;    }
#box3{height: 216px;width: 216px;margin:41px ; background-color: #ffa0df;border: 1px dashed #ffffff;    }
#box4{height: 208px;width: 208px;margin:3px ; background-color: #ffa0df;border: 1px dashed #ffffff;    }
#box5{height: 102px;width: 102px;margin:49px; background-color: #96ff38;border: 4px solid #fcff00;    }
</style>

<body>

<div id="box">
    <div id="box1">
        <div id="box2">
            <div id="box3">
                <div id="box4">
                    <div id="box5"></div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>

做出了的效果

实际上我要做的效果

问题来了,box2和box3上面的margin值为什么出现问题,这个要怎么改。

解决方案

这是margin边距重叠的问题
你的#box2 和 #box3 的外边距重叠了,在#box1 #box2 #box3 之间没有border隔断,导致外边距选大的,即#box3的margin 41px起了作用,而#box2的margin-top没有起作用

你可以把box都BFC话,添加overflow:hidden

或者 给box2添加border予以隔断

这篇关于css - 关于margin的问题?小练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆