css使多个相互之间有间隔的元素两端对齐显示

场景重现

这里写图片描述
电商网站经常会出现如下布局来展示商品图片,每行4个,每个和每个之间有20px的距离,两端对齐,假设总宽度为500px

效果列举

  • 失败效果
    这里写图片描述

  • 成功效果
    这里写图片描述

代码示例

方法一:width: 25%; margin-right: -20px;

说明:每个大的item宽度25%,那么4个正好排成一行,item里面的div右边距20px,最后设置最大的包裹div右边距-20px,使右边界对齐

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>demo</title>
  <style>
    * {
        margin: 0;
        padding: 0;
    }
    body, html {
        width: 100%;
        height: 100%;
    }
    .ctn {
        width: 500px;
        height: 300px;
        background: grey;
    }
    .hidden {
        overflow: hidden;
    }
    .item-ctn {
        margin-right: -20px;
        margin-bottom: -20px;
        background: blue;
    }
    .item-ctn:after {
        content: '';
        display: block;
        clear: both;
    }
    .item {
        float: left;
        width: 25%;
        height: 100px;
        margin-bottom: 20px;
    }
    .item-ctx {
        margin-right: 20px;
        height: 100%;
        background: red;
    }
  </style>
</head>
<body>
    <div class="ctn">
        <div class="hidden">
            <div class="item-ctn">
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
                <div class="item">
                    <div class="item-ctx">
                        <p>1</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
方法二calcwidth: calc((100% - 20px*3)/4); .item:nth-child(4n)

说明:使用css3新属性calc布局(注意不同浏览器的兼容前缀)
* 设置宽度

<style>
  * {
    margin: 0;
    padding: 0;
  }

  body,
  html {
    width: 100%;
    height: 100%;
  }

  .ctn {
    width: 500px;
    height: 300px;
    background: grey;
  }

  .hidden {
    overflow: hidden;
  }

  .item-ctn {
    margin-bottom: -20px;
    background: blue;
  }

  .item-ctn:after {
    content: '';
    display: block;
    clear: both;
  }

  .item {
    float: left;
    width: calc((100% - 20px*3)/4);
    margin-right: 20px;
    height: 100px;
    margin-bottom: 20px;
  }

  .item:nth-child(4n) {
    margin-right: 0;
  }

  .item-ctx {
    height: 100%;
    background: red;
  }
</style>
方法三flexdisplay: flex; justify-content: space-between;

说明:使用flex布局,子元素间隔且两端对齐即可(注意不同浏览器的兼容前缀)

<style>
  * {
    margin: 0;
    padding: 0;
  }

  body,
  html {
    width: 100%;
    height: 100%;
  }

  .ctn {
    width: 500px;
    height: 300px;
    background: grey;
  }

  .hidden {
    overflow: hidden;
  }

  .item-ctn {
    margin-bottom: -20px;
    background: blue;
    /* 弹性盒子 */
    display: flex;
    /* 沿行轴线两端对齐,子元素之间有间隙 */
    justify-content: space-between;
    /* 子元素溢出父容器时换行 */
    flex-flow: row wrap;
  }

  .item {
    width: 110px;
    height: 100px;
    margin-bottom: 20px;
  }

  .item-ctx {
    height: 100%;
    background: red;
  }
</style>
方法四griddisplay: grid; justify-content: space-between;

说明:使用网格布局,子元素间隔且两端对齐即可(注意不同浏览器的兼容前缀)

<style>
  * {
    margin: 0;
    padding: 0;
  }

  body,
  html {
    width: 100%;
    height: 100%;
  }

  .ctn {
    width: 500px;
    height: 300px;
    background: grey;
  }

  .hidden {
    overflow: hidden;
  }

  .item-ctn {
    margin-bottom: -20px;
    background: blue;
    /* 网格布局 */
    display: grid;
    /* 定义网格的行和列 */
    grid-template: auto / 110px 110px 110px 110px;
    /* 沿行轴线两端对齐,子元素之间有间隙 */
    justify-content: space-between;
  }

  .item {
    width: 110px;
    height: 100px;
    margin-bottom: 20px;
  }

  .item-ctx {
    height: 100%;
    background: red;
  }
</style>
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值