flex布局四等分|2×2布局,并且有固定值间隙

flex布局四等分|2×2布局,并且有固定值间隙

  • 废话少说,看图:
    布局图片
  • 难点:
    1. 如何留下固定10px间隙?
    2. 如何向四个方向靠拢?
    3. 如何去除border的影响?
    4. 如何优雅的写代码?
  • 对应上边问题的解决方法:
    1. CSS3的calcheight:calc(50% - 5px),注意两个5px=10px
    2. flex布局,给主轴,副轴都设置space-between
    3. 给子元素box-sizing: border-box;让百分比等于实际大小,(相当于把border放在了盒子内部)
    4. 以上所有技巧均为CSS3
  • 全部代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        height: 500px;
      }
      .temp {
        /* basic */
        margin: 0 auto;
        padding: 0;
        height: 100%;
        width: 100%;
        background-color: pink;
        /* flex布局 */
        display: flex;
        /* 主轴居中(由于) */
        /* justify-content: center; */
        /* 设定主轴为横轴 默认row,还可以column |row-reverse|column-reverse */
        flex-direction: row;
        /* 主轴上的对齐方式 */
        justify-content: space-between;
        /* 换行(类似浮动布局) */
        flex-wrap: wrap;
        /* 副轴居中 */
        /* align-items: center; */
        align-content: space-between;
      }
      .box {
        border: 2px solid black;
        border-radius: 5px;
        box-sizing: border-box;
        width: calc(50% - 5px);
        height: calc(50% - 5px);
      }
    </style>
  </head>
  <body>
    <div class="temp">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </body>
</html>

  • 简写版代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        height: 500px;
      }
      .temp {
        /* basic */
        margin: 0 auto;
        padding: 0;
        height: 100%;
        width: 100%;
        background-color: pink;

        /* flex布局 */
        display: flex;
        /* 主轴上的对齐方式 */
        justify-content: space-between;
        /* 换行(类似浮动布局) */
        flex-wrap: wrap;
        /* 副轴上的对齐方式 */
        align-content: space-between;
      }
      .box {
        border: 2px solid black;
        border-radius: 5px;
        box-sizing: border-box;
        /* calc为CSS3内容 */
        width: calc(50% - 5px);
        height: calc(50% - 5px);
      }
    </style>
  </head>
  <body>
    <div class="temp">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值