margin为负值产生的影响和常见布局应用

一、margin为负值产生的影响

对于自身的影响

当元素不存在width属性或者(width:auto)的时候,负margin会增加元素的宽度,看下下面的例子


    
    
  1. <div class= ”container”>
  2. <div class= ”box1” >
  3. I dont have the width
  4. </div>
  5. < /div>

    
    
  1. .container{
  2. margin:0 auto;
  3. width: 500px;
  4. border: 1px #ccc solid;
  5. margin-bottom: 20px;
  6. }
  7. .box1{
  8. margin-left: -20px;
  9. }

Paste_Image.png


可以看到box1增加了20px宽度,margin-left和margin-right都是可以增加宽度,

注意:
margin-top为负值不会增加高度,只会产生向上位移
margin-bottom为负值不会产生位移,会减少自身的供css读取的高度

为什么是供css读取的高度
我们再来做个实验


    
    
  1. <style>
  2. #box {
  3. width: 50% ;
  4. margin-bottom: -25px ;
  5. background-color: rgba(90, 243, 151, 0.8) ;
  6. height: 50px ;
  7. }
  8. </style>
  9. <div id="box">
  10. box
  11. </div>
  12. <script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.js"> </script>
  13. <script>
  14. var x = $( '#box' ).height()
  15. console .log(x);
  16. </script>

Paste_Image.png


下文的应用段落就会利用这个特点做一个多列等高布局。

对文档流的影响

元素如果用了margin-left:-20px;毋庸置疑的自身会向左偏移20px和定位(position:relative)有点不一样的是,在其后面的元素会补位,也就是后面的行内元素会紧贴在此元素的之后。总结,不脱离文档流不使用float的话,负margin元素是不会破坏页面的文档流。


对文档流的影响

所以如果你使用负margin上移一个元素,所有跟随的元素都会被上移。

对浮动元素的影响

先定义3个box


    
    
  1. <div class= "container">
  2. <div class= "flb flbox1">box1</div>
  3. <div class= "flb flbox2">box2</div>
  4. <div class= "flb flbox3">box3</div>
  5. </div>

    
    
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. }
  6. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  7. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  8. .flbox3{background-color: rgba(90, 243, 151, 0.8);}

Paste_Image.png

demo1,给他们都加上margin-left:-25px;


    
    
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. margin-left: -25px;
  6. }
  7. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  8. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  9. .flbox3{background-color: rgba(90, 243, 151, 0.8);}

margin-left: -25px;


可以看出3个盒子都向左移动25px;
box1自身向左移动了25px,box2又覆盖了其25px,所以我们就看到了“宽度”为50px的box1
box2,box3以此类推!

让我再看看margin-left: -50px;的情况


margin-left: -50px

如果只给box3设置margin-left:-200px;


    
    
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. }
  6. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  7. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  8. .flbox3{background-color: rgba(90, 243, 151, 0.8);
  9. margin-left: -200px;}

.flbox3{margin-left: -200px;}


看一看到box3,向左偏移了200px,完全将box1覆盖了,box3下面还能隐约的看到了box1。

总结:

负margin会改变浮动元素的显示位置,即使我的元素写在DOM的后面,我也能让它显示在最前面。圣杯布局、双飞翼布局啊什么的,都是利用这个原理实现的。(下文有详细)

对绝对定位影响

<div class="absolute"></div>
    
    

    
    
  1. .absolute{
  2. position: absolute;
  3. top:50%;
  4. left:50%;
  5. height: 200px;
  6. width: 200px;
  7. background-color: #ccc;
  8. margin-top: -100px;
  9. margin-left: -100px;
  10. }

一个居中的box


对于绝对定位元素,负margin会基于其绝对定位坐标再偏移,
唯有的缺点就是你必须知道这个觉得定位元素宽度的和高度才能并设置负margin值使其居中浏览器窗口,
若对于不确定宽度和高度可以用

transform: translate3d(-50%,-50%,0);
    
    

使用translate3d可以开启GPU加速,就不用cpu去从新计算所有点素位置,开启GPU加速后,GPU自动将这个元素放在一个新的“层”,直接偏移这个“层”来提高渲染速度(个人理解,若有错误欢迎指正)。

