美文网首页
margin为负值所产生的影响

margin为负值所产生的影响

作者: 埃米莉Emily | 来源:发表于2017-08-09 23:02 被阅读30次

一、margin为负值产生的影响

对于自身的影响

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

<div class="container">
  <div class="box1">
         I dont have the width
    </div>
</div>
.container{
  margin:0 auto;
  width: 500px;
  border: 1px #ccc solid;
  margin-bottom: 20px;
  background-color: pink;
}
.box1{
  margin-left: -20px;
}
self.png

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

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

什么是供css读取的高度?

<style>
    #box {
        width: 50%;
        margin-bottom: -25px;
        background-color: rgba(90, 243, 151, 0.8);
        height: 50px;
    }
    </style>
    <div id="box">
        box
    </div>
    <script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.js"></script>
    <script>
    var x = $('#box').height()
    console.log(x);
    </script>
css_height.png

对文档流的影响

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

对浮动元素的影响

定义3个box

<div class="container">
    <div class="flb flbox1">box1</div>
    <div class="flb flbox2">box2</div>
    <div class="flb flbox3">box3</div>
</div>
.flb{
    float: left;
    width: 100px;
    height: 100px;
}
.flbox1{background-color: rgba(33, 114, 214, 0.8);}
.flbox2{background-color: rgba(255, 82, 0, 0.8);}
.flbox3{background-color: rgba(90, 243, 151, 0.8);}
float.png

加上margin-left:-25px;

.flb{
    float: left;
    width: 100px;
    height: 100px;
    margin-left: -25px;
}
.flbox1{background-color: rgba(33, 114, 214, 0.8);}
.flbox2{background-color: rgba(255, 82, 0, 0.8);}
.flbox3{background-color: rgba(90, 243, 151, 0.8);}
add_margin.png

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

负margin会改变浮动元素的显示位置。

对绝对定位影响

<div class="absolute"></div>
.absolute{
    position: absolute;
    top:50%;
    left:50%;
    height: 200px;
    width: 200px;
    background-color: #ccc;
    margin-top: -100px;
    margin-left: -100px;
}
一个居中的box.png

对于绝对定位元素,负margin会基于其绝对定位坐标再偏移

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

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

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

双飞翼.png
<div class="main">
   <div class="main-content">main content</div>
</div>
<div class="left">left</div>
<div class="right">right</div> 
*{
     margin:0;
     padding: 0
 }
 .main{
     float: left;
     width: 100%;
 }
 .main .main-content{
     margin: 0 210px;
     background-color: rgba(33, 114, 214, 0.8);
     height: 500px
 }
 .left{
     width: 200px;
     float: left;
     background-color: rgba(255, 82, 0, 0.8);
     margin-left: -100%; 
     height: 200px
  }
  .right{
      width: 200px;
      height: 200px;
      margin-left: -200px;
      float: left;
      background-color: rgba(90, 243, 151, 0.8);
  } 

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

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

<style>
    ul.table{
        border:1px #ccc solid;
        margin-top:100px;
    }
    ul.table li{
        border-bottom: 1px #ccc solid;
        list-style: none;
    }
    </style>
    <ul class="table"> 
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
    </ul>
border.png

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

<style>
    ul.table{
        border:1px #ccc solid;
        margin-top:100px;
    }
    ul.table li{
        border-bottom: 1px #ccc solid;
        list-style: none;
        margin-bottom: -1px;
    }
    </style>
    <ul class="table"> 
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
        <li>I am li</li>
    </ul>
add_margin.png

多列等高

<style>
    .container{
        margin:0 auto;
        width: 100%;
        overflow: hidden;
    }
    .left{
        height: 50px;
        width: 33.33%;
        margin-bottom: -5000px;
        padding-bottom: 5000px;
        float: left;
        background-color: rgba(33, 114, 214, 0.8);
    }
    .main{
        height:100px;
        margin-bottom: -5000px;
        width: 33.33%;
        float: left;
        padding-bottom: 5000px;
        background-color: rgba(255, 82, 0, 0.8);
    }
    .right{
        width: 33.33%;
        float: left;
        margin-bottom: -5000px;
        padding-bottom: 5000px;
        background-color: rgba(90, 243, 151, 0.8)
    }
</style>
<div class="container">
    <div class="left"> height:50px</div>
    <div class="main">height:100px</div>
    <div class="right">height:30px</div>
</div>
多列等高.png

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

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

相关文章

  • margin为负值所产生的影响

    一、margin为负值产生的影响 对于自身的影响 当元素不存在width属性或者(width:auto)的时候,负...

  • css 负边距

    可以通过负值的margin来抵消掉padding的影响 1.margin:负值 当给一个元素设置margin 负值...

  • css杂项

    bo一、margin负值 margin-left / margin-top为负值,当前元素会向左 / 上拖动 ...

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

    [TOC] 前几天去了一家公司面试前端,问了我双飞翼的布局,说实话,之前真没好好研究过实现原理。面试回来,查了下,...

  • 前端记录20171208

    margin 设置为负值,元素将会变大

  • margin为负值的拓展

    css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果。很...

  • margin为负值的分析

    引子 解决一个问题的关键在于马上着手去解决这个问题。我们都觉得CSS难学,那么就抓紧时间去学,去学,去学 marg...

  • margin为负值的规则

    元素的margin为负值时有一些特定的规则: A negative margin to the left or t...

  • margin负值

    margin-left和margin-right负值会增加元素的宽度(当元素不存在width属性或者width属性...

  • task 12

    负边距在让元素产生偏移时和position: relative有什么区别?负边距通过元素的margin值为负值让元...

网友评论

      本文标题:margin为负值所产生的影响

      本文链接:https://www.haomeiwen.com/subject/ayqcrxtx.html