一、flex-direction用于设置主轴的方向

1.主轴和侧轴

在flex布局中分为主轴和侧轴两个方向,也可称为 行和列、x轴和y轴

  • 其中默认主轴方向为x轴,水平向右
  • 默认侧轴方向为y轴,水平向下

2.属性值

flex-direction属性值用于决定主轴的方向

flex-direction
属性值 说明
row 从左到右,默认值
row-reverse 从右到左
column 从上到下
column-reverse 从下到上

3.案例

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
<style>
        div{
            width: 200px;
            height: 200px;
            background-color: skyblue;
            color: white;
            
        }
        div span{
            width: 50px;
            height: 50px;
            background-color: red;
        }
    </style>

页面效果展示

(1)首先为父级赋值flex属性

div{
            width: 200px;
            height: 200px;
            background-color: skyblue;
            color: white;
            /* 父级添加flex属性 */
            display: flex;
        }

页面效果展示 - 从左到右(默认值)

  

(2)设置flex-direction属性值:row

div{
            width: 200px;
            height: 200px;
            background-color: skyblue;
            color: white;
            /* 父级添加flex属性 */
            display: flex;
            /* flex-direction默认值row */
            flex-direction: row;
        }

页面效果展示 - 从左到右,与无特别设置属性值时相同

 (3)设置flex-direction属性值:row-reverse

flex-direction: row-reverse;

页面效果展示 - 从右到左

 (4)设置flex-direction属性值:column

flex-direction: column; 

页面效果展示 - 从上到下

 (5)设置flex-direction属性值:column-reverse

flex-direction: column-reverse;

页面效果展示 - 从下到上

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