css负边距margin

关于负margin的原理,参见文章:
http://www.cnblogs.com/2050/archive/2012/08/13/2636467.html#2457812
应用:
http://www.cnblogs.com/jscode/archive/2012/08/28/2660078.html

应用之一:等高布局(其他方法可查看
http://blog.csdn.net/charlene0824/article/details/51222502

<div class="container">
    <div class="item1">该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 </div>
    <div class="item2">该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 </div>
</div>

css:

.container{
        width:800px;
        background-color: #98F59E;
        overflow: hidden;

    }
    .item1,.item2{
        float:left;
        width:300px;
        background-color: #96FCEF;
        word-wrap: break-word;
        margin-left:10px;
        margin-bottom: -10000px;
        padding-bottom:10000px;
        border: 2px solid #A7525C;
    }

运行结果:
这里写图片描述

该方法的缺点是等高的div没有底边的边框。常用的解决方式是使用背景图来模仿底部边框或使用div来模仿列的边框。下面主要介绍第二种方法,第二种方法主要是在等高元素中加入一个相对于容器绝对定位的div元素,并设置其高度为边框高度

<div class="container">
    <div class="item1">该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的
    <div class='colBottom1'></div>
    </div>
    <div class="item2">该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的 该方法是通过负margin,同时将padding的值设为正的margin值实现的
    <div class='colBottom2'></div>
    </div>
</div>

<style type="text/css">
.container{
        width:800px;
        background-color: #98F59E;
        overflow: hidden; 
        position: relative;

    }
    .item1,.item2{
        float:left;
        width:300px;
        background-color: #96FCEF;
        word-wrap: break-word;
        margin-left:10px;
        margin-bottom: -10000px;
        padding-bottom:10000px;
        border: 2px solid #A7525C;

    }
    .colBottom1{
        position: absolute;
        bottom: 0;
        left: 10px;
        height: 2px;
        width: 304px;
        background-color:#A7525C; 

    }
    .colBottom2{
        position: absolute;
        bottom: 0;
        left: 324px;
        height: 2px;
        width: 304px;
        background-color:#A7525C; 

    }
</style>

其运行结果如下
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值