弹性盒布局display:flex;

写在容器(父元素)身上的属性

1、声明弹性盒子

display: flex;

display:inline-flex;

2、设置在主轴的排列方式 写在父元素

justify-content:flex-start; 左上角开始 默认值

justify-content:flex-end; 右上角开始

justify-content:center; 居中显示

justify-content:space-between; 两端对齐,中间自动分配

justify-content:space-around; 每个项目左右间隔一致

justify-content:space-evenly; 每个项目的间隔一样

3、设置主轴方向(主轴默认是横轴)写在父元素

flex-direction:row; 横轴(从前往后)

flex-direction:row-reverse; 横轴(从后往前)

flex-direction:column; 竖轴(从上到下)

flex-direction:column-reverse; 竖轴(从下到上)

4、交叉轴的排列方式(写在父元素上)

align-items:stretch; 默认值 高度铺满父元素

align-items:flex-start 最顶部

align-items:flex-end 最底部

align-items:center 垂直居中显示

align-items:baseline 基线对齐

5、多行排列方式(写在父元素上)

align-content:flex-start;

align-content:flex-end;

align-content:center;

align-content:space-around;

align-content:space-between;

写在容器(父元素)身上的属性

1、声明弹性盒子

display: flex;

display:inline-flex;

2、设置在主轴的排列方式(写在父元素上)

justify-content:flex-start; 左上角开始 默认值

justify-content:flex-end; 右上角开始

justify-content:center; 居中显示

justify-content:space-between; 两端对齐,中间自动分配

justify-content:space-around; 每个项目左右间隔一致

justify-content:space-evenly; 每个项目的间隔一样

3、设置主轴方向(主轴默认是横轴)写在父元素

flex-direction:row; 横轴(从前往后)

flex-direction:row-reverse; 横轴(从后往前)

flex-direction:column; 竖轴(从上到下)

flex-direction:column-reverse; 竖轴(从下到上)

4、交叉轴的排列方式(写在父元素上)

align-items:stretch; 默认值 高度铺满父元素

align-items:flex-start 最顶部

align-items:flex-end 最底部

align-items:center 垂直居中显示

align-items:baseline 基线对齐

5、多行排列方式

align-content:flex-start;

align-content:flex-end;

align-content:center;

align-content:space-around;

align-content:space-between;

6、一行排不下 是否换行(默认是收缩显示 不换行)

flex-wrap:nowrap(不换行 默认值)

flex-wrap:wrap 换行

代码示例:

可以控制单一变量验证以上效果,多行排列方式的没写,多加几个parent下的子div就能实现

<!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>
        .left div {
            width: 100px;
            height: 100px;
            background: palevioletred;
            /* margin: 10px; */
        }

        .right div {
            width: 100px;
            height: 100px;
            background: #2f7dae;
            margin: 10px;
        }

        .center div { 
            width: 100px;
            height: 100px;
            background: #2ecd50;
            margin: 10px;
        }

        .parent {
            /* 弹性盒布局 */
            display: flex;
            height: 700px;

            /* 左上角开始  默认值 */
            /* justify-content:flex-start;   */

            /* 右上角开始 */
            /* justify-content:flex-end;  */

            /*   居中显示 */
            justify-content: center;


            /* 两端对齐,中间自动分配 */
            /* justify-content:space-between;  */

            /* 每个项目左右间隔一致 */
            /* justify-content:space-around;  */

            /* 每个项目的间隔一样 */
            /* justify-content:space-evenly;   */

            /*   横轴(从前往后) */
            /* flex-direction:row;     */

            /* 横轴(从后往前) */
            /* flex-direction:row-reverse;   */

            /* 竖轴(从上到下) */
            /* flex-direction:column;    */

            /*竖轴(从下到上)  */
            /* flex-direction:column-reverse;  */



            /* 默认值  高度铺满父元素 */
            /* align-items:stretch;      */

            /* 最顶部 */
            /* align-items:flex-start  ;  */

            /* 最底部 */
            /* align-items:flex-end;  */

            /*垂直居中显示   */
            /* align-items:center  ;   */

            /*   基线对齐 */
            /* align-items:baseline ;   */

            /* 多行排列方式   即轴线不止一条,可换行,要每个元素都铺满时使用align-content*/
            align-content:stretch;
            /* align-content:  1. stretch 沿着交叉轴方向自动进行拉升到最大(默认值)。 
            2. center 沿着交叉轴方向 居中 对齐。 
            3. flex-start 沿着交叉轴方向 起点 对齐。 
            4. flex-end 沿着交叉轴方向 结尾 对齐。
            5. space-between 沿着交叉轴方向 间隔 对齐,头尾没有间距。 
            6. space-around 沿着交叉轴方向 间隔 对齐,头尾有间距。 */
        }

        .left {
              /* 占一份 */
              flex: 1;
            width: 400px;
            background: #f91010;
        }
        .right{
            /* 占一份 */
            flex: 1;
            width: 400px;
            /* height: 500px; */
            background: #cbec48;

        }
        .center{
             /* 占两份 */
            flex: 2;
            width: 400px;
            /* height: 500px; */
            background: #ee2ebe;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="left">
            <div class="child">1</div>
            <div class="child">2</div>
            <div class="child">3</div>
            <div class="child">4</div>
        </div>
        <div class="center">
            <div class="child">5</div>
            <div class="child">6</div>
            <div class="child">7</div>
        </div>
        <div class="right">
            <div class="child"></div>
            <div class="child"></div>
            <div class="child"></div>
        </div>
    </div>

</body>

</html>

写在项目身上(子元素)属性

1、定义项目的顺序

order: 2; 数字越大 就越靠后

2、设置单个项目在交叉轴的排列(和上面多行排列同理,就写进示例里了)

align-self:flex-start;

align-self:flex-end;

align-self:center;

3、项目放大 (子元素总宽小于父元素时放大)

flex-grow:1; 数字是份数 默认值0

4、项目的缩小(子元素总宽大于父元素时缩小)

flex-shrink:1; 默认值是1(收缩) 0 不收缩

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值