温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css如何解决网页重叠问题

发布时间:2020-12-18 14:02:28 来源:亿速云 阅读:204 作者:小新 栏目:web开发

这篇文章将为大家详细讲解有关css如何解决网页重叠问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

css解决网页重叠的方法:1、给父元素设置边框;2、给父元素添加padding;3、在子元素上方加一个有宽高的兄弟元素;4、给父元素设置“overflow: hidden;”属性等。

css解决网页边距重叠

下面给大家介绍用css防止边距重叠的几种方法。

先假设一组dom结构

<div class="parent">
    <div class="child">
    </div>
</div>

通常情况下,如果给子元素设置margin,就会产生这个属性对父元素也产生了同样的效果,然而

这其实不是我们想要的结果,我们只想对子元素设置margin,那么现在我们应该怎么做呢?

1、给父元素设置边框

.parent { 
    width: 300px;       
    height: 300px;
    border: 1px solid #ccc;
}
.child {
    width: 200px;
    height: 200px;
    margin: 20px;
}

2、给父元素添加padding

.parent {
    padding: 1px;
    width: 300px;
    height: 300px;
}
.child {
    width: 200px;
    height: 200px;
    margin: 20px;
}

3、在子元素上方加一个有宽高的兄弟元素,记住是有宽高的。

<div class="parent">
     <div style="width: 20px;height: 20px;margin-top: "></div>
     <div class="child">
     </div>
</div>

4、给父元素设置 overflow: hidden; 属性

.parent {
    overflow: hidden;
    width: 300px;
    height: 300px;
}
.child {
    width: 200px;
    height: 200px;
    margin: 20px;
}

5、给子元素设置 display: inline-block;(如果子元素是行内元素或者行内块级元素则不会产生边距重叠的问题)

.parent {
    width: 300px;
    height: 300px;
} 
.child {
    width: 200px;
    height: 200px;
    margin: 20px; 
    display: inline-block;
}

6、使子元素脱离文档流这个实现的方法有很多,浮动,绝对定位等,这里我就不做具体的解释了。

关于css如何解决网页重叠问题就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI