flex布局实现两行两列

利用flex布局,实现两行两列效果:

父元素

section {

        display: flex;

        width: 100%;

        height: 100px;

        border-radius: 4px;

        margin-top: 20px;

        justify-content: space-around;

        align-items: center;

        flex-wrap: wrap;  //设置换行

        gap: 10px;          //每个元素间隔10px

      }

子元素

      .section:nth-child(1) {

        display: flex;

        border-radius: 5px;

        background-color: rgb(189, 231, 241);

        justify-content: space-around;

        align-items: center;

        width: calc(50% - 20px);   //设置所占宽度50%,再减去20px 

        padding: 10px;                 

        box-sizing: border-box;    //设置后宽高不变,不会因为间隔而变小

      }

完整代码:直接找一个html文件粘贴看效果 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
    <style>
      /* 在此处补充代码*/
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      section {
        display: flex;
        width: 100%;
        height: 100px;
        border-radius: 4px;
        margin-top: 20px;
        justify-content: space-around;
        align-items: center;
        flex-wrap: wrap;
        gap: 10px;
      }
      .section:nth-child(1) {
        display: flex;
        border-radius: 5px;
        background-color: rgb(189, 231, 241);
        justify-content: space-around;
        align-items: center;
        width: calc(50% - 20px);
        padding: 10px;
        box-sizing: border-box;
      }
      .section:nth-child(2) {
        display: flex;
        border-radius: 5px;
        background-color: rgb(189, 231, 241);
        justify-content: space-around;
        align-items: center;
        width: calc(50% - 20px);
        padding: 10px;
        box-sizing: border-box;
      }
      .section:nth-child(3) {
        display: flex;
        border-radius: 5px;
        background-color: rgb(189, 231, 241);
        justify-content: space-around;
        align-items: center;
        width: calc(50% - 20px);
        padding: 10px;
        box-sizing: border-box;
      }
      .section:nth-child(4) {
        display: flex;
        border-radius: 5px;
        background-color: rgb(189, 231, 241);
        justify-content: space-around;
        align-items: center;
        width: calc(50% - 20px);
        padding: 10px;
        box-sizing: border-box;
      }
      .shopping {
        width: 90px;
        height: 30px;
        border-radius: 5px;
        background-color: orange;
        color: #fff;
        font-size: 14px;
        text-align: center;
        line-height: 30px;
      }
      .box {
        line-height: 35px;
      }
    </style>
  </head>
  <body>
    <!-- 主体内容 -->
    <section>
      <div class="section">
        <div class="box">
          <p>《前端小白入门手册》</p>
          <div>适用人群:没有任何前端基础的小白</div>
          <div>费用:¥499</div>
        </div>
        <div class="shopping">
          <div class="btn">加入购物车</div>
        </div>
      </div>
      <div class="section">
        <div class="box">
          <p>《HTML5月CSS3实现动态网页》</p>
          <div>适用人群:有html和css基础的童鞋</div>
          <div>费用:¥599</div>
        </div>
        <div class="shopping">
          <div class="btn">加入购物车</div>
        </div>
      </div>
      <div class="section">
        <div class="box">
          <p>《前端小白入门手册》</p>
          <div>适用人群:没有任何前端基础的小白</div>
          <div>费用:¥599</div>
        </div>
        <div class="shopping">
          <div class="btn">加入购物车</div>
        </div>
      </div>
      <div class="section">
        <div class="box">
          <p>《HTML5月CSS3实现动态网页》</p>
          <div>适用人群:有html和css基础的童鞋</div>
          <div>费用:¥699</div>
        </div>
        <div class="shopping">
          <div class="btn">加入购物车</div>
        </div>
      </div>
    </section>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值