二、margin为负值的常见布局应用

左右固定,中间自适应(双飞翼)

双飞翼的好处:
1.可以让主要内容出现在dom结构的前面,现将主要内容渲染
2.中间只适应,两边固定宽度的效果


双飞翼

    
    
  1. <div class= "main">
  2. <div class= "main-content">main content</div>
  3. </div>
  4. <div class= "left">left</div>
  5. <div class= "right">right</div>

    
    
  1. *{
  2. margin:0;
  3. padding: 0
  4. }
  5. .main{
  6. float: left;
  7. width: 100%;
  8. }
  9. .main .main-content{
  10. margin: 0 210px;
  11. background-color: rgba(33, 114, 214, 0.8);
  12. height: 500px
  13. }
  14. .left{
  15. width: 200px;
  16. float: left;
  17. background-color: rgba(255, 82, 0, 0.8);
  18. margin-left: -100%;
  19. height: 200px
  20. }
  21. .right{
  22. width: 200px;
  23. height: 200px;
  24. margin-left: -200px;
  25. float: left;
  26. background-color: rgba(90, 243, 151, 0.8);
  27. }

去除列表右边框

利用负margin增加宽度的特点,举个在实际中应用例子��

对于图片列表,我会常常设计成两边对齐,中间元素平均分布,类似下面的布局


慕课网截图


想让每一排最后一个对齐父元素的右边框,一般都两种处理,第一种会在给个循环体内的最后一个添加一个float:right属性或者做特殊处理,但这样还需要后端配合,我们自己能解决的就尽量自己解决。还有就是用css3的flexbox布局能解决这个两边对齐,但是这种布局兼容性不好,你的用户用IE的话,不推荐!
so,负margin就能发挥其增加元素宽度的特点,完美的解决这个问题!


    
    
  1. <div class="container">
  2. <ul class="list">
  3. <li>我是一个列表 </li>
  4. <li>我是一个列表 </li>
  5. <li>我是一个列表 </li>
  6. <li>我是一个列表 </li>
  7. <li>我是一个列表 </li>
  8. <li>我是一个列表 </li>
  9. <li>我是一个列表 </li>
  10. <li>我是一个列表 </li>
  11. <li>我是一个列表 </li>
  12. <li>我是一个列表 </li>
  13. </ul>
  14. </div>

    
    
  1. .container{
  2. margin:0 auto;
  3. width: 500px;
  4. border: 1px #ccc solid;
  5. margin-bottom: 20px;
  6. }
  7. .list{
  8. overflow: hidden;
  9. margin-right: -10px;
  10. }
  11. .list li{
  12. width:92px;
  13. height: 92px;
  14. background-color: #ccc;
  15. margin-bottom: 20px;
  16. float: left;
  17. margin-right: 10px;
  18. }

图片列表

计算公式:
假设一横列展示的数量为x,元素见的间距为y,父距宽度z
则元素的宽度=(z-y(x-1))/x
本例中li的宽度为(500-10(5-1))/5=92

负边距+定位:水平垂直居中

上面已经举例,用了负margin会向对应方向偏移的特点!

去除列表最后一个li元素的border-bottom

很多情况下,我们会用li标签来模拟表格,再给li标签添加下边距的时候,最后一个li标签表格会和父元素的边框靠在一起,会显得整个“table”的底部边框会比其他的边粗一些!


    
    
  1. <style>
  2. ul .table {
  3. border:1px #ccc solid ;
  4. margin-top:100px ;
  5. }
  6. ul .table li {
  7. border-bottom: 1px #ccc solid ;
  8. list-style: none ;
  9. }
  10. </style>
  11. <ul class="table">
  12. <li>I am li </li>
  13. <li>I am li </li>
  14. <li>I am li </li>
  15. <li>I am li </li>
  16. <li>I am li </li>
  17. </ul>

没有设置margin-bottom:-1px


下面添加一个margin-bottom:-1px;的属性,就可以使其看起来更完美


    
    
  1. <style>
  2. ul .table {
  3. border:1px #ccc solid ;
  4. margin-top:100px ;
  5. }
  6. ul .table li {
  7. border-bottom: 1px #ccc solid ;
  8. list-style: none ;
  9. margin-bottom: -1px ;
  10. }
  11. </style>
  12. <ul class="table">
  13. <li>I am li </li>
  14. <li>I am li </li>
  15. <li>I am li </li>
  16. <li>I am li </li>
  17. <li>I am li </li>
  18. </ul>

