CSS3 盒模型- 外边距折叠

本文详细介绍了CSS3中垂直外边距折叠的现象,并提供了三种消除外边距折叠的方法:1) 使用`display:inline-block`;2) 设置元素`float`属性不为`none`;3) 应用绝对定位。通过这些技巧,开发者可以更好地控制元素间的间距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1简介

外边距折叠: 即垂直方向上的两个外边距相遇时,会折叠成一个外边距,且折叠之后的外边距高度为两者之中较大的那一个。

示例:
在这里插入图片描述
代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .top{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(37, 35, 35);
            }
            .bottom{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(200, 90, 90);
            }
        </style>
    </head>
    <body>
        <section class="top"></section>
        <section class="bottom"></section>
    </body>
</html>

2 消除外边距内折

2.1 display: inline-block

样式:

在这里插入图片描述
代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .top{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(37, 35, 35);
                display: inline-block  /* 只为其中一个元素添加也可达到效果*/
            }
            .bottom{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(200, 90, 90);
                display: inline-block
            }
        </style>
    </head>
    <body>
        <section class="top"></section>
        <section class="bottom"></section>
    </body>
</html>

2.2 float属性值不是"none"的元素

代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .top{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(37, 35, 35);
                float: left;  /* 两个元素都要添加*/
            }
            .bottom{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(200, 90, 90);
                float: left; /* 两个元素都要添加*/
            }
        </style>
    </head>
    <body>
        <section class="top"></section>
        <section class="bottom"></section>
    </body>
</html>

2.3 绝对定位

代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .top{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(37, 35, 35);
             
            }
            .bottom{
                width:100px;
                height:100px;
                margin:10px;
                background-color: rgb(200, 90, 90);
                position: absolute; /* 只能给该元素添加*/
                
            }
        </style>
    </head>
    <body>
        <section class="top"></section>
        <section class="bottom"></section>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GT-一二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值