Flex布局应用典型案例

1.左右两侧宽度100px固定,中间宽度根据屏幕宽度自适应
在这里插入图片描述
实现:

<div class="main">
        <div class="box1">
            左侧
        </div>
        <div class="box2">
            中间
        </div>
        <div class="box3">
            右侧
        </div>
    </div>
.main {
    /* 占父盒子宽度的100% */
    width: 100%;
    height: 40px;
    background: #999;
    /* 父盒子设置flex */
    display: flex;
    color: #fff;
    line-height: 40px;
    text-align: center;
}

.main .box1,
.main .box3 {
    /* box1,box3固定宽度 */
    width: 100px;
    height: 40px;
    background: pink;
}

.main .box2 {
    /* box2占据剩余宽度 */
    flex: 1;
    height: 100%;
    background: blue;
}

2.子盒子在父盒子中水平垂直方向居中(其他方式)
在这里插入图片描述
实现:

<div class="box">
        <div class="childrenBox">

        </div>
    </div>
.box {
    width: 400px;
    height: 400px;
    background: #666;
    /* 父级设置flex */
    display: flex;
    /* 设置主轴上的子元素排列方式:居中 */
    justify-content: center;
    /* 设置侧轴上的子元素排列方式:居中(单行) */
    align-items: center;
    border-radius: 10px;
}

.childrenBox {
    width: 100px;
    height: 100px;
    background: #999;
}

3.父盒子宽500,高400,父盒子内部有6个子盒子,宽高为150*150,将子盒子一排3个显示两排,并且子盒子的左右间距相等,上下间距相等
在这里插入图片描述

实现:

<ul class="box">
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
</ul>
.box {
    width: 500px;
    height: 400px;
    padding: 0;
    background: #ccc;
    display: flex;
    list-style: none;
    display: flex;
    /*  换行显示 */
    flex-wrap: wrap;
    /*  设置主轴 间距平均分配 */
    justify-content: space-around;
    /*  设置侧轴 间距平均分配(子项多行显示的情况) */
    align-content: space-around;
}

.box li {
    width: 150px;
    height: 150px;
    background: pink;
}```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值