zoukankan      html  css  js  c++  java
  • 外边距的重叠

    垂直方向上的外边距

      垂直外边距的重叠

        -相邻的垂直方向外边距发生重叠现象

        -兄弟元素

          -兄弟元素间的相邻垂直外边距会取二者之间的较大值(二者都是正值)

          -特殊情况:

            如果相邻的外边距一正一负,则取两者的和

            如果相邻的外边距都是负值,则取两者中绝对值较大的

          -兄弟元素之间的外边距的重叠,对于开发是有利的,所以我们不需要处理

    第一种情况:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box1,.box2{
                width: 200px;
                height: 200px;
                font-size: 100px;
            }
            .box1{
                background-color: red;
                margin-bottom: 100px;
            }
            .box2{
                background-color: pink;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
        <div class="box2"></div>
    </body>
    </html>

    效果如下:

     第二中情况(相邻外边距的值一正一负):

      

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box1,.box2{
                width: 200px;
                height: 200px;
                font-size: 100px;
            }
            .box1{
                background-color: red;
                margin-bottom: -50px;
            }
            .box2{
                background-color: pink;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
        <div class="box2"></div>
    </body>
    </html>

    效果如下:

      

     -父子元素

      -父子元素间相邻(父子上外边距重叠)外边距,子元素的会传递给父元素(上外边距)

      -父子外边距的折叠会影响到页面的布局,必须要进行处理

    例如下面的现象:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box1,.box2{
                width: 200px;
                height: 200px;
                font-size: 100px;
            }
            .box1{
                background-color: red;
                /* margin-bottom: -50px; */
            }
            .box2{
                width: 100px;
                height: 100px;
                background-color: pink;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="box1"><div class="box2"></div></div>
        
    </body>
    </html>

    效果:

    给子元素设置margin-top竟然把父元素也拖下来了

    解决方法就是在父元素加一行代码:overflow:hidden;

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box1,.box2{
                width: 200px;
                height: 200px;
                font-size: 100px;
            }
            .box1{
                background-color: red;
                /* margin-bottom: -50px; */
                overflow: hidden;
            }
            .box2{
                width: 100px;
                height: 100px;
                background-color: pink;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="box1"><div class="box2"></div></div>
        
    </body>
    </html>

    效果:

      

  • 相关阅读:
    20年美亚杯WRITE UP
    博客搬迁
    前端工程化1-模块
    移动端(h5)工具函数--视口适配(viewport)
    Understanding ES6 Modules(译)
    手写http请求并发、重试、超时函数
    两道编程题引发的思考
    前端基础走查(六):异步任务和事件循环
    vue 本地图片批量下载以及压缩成zip文件下载
    mybatis generator逆向工程
  • 原文地址:https://www.cnblogs.com/webpon/p/13466726.html
Copyright © 2011-2022 走看看