移动web flex弹性布局

flex布局: 父元素添加display:flex 父元素就称之为弹性容器,子元素成为弹性盒子

主轴对齐居中方式:justify-content: space-between; space-around; space-evenly; center; 

侧轴对齐居中方式: align-items: center;

.box {
            width: 800px;
            height: 400px;
            background-color: pink;
            display: flex;
            /* 弹性容器 给父元素添加 */
            justify-content: space-between;
            /* 主轴对齐方式 */
            margin: 0 auto;
        }
        .box div {
            width: 150px;
            height: 200px;
            background-color: aqua;
        }
        .box div:nth-child(2n) {
            background-color: blue;
        }
        /* flex布局:弹性布局 (操作父亲管儿子)
        1:父元素:弹性容器
        2:子盒子:弹性盒子
        3:主轴:(默认x轴)
        4:侧轴:(默认y轴) */
 <style>
          .box {
            width: 800px;
            height: 400px;
            background-color: pink;
            display: flex;
            /* 弹性容器 给父元素添加 */
            /* justify-content: space-between; */
            /* 主轴对齐方式 */
            margin: 0 auto;
            align-items: flex-start;
            /* 侧轴从上到下 */
            align-items: flex-end;
            /* 侧轴从下到上 */
            align-items: center;
            /* 子盒子垂直居中 */
            align-items: stretch;
            /* 拉伸子盒子,若子盒子没高度默认拉伸与父元素一样的高度 */
        }
        .box div {
            width: 150px;
            /* height: 200px; */
            background-color: aqua;
        }
        .box div:nth-child(2n) {
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    
</body>
    <style>
         .box {
            width: 800px;
            height: 400px;
            background-color: pink;
            display: flex;
            /* 弹性容器 给父元素添加 */
            margin: 0 auto;
            /* 主轴对齐方式 */
            justify-content: space-between;
            /* 左右边距对齐,中间自适应 */
            justify-content: space-around;
            /* 左右边距比例为1,中间边距为2 */
            justify-content: space-evenly;
             /* 中间空隙1:1 */
        }
        .box div {
            width: 150px;
            height: 200px;
            background-color: aqua;
        }
        .box div:nth-child(2n) {
            background-color: blue;
        }

    </style>
</head>
<body>
      <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
</body>

圣杯布局:左右两边弹性盒子固定贴在弹性容器旁边,中间 剩余内容在自身弹性盒子里占比例一份 取flex为1 

<style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            width: 100%;
            height: 50px;
            background-color: pink;
            display: flex;
        }
        .box .left,
        .box .right {
            width: 50px;
            height: 50px;
            background-color: blue;
        }
       
        .box .center {
            flex: 1;
            height: 50px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </div>
</body>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值