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;
}```

猜你喜欢

转载自blog.csdn.net/pilgrim_121/article/details/111992472