首页 > 其他分享 >css边距合并的问题

css边距合并的问题

时间:2023-04-08 10:55:29浏览次数:41  
标签:color father 合并 height 边距 background 10px margin css

上下两个盒子边距合并问题

    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        div {
            width: 100px;
            height: 100px;
        }

        .top {
            background-color: red;
            margin-bottom: 20px;
        }

        .bottom {
            background-color: blue;
            margin-top: 10px;
        }
    </style>
    <div class="top"></div>
    <div class="bottom"></div>

效果如下

可以看到下面蓝色的外边距被上面的取代了,不是我们想象的那样 20+10 而是 20

结论:上下两个兄弟盒子的margin都为正取大,都为负取小,一正一负相加
注意:只有普通文档流中块框的垂直外边距才会发生外边距合并。

父子边距问题

<style>
        .father {
            width: 200px;
            height: 200px;
            background-color: red;
            /* overflow: auto; */
        }

        /* .father::before {
            display: table;
            content: "";
        } */

        .son {
            width: 50px;
            height: 50px;
            background-color: blue;
            margin-top: 10px;
        }
    </style>
    <div class="father">
        <div class="son"></div>
    </div>

效果如下

我们想象的结果是son这个盒子距离父盒子father顶部10px,但实际效果却father距离顶部10px。

这是因为父盒子没有设置外边距,边框,内边距 。当父盒子设置了这三者之一,都不会出现这样的效果

如果不想设置外边距,边框,内边距 又想到达同样的效果,可以这样做:

父元素{
	overflow:auto;
}
/*或者*/
父元素::before{
  display: table;
  content: "";
}

改正后:

    <style>
        .father {
            width: 200px;
            height: 200px;
            background-color: red;
            overflow: auto;
        }

        /* .father::before {
            display: table;
            content: "";
        } */

        .son {
            width: 50px;
            height: 50px;
            background-color: blue;
            margin-top: 10px;
        }
    </style>
    <div class="father">
        <div class="son"></div>
    </div>

效果如下

注意:只有普通文档流中块框的垂直外边距才会发生外边距合并。

标签:color,father,合并,height,边距,background,10px,margin,css
From: https://www.cnblogs.com/SYF--BLOG/p/17298143.html

相关文章

  • 颜色分类(数组、双指针)、排列序列(递归、数学)、合并区间(数组、排序)
    颜色分类(数组、双指针)给定一个包含红色、白色和蓝色,一共n__个元素的数组,**原地(https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)**对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数0、1和2分别表示红色、......
  • CSS标准盒子模型与怪异盒子模型
    <style>*{margin:0;padding:0;}div{/*宽高改变的是内容大小*/width:200px;height:200px;}/*w3c标准盒子:盒子的总大小=内容+内边距+边框+外边距*//*只要改变内容,内边距,外边距,边框,盒子都......
  • CSS伪类选择器
    伪类选择器选择第几个元素<style>/*类num后代li的第1个*/.numli:first-child{color:red;}/*类num后代li的最后一个*/.numli:last-child{color:red;}/*类num后代li的第......
  • CSS基础选择器
    基础选择器前端页面结构如下<h1>h1:标签选择器对选定的所有的标签都生效</h1><p>p:标签选择器对选定的所有的标签都生效</p><divclass="green">div:类选择器测试</div><divid="myid">id选择器测试,id是唯一的</div>效果展示如下标签选择器加上如下代码......
  • 完美的模板CSS
    body{ margin:0px; padding:0px;}.left{ background:#313541; width:20%; float:left; position:absolute; top:0px; bottom:0px;}.top{ width:80%; float:right; height:100px; line-height:100px; border-bottom:1pxsolid#b0cdff;}.content{ float:r......
  • 617. 合并二叉树
    给你两棵二叉树:root1和root2。想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为null的节点将直接作为新二叉树......
  • 区间合并 acwing803
    linkcode#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;intmain(){ intn; intans=1,tpr=0; vector<pair<int,int>>v; intl,r; cin>>n; for(inti=1;i<=n;i++){ cin>>l>>r;......
  • hdu-1540(线段树+区间合并)
    TunnelWarfareHDU-1540思路:没被摧毁的村庄为1,否则为0,用len记录线段树维护区间的两个信息:前缀最长1的序列pre后缀最长1的序列suf父节点与左右子节点的关系://lc为左节点,rc为右节点1.若左右结点都不满1,则tr[p].pre=tr[lc].pre,tr[p].suf=tr[rc].suf2.若左节点满1,tr......
  • css 实现文字交融展开动画
    css实现文字交融展开动画效果  ......
  • 直播平台制作,html+css复刻登录输入框
    直播平台制作,html+css复刻登录输入框 <!DOCTYPEhtml><html><!--  *Material文本框  *作者:程序员阿诺斯  *时间:2023-3-50:50--><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge"......