设置margin-bottom:-1px

多列等高

利用margin-bottom为负值会减少css读取元素高度的特性,加上padding-bottom和overflow:hidden,就能做一个未知高度的多列等高布局(当然也可以用table)


    
    
  1. <style>
  2. .container {
  3. margin:0 auto ;
  4. width: 100% ;
  5. overflow: hidden ;
  6. }
  7. .left {
  8. height: 50px ;
  9. width: 33.33% ;
  10. margin-bottom: -5000px ;
  11. padding-bottom: 5000px ;
  12. float: left ;
  13. background-color: rgba(33, 114, 214, 0.8) ;
  14. }
  15. .main {
  16. height:100px ;
  17. margin-bottom: -5000px ;
  18. width: 33.33% ;
  19. float: left ;
  20. padding-bottom: 5000px ;
  21. background-color: rgba(255, 82, 0, 0.8) ;
  22. }
  23. .right {
  24. width: 33.33% ;
  25. float: left ;
  26. margin-bottom: -5000px ;
  27. padding-bottom: 5000px ;
  28. background-color: rgba(90, 243, 151, 0.8)
  29. }
  30. </style>
  31. <div class="container">
  32. <div class="left"> height:50px </div>
  33. <div class="main">height:100px </div>
  34. <div class="right">height:30px </div>
  35. </div>

多列等高


虽然设置了5000的内边距,但是我用-5000的外边距去抵消掉,所以对于父元素来说(上文所说的css能读取的高度),他还是原来的高度(但其自身实际高度为5000p x+本来高度),然后父元素在加上overflow:hidden;以最高的高度进行裁切,所以就有了看起来“等高”的3个div。

今天整理和实验了下负margin的原理和应用,希望你看了文章有所收获,欢迎交流!



参考资料



一、margin为负值产生的影响

对于自身的影响

当元素不存在width属性或者(width:auto)的时候,负margin会增加元素的宽度,看下下面的例子


  
  
  1. <div class= ”container”>
  2. <div class= ”box1” >
  3. I dont have the width
  4. </div>
  5. < /div>

  
  
  1. .container{
  2. margin:0 auto;
  3. width: 500px;
  4. border: 1px #ccc solid;
  5. margin-bottom: 20px;
  6. }
  7. .box1{
  8. margin-left: -20px;
  9. }

Paste_Image.png


可以看到box1增加了20px宽度,margin-left和margin-right都是可以增加宽度,

注意:
margin-top为负值不会增加高度,只会产生向上位移
margin-bottom为负值不会产生位移,会减少自身的供css读取的高度

为什么是供css读取的高度
我们再来做个实验


  
  
  1. <style>
  2. #box {
  3. width: 50% ;
  4. margin-bottom: -25px ;
  5. background-color: rgba(90, 243, 151, 0.8) ;
  6. height: 50px ;
  7. }
  8. </style>
  9. <div id="box">
  10. box
  11. </div>
  12. <script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.js"> </script>
  13. <script>
  14. var x = $( '#box' ).height()
  15. console .log(x);
  16. </script>

Paste_Image.png


下文的应用段落就会利用这个特点做一个多列等高布局。

对文档流的影响

元素如果用了margin-left:-20px;毋庸置疑的自身会向左偏移20px和定位(position:relative)有点不一样的是,在其后面的元素会补位,也就是后面的行内元素会紧贴在此元素的之后。总结,不脱离文档流不使用float的话,负margin元素是不会破坏页面的文档流。


对文档流的影响

所以如果你使用负margin上移一个元素,所有跟随的元素都会被上移。

对浮动元素的影响

先定义3个box


  
  
  1. <div class= "container">
  2. <div class= "flb flbox1">box1</div>
  3. <div class= "flb flbox2">box2</div>
  4. <div class= "flb flbox3">box3</div>
  5. </div>

  
  
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. }
  6. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  7. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  8. .flbox3{background-color: rgba(90, 243, 151, 0.8);}

Paste_Image.png

