zoukankan      html  css  js  c++  java
  • css边距重叠的解决方案

    **

    css防止边距重叠的方法

    **

    今天整理了一下用css防止边距重叠的几种方法
    先假设一组dom结构

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

    通常情况下,如果给子元素设置margin,就会产生这个属性对父元素也产生了同样的效果,然而
    这其实不是我们想要的结果,我们只想对子元素设置margin,那么现在我们应该怎么做呢?
    (1) 给父元素设置边框

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

    (2)给父元素添加padding

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

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

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

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

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

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

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

    (6)使子元素脱离文档流这个实现的方法有很多,浮动,绝对定位等,这里我就不做具体的解释了。
    希望可以能帮助到需要的人,如果你觉得这个文章帮到你了,就麻烦动动小手点个赞吧!嘿嘿

  • 相关阅读:
    Keras学习笔记——Hello Keras
    记一次线上事故的JVM内存学习
    postgresql中的search_path
    CentOS7安装setuptools
    CentOS7安装EPEL的两种方式
    Ncures库的介绍与安装
    CentOs6.5 安装Zlib
    Centos 安装zlib
    Windows如何压缩tar.gz格式
    nginx运行文件出错env: /etc/init.d/nginx: No such file or directory
  • 原文地址:https://www.cnblogs.com/10manongit/p/12864678.html
Copyright © 2011-2022 走看看