知识库

margin负值使用之八大绝技

来源: 本站    类别: 知识库    日期: 2014/6/7

 

相信关于margin负值的使用,每个人都有自己的一套方法。在这里,整理了一下平时常用的一些margin负值制作的效果,与大家分享!

先来看一下margin负值的原理解析图:

当给一个元素设置margin 负值(top/left),该元素将在该方向上产生位移。例如:

 /* 元素向上位移10px */  

.demo {margin-top:-10px;}   

二、当给一个元素设置margin负值(bottom/right),这个元素并不会像你所预想的产生位移,而是将任何紧随其后的元素“拉”过来,覆盖在自己的上边。例如:

/* 所有紧随元素demo之后的元素向上位移10px */  

.demo {margin-bottom:-10px;}   

注:如果元素没有设置宽度,那么给该元素设置margin负值(left/right)将会在两个方向增大其宽度,看起来就好像给该元素添加了padding。 

【案例解析】

一、三栏显示(无需浮动及额外标签);

列表过长时,我们一般都会考虑通过浮动来使之显示为多列,其实换个思路也可以实现。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>negative margin 之列表三栏显示</title>
    <style>
    *{margin:0;padding:0;font-size: 14px;}
    .goback{clear:both;height:30px;line-height:30px;text-align: center;}

    ul {list-style:none;}
    li {width:100px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;line-height:1.3em;}
    .col2 {margin-left:120px;}
    .col3 {margin-left:240px;}
    .top {margin-top:-2.6em;}
    </style>
</head>
<body>
    <ul>
        <li class="col1">10家国企年招待费超29亿 被指仅用于吃喝</li>
        <li class="col1">媒体称中国豪车市场因政府严控公车快速萎缩</li>
        <li class="col2 top">菲发言人笑答射杀台渔民事件</li>
        <li class="col2">情妇打越洋电话提供证据</li>
        <li class="col3 top">雷政富涉嫌受贿罪被检察机关提起公诉</li>
        <li class="col3">日称冲绳久米岛附近海域发现疑似中国海军潜艇</li>
    </ul>
</body>
</html>

二、叠加效果;

最常见的选项卡,当前项选中状态;

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>negative margin 之叠加</title>
    <style>
    *{list-style:none;margin:0;padding:0;font-size: 12px;}
    .clearfix:after {
    clear: both;
    display: block;
    height: 0;
    line-height: 0;
    visibility: hidden;
    content: "";
}
    .clearfix {*zoom:1;}
    .goback{clear:both;height:30px;line-height:30px;text-align: center;}
    
    .demo{width:400px;}
    .tab{
    border-bottom:1px solid #66CC66;
    }
    .tab li{
    float:left;
    display:inline;
    margin-left:8px;
    }
    .tab li a{
    background-color:#CCFFCC;
    border:1px solid #66CC66;
    color:#666;
    display:block;
    margin-bottom:-1px;
    padding:0 5px;
    line-height:20px;
    float:left;
    font-weight:bold;
    text-decoration:none;
    }
    .tab li .current,
    .tab li a:hover{
    background-color:#fff;
    border-bottom:1px solid #fff;
    _position:relative;
    }
    .cont{padding:10px;border:1px solid #6c6;border-top:0;}
    </style>
</head>
<body>
    <div class="demo">
        <div id="demo2">
                    <ul class="tab clearfix">
                        <li><a href="#" class="current">Tab One</a></li>
                        <li><a href="#">Tab Two</a></li>
                        <li><a href="#">Tab Three</a></li>
                        <li><a href="#">Tab Four</a></li>
                    </ul>
                </div>
        <div class="cont">荒凉的旷野,一群暴虐的劫匪正在鞭挞一位美丽的姑娘玛丽亚(萝蕾丹娜·卡波莱特 Loredana Cappelletti饰)。幸得迪亚戈(弗兰科·尼罗 Franco Nero饰)的解救。他神秘而冷峻,拖着一副棺材漫步在泥泞的荒野上。迪亚戈带着玛丽亚来到荒凉的小镇,人们对他的到来充满好奇,更对这副棺材里隐藏的秘密而惶惶不安。</div>
    </div>
</body>
</html>

三、两列流式布局;

固定宽度的布局so easy,配合浮动轻松搞定两列自适应布局,三列同理。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>negative margin 之两列自适应布局</title>
    <style>
    *{list-style:none;margin:0;padding:0;font-size: 12px;}
    .goback{clear:both;height:30px;line-height:30px;text-align: center;}
    
    .layout{width:960px;margin:0 auto;background:#f5f5f5;}
    .main{float:left;width:100%;height:300px;margin:0 -200px 0 0;background:#ccc;}
    .main-wrap{margin:0 210px 0 0;}
    .aside{float:left;width:200px;height:300px;background:#f5f5f5;}
    </style>
</head>
<body>
    <div class="layout">
        <div class="main">
            <div class="main-wrap">main</div>
        </div>
        <div class="aside">aside</div>
    </div>
</body>
</html>

四、去除多余的外边距;

图文混排,每行最后一个元素的margin值如何处理?单独设置类名消0?通过父层来裁切?使用margin负值吧!

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>negative margin 之去除多余外边距</title>
    <style>
    *{list-style:none;margin:0;padding:0;font-size: 12px;}
    .clearfix:after {
    clear: both;
    display: block;
    height: 0;
    line-height: 0;
    visibility: hidden;
    content: "";
}
    .clearfix {*zoom:1;}
    .goback{clear:both;height:30px;line-height:30px;text-align: center;}
    
    .demo{width:320px;margin:0 auto;padding:10px 0;*overflow:hidden;background:green;}
    .figure-list{margin:0 -10px 0 0;}
    .figure-list li{float:left;width:100px;height:100px;margin:0 10px 10px 0;background:#f5f5f5;}
    </style>
</head>
<body>
    <div class="demo">
        <ul class="figure-list clearfix">
            <li>item-1</li>
            <li>item-2</li>
            <li>item-3</li>
            <li>item-4</li>
            <li>item-5</li>
            <li>item-6</li>
        </ul>
    </div>
</body>
</html>

相关文章


Copyright © 2004 - 2024 CNHWW Inc. All Rights Reserved
石家庄市征红网络科技有限公司版权所有 邮政编码:050051
服务电话:0311-85315152 13931185013 在线客服QQ:81447932 / 81447933 邮箱: cnhww@163.com