Css flex 弹性布局

1.容器的属性

以下6个属性设置在容器上
flex-direction 项目的排列方向
flex-wrap 换行
flex-flow <排列方向> + <换行>
justify-content 项目在主轴上(横向)的对齐方式
align-items 项目在交叉轴(垂直方向)上如何对齐
align-content 对齐内容

1.1 flex-direction 方向

决定主轴的方向(即项目的排列方向)。

四个值
1.flex-direction: row(默认); 水平方向从左往右排列 起点在左端
2.flex-direction: row-reverse; 水平方向从右往左排列 起点在右端
3.flex-direction: column;垂直从上到下排列 起点在下沿
4.flex-direction: column-reverse;垂直方向从下到上排列 起点在下沿

1.row


微信图片_20200916140346.png

2.row-reverse


微信图片_202009161403461.png

3.column


微信图片_202009161403462.png

4.column-reverse


微信图片_202009161403463.png

1.2 flex-wrap 换行

默认情况下,项目都排在一条线(又称"横向线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行

三个值
1.flex-wrap:nowrap 不换行(默认)
2.flex-wrap:wrap 换行 第一行在上方
3.flex-wrap:wrap-reverse 第一行在下方 

1.nowrap

微信图片_202009161418542.png

2.wrap

微信图片_202009161418541.png

3.wrap-reverse

微信图片_20200916141854.png

1.3 flex-flow 流动

flex-flow 是 flex-direction 方向 + flex-wrap 换行 的简写方式

.box {
  flex-flow: <flex-direction> || <flex-wrap>;
}
flex-flow: row nowrap;(默认值 )

1.4 justify-content 定义了项目在主轴上(横向)对齐方式

属性值
六个值
justify-content:flex-start;左对齐(默认)
justify-content:flex-end;右对齐
justify-content:center; 居中
justify-content: space-between; 两端对齐 项目之间间隔相同
justify-content: space-around 每个项目两侧的间隔相等,所以项目之间的间隔比项目与边框的间隔大一倍。
justify-content: space-evenly 平均分布

1.flex-start


微信图片_20200916145838.png

2.flex-end


微信图片_202009161458381.png

3.center


微信图片_202009161458382.png

4.space-between


微信图片_202009161458383.png

5.space-around


微信图片_202009161458384.png

6.space-evenly


微信图片_20200917102700.png

1.5 align-items 垂直对齐项目

定义了项目在主轴上的垂直对齐方式

align-items: flex-start; 交叉轴的起点对齐
align-items: flex-end;交叉轴的终点对齐
align-items: center;交叉轴的中点对齐
align-items: baseline;项目的第一行文字的基线对齐。
align-items: stretch;(默认值)如果项目没有设置高度 将和日期对齐

1.flex-start


微信图片_20200916151644.png

2.flex-end


微信图片_202009161516441.png

3.center


微信图片_202009161516442.png

4.baseline


image.png

5.stretch
未设置高度或者高度为auto将屏幕占满


微信图片_202009161516444.png

1.6 align-content 属性定义了多根横线的对齐方式。如果项目只有一根横线,该属性不起作用。(也就是需要有多行才能体现行于行之间的距离)

align-content:flex-start:顶部对齐
align-content:flex-end:底部对齐
align-content:center:垂直中间对齐
align-content:space-between:上下贴壁,中间均匀分布
align-content:space-around:每个元素之间的距离固定,所以元素相交距离为2倍,类似元素为“回”字
align-content:space-evenly:均匀分布,元素及border距离相等
align-content:stretch(默认值):轴线占满整个交叉轴。

1.flex-start


微信图片_20200917101826.png

2.flex-end


微信图片_202009171018261.png

3.center


微信图片_20200917101958.png

4.space-between


微信图片_202009171018262.png

5.space-around


微信图片_202009171018263.png

6.space-evenly


微信图片_202009171018264.png

7.stretch(默认值)


微信图片_202009171018265.png

2.项目的属性

以下6个属性设置在项目上。
order 顺序
flex-grow 放大比例
flex-shrink 缩小比例
flex-basis 主要成分
flex 弯曲
align-self 对齐自身

2.1 order 顺序

.item {
  order: <integer>;
}

属性定义的数字越小越靠前 ,默认为0,可设置负数


微信图片_20200916164503.png

2.2 flex-grow 放大比例

.item {
  flex-grow: <number>; /* default 0 */
}

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。


微信图片_20200916165112.png

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

2.3 flex-shrink 缩小比例

.item {
  flex-shrink: <number>; /* default 1 */
}

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
值越大缩小的越厉害


微信图片_20200916174014.png

如果设置了flex-shrink就不会溢出

2.4 flex-basis属性 级别比width更高

.item {
  flex-basis: <length> | auto; /* default auto */
}

2.5 flex属性

放大默认0 缩小默认1 宽度
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

2.6 align-self属性 自身特别的对齐方式

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

.item {
 align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
微信图片_20200916175630.png

该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

3.以下是 flex 布局的一些应用

<!-- HTML -->
<div className='father'>
        <div className='son'>css</div>
        <div className='son'>flex</div>
        <div className='son'>布局</div>
        <div className='son'>dome</div>
        <div className='son'>梁梁</div>
 </div>

1.水平居中排列


css-flex水平布局.png
/* CSS */
.box{
    display: flex; *
    justify-content: center; *
    align-items: center; *
}
//或
.box{
    display: flex;// 或inline-flex
    justify-content: center;
    .son{
        align-self: center;
    }
}
//或
.box{
    display: flex;
    place-content: center;
    .son{
        align-self: center;
    }
}
//或
.box{
    display: flex;
    place-content: center;
    place-items: center;
}

2.水平居中垂直排列 多个元素


css-flex水平垂直布局.png
.box{
    display: flex; *
    justify-content: center; *
    align-items: center; *
    flex-direction: column;* 跟水平居中排列代码相同 只需要改变方向
}

属性等价关系

.main{
    place-content: center;

    place-items: center;
}
//等价于
.main{
    align-content: center;
    justify-content: center;

    align-items: center;
    justify-items: center;
}
//多行  垂直水平居中
.main{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

如果只有一个Flex项目时 可以设置margin:auto中水平垂直居中


css-flex-margin.png
.father{
    display: flex;// 或 inline-flex
    .son{
        margin: auto;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。