margin重叠与line-height属性

line-height
line-height代表行高
蓝色背景部分为行高
line-height:2em
相邻兄弟元素margin重叠
蓝色与蓝色之间的白色部分为
margin-bottom:1em;
margin-top:1em;
因为发生了margin-top与margin-bottom重叠
所以只有1em的留白

 

<!DOCTYPE html>
<html>
      <head>
            <meta charset="utf-8" />
            <title></title>
            <style type="text/css">
            p{
                  line-height: 2em;
                  margin: 1em 0;
                  background: lightseagreen;
            }

            </style>
      </head>
      <body>
            <p>第一行</p>
            <p>第二行</p>
      </body>
</html>
父类元素与子元素margin重叠    
条件  
  1. 父类元素没有设置border-top值
  2. 父类元素没有设置padding-top值
 
子元素设置margin-top等于20px
 
父类元素设置margin-top等于20px
 

 

父类元素与子元素同时设置margin-top等于20px, 

 

此时父类元素的margin-top与子元素margin-top发生重叠
<!DOCTYPE html>
<html>
      <head>
            <meta charset="utf-8" />
            <title></title>
            <style type="text/css">
            body{
                  background: lightblue;
            }
            #father{
                  background: lightcoral;
                  margin-top:20px;
            }
            #son{
                  margin-top:20px;//上外边距等于20px
            }
            </style>
      </head>
      <body>
            <div id="father">
                  <div id="son">
                        my name is son
                  </div>
            </div>
      </body>
</html>
当第一行的margin-bottom等于50px,
第二行的margin-top等于30px,
这时它们两个之间的间距取较大值50px
当一个值为正数,一个为负值时,
间距取它们两个值的和
当两个值都为负值,取它们其中绝对值较大的负值。

 

<!DOCTYPE html>
<html>
      <head>
            <meta charset="utf-8" />
            <title></title>
            <style type="text/css">
            .first{
                  margin-bottom: 50px;
                  background: lightblue;
            }
            .second{
                  margin-top: 30px;
                  background: lightcoral;
            }
            </style>
      </head>
      <body>
            <p class="first">第一行</p>
            <p class="second">第二行</p>
      </body>
</html>

 

转载于:https://www.cnblogs.com/yatao/p/5844942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改这段代码,实现图片点击切换效果@charset "utf-8";* { margin: 0; padding: 0; list-style: none; }html { height: 100%;}body { width: 100%; height: 100%; margin: 0; display: flex; flex-direction: column;}.class1 { height: 70px; width: 100%; background: #ccc; }.class2 { height: 50px; width: 100%; background: #ffaa00;}.class3 { width: 100%; }header{ background-color: #00ffff; height: 70px; width: 1200px; margin: 0 auto; }.logo{ width: 200px; height: 65px; background-color: #16A1E7; float: left; padding-top: 5px; } .logo.p{ line-height: 10px; } .search{ width: 800px; height: 70px; float: left; } /* 搜索框在导航中居中 */ header .search form{ margin-left: 150px; margin-top: 20px; } /* 输入框样式 */ header .search input{ height:36px; border:none; float:left; } header .search input[type=text]{ width:300px; border:0.5px solid #999; padding:0px 5px; } /* 输入框 */ header .search input[type=submit]{ background-color:#16A1E7; color:#fff; width:100px; font-size:14px; font-family:"微软雅黑";} .reg{ width: 200px; height: 70px; background-color: #ffaa7f; float: left; } .reg a{ text-decoration: none; } .delu{ margin: 25px 0px 0px 120px; } .nav{ width: 100%; height: 50px; border-bottom: 2px solid #0099CC; display: flex; } ul{ background-color: #ffffff; list-style-type: none; overflow: hidden; width: 1200px; margin: 0 auto; } li{ /*display与float两种方式使列表转为横向*/ /* display: inline; */ float: left; } li a{ display:block ; color: #000000; text-decoration: none; text-align: center; padding: 14px 46px; font-size: 16px; } li a:hover{ background-color:#000000 ; color: #ffffff; } .active{ background-color: #00aaff; } .sildeshow > *{ position: absolute; } .href{ z-index: 100; margin-top: 470px; } .href a{ display: block; width: 80px; height: 30px; background-color: #333; float: left; margin-left: 25px; } .images > li img{ width: 500px; height: 500px; }
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值