demo1,给他们都加上margin-left:-25px;


  
  
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. margin-left: -25px;
  6. }
  7. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  8. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  9. .flbox3{background-color: rgba(90, 243, 151, 0.8);}

margin-left: -25px;


可以看出3个盒子都向左移动25px;
box1自身向左移动了25px,box2又覆盖了其25px,所以我们就看到了“宽度”为50px的box1
box2,box3以此类推!

让我再看看margin-left: -50px;的情况


margin-left: -50px

如果只给box3设置margin-left:-200px;


  
  
  1. .flb{
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. }
  6. .flbox1{background-color: rgba(33, 114, 214, 0.8);}
  7. .flbox2{background-color: rgba(255, 82, 0, 0.8);}
  8. .flbox3{background-color: rgba(90, 243, 151, 0.8);
  9. margin-left: -200px;}

.flbox3{margin-left: -200px;}


看一看到box3,向左偏移了200px,完全将box1覆盖了,box3下面还能隐约的看到了box1。

总结:

负margin会改变浮动元素的显示位置,即使我的元素写在DOM的后面,我也能让它显示在最前面。圣杯布局、双飞翼布局啊什么的,都是利用这个原理实现的。(下文有详细)

对绝对定位影响

<div class="absolute"></div>
  
  

  
  
  1. .absolute{
  2. position: absolute;
  3. top:50%;
  4. left:50%;
  5. height: 200px;
  6. width: 200px;
  7. background-color: #ccc;
  8. margin-top: -100px;
  9. margin-left: -100px;
  10. }

一个居中的box


对于绝对定位元素,负margin会基于其绝对定位坐标再偏移,
唯有的缺点就是你必须知道这个觉得定位元素宽度的和高度才能并设置负margin值使其居中浏览器窗口,
若对于不确定宽度和高度可以用

transform: translate3d(-50%,-50%,0);
  
  

使用translate3d可以开启GPU加速,就不用cpu去从新计算所有点素位置,开启GPU加速后,GPU自动将这个元素放在一个新的“层”,直接偏移这个“层”来提高渲染速度(个人理解,若有错误欢迎指正)。

二、margin为负值的常见布局应用

左右固定,中间自适应(双飞翼)

双飞翼的好处:
1.可以让主要内容出现在dom结构的前面,现将主要内容渲染
2.中间只适应,两边固定宽度的效果


双飞翼

  
  
  1. <div class= "main">
  2. <div class= "main-content">main content</div>
  3. </div>
  4. <div class= "left">left</div>
  5. <div class= "right">right</div>

  
  
  1. *{
  2. margin:0;
  3. padding: 0
  4. }
  5. .main{
  6. float: left;
  7. width: 100%;
  8. }
  9. .main .main-content{
  10. margin: 0 210px;
  11. background-color: rgba(33, 114, 214, 0.8);
  12. height: 500px
  13. }
  14. .left{
  15. width: 200px;
  16. float: left;
  17. background-color: rgba(255, 82, 0, 0.8);
  18. margin-left: -100%;
  19. height: 200px
  20. }
  21. .right{
  22. width: 200px;
  23. height: 200px;
  24. margin-left: -200px;
  25. float: left;
  26. background-color: rgba(90, 243, 151, 0.8);
  27. }

去除列表右边框

利用负margin增加宽度的特点,举个在实际中应用例子��

对于图片列表,我会常常设计成两边对齐,中间元素平均分布,类似下面的布局


慕课网截图


想让每一排最后一个对齐父元素的右边框,一般都两种处理,第一种会在给个循环体内的最后一个添加一个float:right属性或者做特殊处理,但这样还需要后端配合,我们自己能解决的就尽量自己解决。还有就是用css3的flexbox布局能解决这个两边对齐,但是这种布局兼容性不好,你的用户用IE的话,不推荐!
so,负margin就能发挥其增加元素宽度的特点,完美的解决这个问题!


  
  
  1. <div class="container">
  2. <ul class="list">
  3. <li>我是一个列表 </li>
  4. <li>我是一个列表 </li>
  5. <li>我是一个列表 </li>
  6. <li>我是一个列表 </li>
  7. <li>我是一个列表 </li>
  8. <li>我是一个列表 </li>
  9. <li>我是一个列表 </li>
  10. <li>我是一个列表 </li>
  11. <li>我是一个列表 </li>
  12. <li>我是一个列表 </li>
  13. </ul>
  14. </div>

  
  
  1. .container{
  2. margin:0 auto;
  3. width: 500px;
  4. border: 1px #ccc solid;
  5. margin-bottom: 20px;
  6. }
  7. .list{
  8. overflow: hidden;
  9. margin-right: -10px;
  10. }
  11. .list li{
  12. width:92px;
  13. height: 92px;
  14. background-color: #ccc;
  15. margin-bottom: 20px;
  16. float: left;
  17. margin-right: 10px;
  18. }

