margin负值的应用

一、圣杯布局和双飞翼布局

  1. 双飞翼布局
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双飞翼布局</title>
    <style type="text/css">
        .center {
            width: 100%;
            float: left;
        }
            .center-content {
                margin: 0 220px;
                height: 300px;
                background-color: #ffa;
            }
        .left {
            margin-left: -100%;
            float: left;
            width: 200px;
            height: 300px;
            background-color: #de6ada;
        }
        .right {
            margin-left: -200px;
            float: right;
            width: 200px;
            height: 300px;
            background-color: #98a3df;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center">
            <div class="center-content"></div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>
  1. 圣杯布局
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
    <style type="text/css">
    .container {
        padding: 0 220px;
    }
        .center {
            float: left;
            width: 100%;
            height: 300px;
            background-color: #ffa;
        }
        .left {
            position: relative;
            left: -210px;
            margin-left: -100%;
            float: left;
            width: 200px;
            height: 300px;
            background-color: #de6ada;
        }
        .right {
            position: relative;
            left: 210px;
            margin-left: -200px;
            float: right;
            width: 200px;
            height: 300px;
            background-color: #98a3df;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="center"></div>
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>


这里写图片描述

二、去除列表的右边框

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <style>
    ul,li {
      margin: 0;
      padding: 0;
    }
    .container {
      margin: 100px auto;
      width: 320px;
      border: 1px solid #000;
      overflow: hidden;
    }
      .container-ul {
        list-style-type: none;
        /* margin-right: -10px; */
      }
      .container-li {
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        width: 100px;
        height: 100px;
        background-color: aqua;
      }
  </style>
</head>
<body>
  <div class="container">
    <ul class="container-ul">
      <li class="container-li"></li>
      <li class="container-li"></li>
      <li class="container-li"></li>
      <li class="container-li"></li>
      <li class="container-li"></li>
      <li class="container-li"></li>
    </ul>
  </div>
</body>
</html>

这里写图片描述
这里3应该在红色框这这里,就不会造成空间浪费,做法是ul:margin-right: -10px,就是把上面的注释代码回复。
结果:
这里写图片描述

三、去除列表最后一项下边框重合问题

这里写图片描述

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <style>
    ul,li {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    .container {
      margin: 100px 220px;
    }
      ul {
        width: 300px;
        border: 1px solid #000;
      }
      li {
        height: 50px;
        line-height: 50px;
        text-align: center;
        background-color: aliceblue;
        border-bottom: 1px solid #000;
      }
      li:nth-last-child(1) {
        margin-bottom: -1px;
      }
  </style>
</head>
<body>
  <div class="container">
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>
  </div>
</body>
</html>

在列表最后一项加:margin-bottom: -1px

这里写图片描述

四、多列等高

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

<!DOCTYPE html>
<html>
  <head>
    <style>
      .container {
        overflow: hidden;
      }
      .div1 {
        margin-bottom: -200px;
        padding-bottom: 200px;
        float: left;
        width: 30%;
        height: 100px;
        background-color: beige;
      }
      .div2 {
        margin-bottom: -200px;
        padding-bottom: 200px;
        float: left;
        width: 30%;
        height: 50px;
        background-color: aquamarine;
      }
      .div3 {
        margin-bottom: -200px;
        padding-bottom: 200px;
        float: left;
        width: 30%;
        height: 200px;
        background-color: blue;
      }
    </style>
  </head>
<body>
<div class="container">
  <div class="div1">heigth: 100px</div>
  <div class="div2">height: 50px</div>
  <div class="div3">height: 200px</div>
</div>
</body>
</html>

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值