flex弹性布局

flex弹性布局(display: flex;)

添加在父容器上

1. display: flex;
2. flex-direction 布局的排列方向

  • flex-direction: row;默认值,显示为行。方向为当前文档水平流方向,默认情况下是从左往右。
<style>
        #main ul{
            margin: 100px;
            display: flex;
            list-style: none;
            flex-direction: row;
        }
        #main ul li:nth-of-type(1){ width: 100px; height: 100px; background: blue;}
        #main ul li:nth-of-type(2){ width: 100px; height: 100px; background: red;}
        #main ul li:nth-of-type(3){ width: 100px; height: 100px; background: yellow;}
        #main ul li:nth-of-type(4){ width: 100px; height: 100px; background: pink;}
        #main ul li:nth-of-type(5){ width: 100px; height: 100px; background: green;}
        #main ul li:nth-of-type(6){ width: 100px; height: 100px; background: gray;}
        #main ul li:nth-of-type(7){ width: 100px; height: 100px; background: orange;}
        #main ul li:nth-of-type(8){ width: 100px; height: 100px; background: orangered;}
    </style>

在这里插入图片描述

  • flex-direction: row-reverse;显示为行。但方向和row属性值是反的
#main ul{
            display: flex;
            list-style: none;
            flex-direction: row-reverse;
        }

在这里插入图片描述

  • flex-direction: column;显示为列
#main ul{
            display: flex;
            list-style: none;
            flex-direction: column;
        }

在这里插入图片描述

  • flex-direction: column-reverse; 显示为列。但方向和column属性值是反的
#main ul{
            display: flex;
            list-style: none;
            flex-direction: column-reverse;
        }

在这里插入图片描述
3.flex-wrap 是否进行换行处理.

  • flex-wrap: nowrap;默认值,不换行处理
#main ul{
            width: 500px;
            display: flex;
            list-style: none;
            flex-wrap: nowrap;
        }

在这里插入图片描述

  • flex-wrap: wrap;换行处理
#main ul{
            width: 500px;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
        }

在这里插入图片描述

  • wrap-reverse; 反向换行
#main ul{
            width: 500px;
            display: flex;
            list-style: none;
            flex-wrap: wrap-reverse;
        }

在这里插入图片描述
4.justify-content 主轴方向上子项的对齐和分布方式

  • justify-content: flex-start; 子项都去起始位置对齐
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: flex-start;
        }

在这里插入图片描述

  • justify-content: flex-end; 子项都去结束位置对齐。
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: flex-end;
        }

在这里插入图片描述

  • justify-content: center; 子项都去中心位置对齐
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: center;
        }

在这里插入图片描述

  • justify-content: space-between; 两端对齐。
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: space-between;
        }

在这里插入图片描述

  • justify-content: space-around;around是环绕的意思,意思是每个flex子项两侧都环绕互不干扰的等宽的空白间距,最终视觉上边缘两侧的空白只有中间空白宽度一半。
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: space-around;
        }

在这里插入图片描述

  • justify-content: space-evenly; evenly是匀称、平等的意思。也就是视觉上,每个flex子项两侧空白间距完全相等。
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            justify-content: space-evenly;
        }

在这里插入图片描述
5.align-items 每一行中的子元素上下对齐方式

  • align-items: flex-start; 起始位置对齐
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            align-items: flex-start;
        }

在这里插入图片描述

  • align-items: center; 中心对齐
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            align-items: center;
        }

在这里插入图片描述

  • align-items: flex-end; 结束位置对齐
#main ul{
            width: 1000px;
            height: 200px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            align-items: flex-end;
        }

在这里插入图片描述
5.align-content 属性定义了在交叉轴方向的对齐方式及额外空间分配(侧轴的对齐方式)。一行看不出效果

  • align-content: flex-start; 从起始线开始排列
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: flex-start;
        }

在这里插入图片描述

  • align-content: flex-end; 从终点线开始排列
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: flex-end;
        }

在这里插入图片描述

  • align-content:center; 居中排列
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: center;
        }

在这里插入图片描述

  • align-content: space-around; 边缘两侧的空白只有中间空白宽度一半(相邻项目之间的距离是两个项目之间留白的和)
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: space-around;
        }

在这里插入图片描述

  • align-content:space-between; 项目均匀分布,第一项在启点线,最后一项在终点线
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: space-between;
}

在这里插入图片描述

  • align-content: stretch; 默认值
#main ul{
            width: 500px;
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-items: flex-end;
            align-content: stretch;
        }

在这里插入图片描述

添加在子容器上

1.order 排序: 值从小到大顺序排列,可以为负值,缺省为0。

<style>
        *{padding: 0; margin: 0;}
        #main ul{
            /* width: 500px; */
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
            flex-wrap: wrap;
            align-content: flex-start;
        }
        #main ul li:nth-of-type(1){ width: 100px; height: 100px; background: blue; order: 8;}
        #main ul li:nth-of-type(2){ width: 100px; height: 100px; background: red; order: 7;}
        #main ul li:nth-of-type(3){ width: 100px; height: 100px; background: yellow; order: 6;}
        #main ul li:nth-of-type(4){ width: 100px; height: 100px; background: pink; order: 5;}
        #main ul li:nth-of-type(5){ width: 100px; height: 100px; background: green; order: 4;}
        #main ul li:nth-of-type(6){ width: 100px; height: 100px; background: gray; order: 3;}
        #main ul li:nth-of-type(7){ width: 100px; height: 100px; background: orange; order: 2;}
        #main ul li:nth-of-type(8){ width: 100px; height: 100px; background: orangered; order: 1;}
    </style>
 <body>
    <div id="main">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
        </ul>
    </div>
</body>

在这里插入图片描述
2.flex-grow 扩展

  • flex-grow: 0;默认值,不扩展
  • flex-grow: 1; 扩展,会把空白区全部占满
#main ul li{
            flex-grow: 0;
        }
#main ul li{
            flex-grow: 1;
        }

在这里插入图片描述在这里插入图片描述
2.flex-shrink 收缩
正常默认值是1
0表示不收缩,.5收缩小一些,2收缩大一些。(大小是跟正常缩放1进行比较的)

#main ul li{
           flex-shrink: 1;
       }
       #main ul li{
           flex-shrink: .5;
       }

在这里插入图片描述
3.flex-basis (跟flex-shrink/flex-grow很像)。
flex-shrink/flex-grow是设置一个比例值,flex-basis是设置一个具体值。

#main ul li{
           flex-basis: auto;
       }
       
#main ul li{
           flex-basis: 60px;
       }

#main ul li{
           flex-basis: 160px;
       }

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
4.align-shef 控制单独某一个子项的对齐方式(和align-items类似)

#main ul{
            height: 400px;
            margin: 100px;
            background: cyan;
            display: flex;
            list-style: none;
        }
#main ul li:nth-of-type(1){ 
	width: 100px; 
	height: 100px; 
	background: blue; 
	align-self: center;
	}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值