图片列表

计算公式:
假设一横列展示的数量为x,元素见的间距为y,父距宽度z
则元素的宽度=(z-y(x-1))/x
本例中li的宽度为(500-10(5-1))/5=92

负边距+定位:水平垂直居中

上面已经举例,用了负margin会向对应方向偏移的特点!

去除列表最后一个li元素的border-bottom

很多情况下,我们会用li标签来模拟表格,再给li标签添加下边距的时候,最后一个li标签表格会和父元素的边框靠在一起,会显得整个“table”的底部边框会比其他的边粗一些!


  
  
  1. <style>
  2. ul .table {
  3. border:1px #ccc solid ;
  4. margin-top:100px ;
  5. }
  6. ul .table li {
  7. border-bottom: 1px #ccc solid ;
  8. list-style: none ;
  9. }
  10. </style>
  11. <ul class="table">
  12. <li>I am li </li>
  13. <li>I am li </li>
  14. <li>I am li </li>
  15. <li>I am li </li>
  16. <li>I am li </li>
  17. </ul>

没有设置margin-bottom:-1px


下面添加一个margin-bottom:-1px;的属性,就可以使其看起来更完美


  
  
  1. <style>
  2. ul .table {
  3. border:1px #ccc solid ;
  4. margin-top:100px ;
  5. }
  6. ul .table li {
  7. border-bottom: 1px #ccc solid ;
  8. list-style: none ;
  9. margin-bottom: -1px ;
  10. }
  11. </style>
  12. <ul class="table">
  13. <li>I am li </li>
  14. <li>I am li </li>
  15. <li>I am li </li>
  16. <li>I am li </li>
  17. <li>I am li </li>
  18. </ul>

设置margin-bottom:-1px

多列等高

利用margin-bottom为负值会减少css读取元素高度的特性,加上padding-bottom和overflow:hidden,就能做一个未知高度的多列等高布局(当然也可以用table)


  
  
  1. <style>
  2. .container {
  3. margin:0 auto ;
  4. width: 100% ;
  5. overflow: hidden ;
  6. }
  7. .left {
  8. height: 50px ;
  9. width: 33.33% ;
  10. margin-bottom: -5000px ;
  11. padding-bottom: 5000px ;
  12. float: left ;
  13. background-color: rgba(33, 114, 214, 0.8) ;
  14. }
  15. .main {
  16. height:100px ;
  17. margin-bottom: -5000px ;
  18. width: 33.33% ;
  19. float: left ;
  20. padding-bottom: 5000px ;
  21. background-color: rgba(255, 82, 0, 0.8) ;
  22. }
  23. .right {
  24. width: 33.33% ;
  25. float: left ;
  26. margin-bottom: -5000px ;
  27. padding-bottom: 5000px ;
  28. background-color: rgba(90, 243, 151, 0.8)
  29. }
  30. </style>
  31. <div class="container">
  32. <div class="left"> height:50px </div>
  33. <div class="main">height:100px </div>
  34. <div class="right">height:30px </div>
  35. </div>

多列等高


虽然设置了5000的内边距,但是我用-5000的外边距去抵消掉,所以对于父元素来说(上文所说的css能读取的高度),他还是原来的高度(但其自身实际高度为5000p x+本来高度),然后父元素在加上overflow:hidden;以最高的高度进行裁切,所以就有了看起来“等高”的3个div。

今天整理和实验了下负margin的原理和应用,希望你看了文章有所收获,欢迎交流!



参考资料



猜你喜欢

转载自blog.csdn.net/qq_39536158/article/details/81669381