Flex布局详解(一)

Flex布局

Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。 任何一个容器都可以指定为Flex布局,行内元素也可以使用Flex布局。

/* 块级元素 */

display: flex;

display: -webkit-flex;/* 需要兼容火狐Safari */

 

/* 行内元素 */

display: inline-flex;

这里需要注意的是设为Flex布局以后,子元素的float、clear和vertical-align属性将失效

兼容性

到目前为止,在PC端其实很乐观了,基本上主流的浏览器都已经兼容了flex的使用,除了IE10以下的浏览器,IE11也有部分不兼容。

<img src="https://pic3.zhimg.com/v2-3b9fcce9ed23db23837a0ee6029d7b16_b.jpg" data-size="normal" data-rawwidth="604" data-rawheight="310" class="origin_image zh-lightbox-thumb" width="604" data-original="https://pic3.zhimg.com/v2-3b9fcce9ed23db23837a0ee6029d7b16_r.jpg"/>

 

市面上浏览器的兼容情况

基本概念

flex布局元素称为Flex容器(flex container),简称容器。他的所有子元素自动称为容器成员,称为Flex项目(flex item),简称项目。

当使用 flex 布局时,首先想到的是两根轴线 —— 主轴和交叉轴。主轴由flex-direction定义,另一根轴垂直于它。我们使用 flexbox 的所有属性都跟这两根轴线有关, 所以有必要在一开始首先理解它。

<img src="https://pic3.zhimg.com/v2-2fff73acc894edca713ca7b38614f1b2_b.jpg" data-caption="" data-size="normal" data-rawwidth="563" data-rawheight="333" class="origin_image zh-lightbox-thumb" width="563" data-original="https://pic3.zhimg.com/v2-2fff73acc894edca713ca7b38614f1b2_r.jpg"/>

 

水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。项目默认沿主轴排列。

这篇文章先介绍容器属性

容器属性

flex-direction  决定主轴的方向即项目的排列方向

 

flex-wrap       项目在容器轴线排不下如何换行

 

flex-flow       是前两个属性的缩写

 

justify-content 项目在容器主轴上对齐方式

 

align-items     项目在交叉轴上如何对齐

 

align-content   多根轴线的对齐方式

1、flex-direction

flex-direction属性决定主轴的方向(即项目的排列方向)

row(默认值)主轴为水平方向起点在左端

 

row-reverse: 主轴水平方向起始点在右端 

 

column:      主轴为垂直方向起点在上沿 

 

column-reverse:主轴在垂直方向起点在下沿

1)row 主轴为水平方向,起点在左端。

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

display: flex;

/* flex-direction: row; */  /*不写这个也是默认水平*/

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

                margin-left: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-fefccd2ef2ce012574ad907c3ec0ce1c_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic1.zhimg.com/v2-fefccd2ef2ce012574ad907c3ec0ce1c_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic1.zhimg.com/v2-fefccd2ef2ce012574ad907c3ec0ce1c_r.jpg"/>

 

 

2)row-reverse 主轴水平方向,起始点在右端。

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

flex-direction: row-reverse;/* ******* */

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin-left: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-b715c91b3ff7b8c806d0c24ae15a35fc_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic1.zhimg.com/v2-b715c91b3ff7b8c806d0c24ae15a35fc_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic1.zhimg.com/v2-b715c91b3ff7b8c806d0c24ae15a35fc_r.jpg"/>

 

 

3)column 主轴为垂直方向,起点在上沿

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

flex-direction: column;/* ******* */

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin-left: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic4.zhimg.com/v2-8364209924cf6b73fa214ea4d405a227_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic4.zhimg.com/v2-8364209924cf6b73fa214ea4d405a227_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic4.zhimg.com/v2-8364209924cf6b73fa214ea4d405a227_r.jpg"/>

 

 

4)Column-reverse这个就不做演示了同上

2、flex-wrap

如果一条轴线排不下,如何换行。

nowrap 默认不换行

wrap   换行 第一行在上方

wrap-reverse    倒着换行第一行在下方

1)nowrap 默认不换行

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

flex-wrap: nowrap;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

<div class="chl_box">5</div>

<div class="chl_box">6</div>

<div class="chl_box">7</div>

<div class="chl_box">8</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-4245472b88bb6bee0e39cbf81ed5dd2c_b.jpg" data-caption="" data-size="normal" data-rawwidth="722" data-rawheight="236" class="origin_image zh-lightbox-thumb" width="722" data-original="https://pic1.zhimg.com/v2-4245472b88bb6bee0e39cbf81ed5dd2c_r.jpg"/>

 

2)wrap换行 第一行在上方

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

flex-wrap: wrap;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

<div class="chl_box">5</div>

<div class="chl_box">6</div>

<div class="chl_box">7</div>

<div class="chl_box">8</div>

</div>

</body>

<img src="https://pic2.zhimg.com/v2-214a51bb786b49c2284dad3fc19b0735_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic2.zhimg.com/v2-214a51bb786b49c2284dad3fc19b0735_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic2.zhimg.com/v2-214a51bb786b49c2284dad3fc19b0735_r.jpg"/>

 

 

3)wrap-reverse 倒着换行

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

flex-wrap: wrap-reverse;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

<div class="chl_box">5</div>

<div class="chl_box">6</div>

<div class="chl_box">7</div>

<div class="chl_box">8</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-127e43dabbc2600336f685ec801b3d48_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic1.zhimg.com/v2-127e43dabbc2600336f685ec801b3d48_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic1.zhimg.com/v2-127e43dabbc2600336f685ec801b3d48_r.jpg"/>

 

 

3、Justify-content(主轴对齐方式/横轴)

flex-start:     默认值 左对齐 

flex-end:       右对齐 

center:         居中 

space-between:  两端对齐项目之间的间隔都相等 

space-around:   每个项目两侧的间隔相等

1)flex-start 默认值左对齐

这个就不做演示了,和默认的一样

<img src="https://pic4.zhimg.com/v2-14b966e12c229c7fafa468b3082a20db_b.jpg" data-caption="" data-size="normal" data-rawwidth="794" data-rawheight="272" class="origin_image zh-lightbox-thumb" width="794" data-original="https://pic4.zhimg.com/v2-14b966e12c229c7fafa468b3082a20db_r.jpg"/>

 

2)flex-end 右对齐

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

justify-content: flex-end;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic4.zhimg.com/v2-9a908b8e10ecab4500940397503177f7_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic4.zhimg.com/v2-9a908b8e10ecab4500940397503177f7_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic4.zhimg.com/v2-9a908b8e10ecab4500940397503177f7_r.jpg"/>

 

 

3)center居中

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

justify-content: center;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-ff90545126b48b0883986ce3072471cc_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic1.zhimg.com/v2-ff90545126b48b0883986ce3072471cc_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic1.zhimg.com/v2-ff90545126b48b0883986ce3072471cc_r.jpg"/>

 

 

4)Space-between 两端对齐,项目之间的间隔都相等

 <style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

justify-content: space-between;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic1.zhimg.com/v2-d68351977234ecce9c9658a2b8a9eda8_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic1.zhimg.com/v2-d68351977234ecce9c9658a2b8a9eda8_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic1.zhimg.com/v2-d68351977234ecce9c9658a2b8a9eda8_r.jpg"/>

 

 

5)Space-around 每个项目两侧的间隔相等

<style type="text/css">

.box{

width: 500px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

justify-content: space-around;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

<div class="chl_box">4</div>

</div>

</body>

<img src="https://pic2.zhimg.com/v2-c20e862b168c366f713e56301ac22ac1_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic2.zhimg.com/v2-c20e862b168c366f713e56301ac22ac1_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic2.zhimg.com/v2-c20e862b168c366f713e56301ac22ac1_r.jpg"/>

 

 

4、align-items 单行(垂直居中使用这个)

flex-start:与交叉轴的起点对齐。 向上对齐

flex-end:与交叉轴的终点对齐。 向下对齐

center:与交叉轴的中点对齐。 居中对齐

baseline: 以文字底部对齐

stretch(默认值)轴线占满整个交叉轴

1)flex-start:与交叉轴的起点对齐。 向上对齐

<style type="text/css">

.box{

width: 500px;

height: 400px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

align-items: flex-start;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

.chl_box:nth-child(2){

height: 150px;

}

.chl_box:nth-child(3){

height: 120px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

</div>

</body>

<img src="https://pic2.zhimg.com/v2-f1306bb8d4014c213ba65a1dbd29aae1_b.jpg" data-caption="" data-size="small" data-rawwidth="739" data-rawheight="571" class="origin_image zh-lightbox-thumb" width="739" data-original="https://pic2.zhimg.com/v2-f1306bb8d4014c213ba65a1dbd29aae1_r.jpg"/>

 

2)flex-end:与交叉轴的终点对齐。 向下对齐

<style type="text/css">

.box{

width: 500px;

height: 400px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

align-items: flex-end;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

.chl_box:nth-child(2){

height: 150px;

}

.chl_box:nth-child(3){

height: 120px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

</div>

</body>

<img src="https://pic3.zhimg.com/v2-f55a071635f9956917302ee7f022c87e_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic3.zhimg.com/v2-f55a071635f9956917302ee7f022c87e_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic3.zhimg.com/v2-f55a071635f9956917302ee7f022c87e_r.jpg"/>

 

 

3)center:与交叉轴的中点对齐。 居中对齐(垂直居中)

<style type="text/css">

.box{

width: 500px;

height: 400px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

align-items: center;

}

.chl_box{

width: 100px;

height: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

.chl_box:nth-child(2){

height: 150px;

}

.chl_box:nth-child(3){

height: 120px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

</div>

</body>

<img src="https://pic2.zhimg.com/v2-2d94a3dbde805d5e971f582d36045465_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic2.zhimg.com/v2-2d94a3dbde805d5e971f582d36045465_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic2.zhimg.com/v2-2d94a3dbde805d5e971f582d36045465_r.jpg"/>

 

 

4)baseline: 以文字底部对齐

为了体现效果给第二个增加行高

<style type="text/css">

.box{

width: 500px;

height: 400px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

align-items: baseline;

}

.chl_box{

width: 100px;

height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

.chl_box:nth-child(2){

height: 150px;

line-height: 150px;

}

.chl_box:nth-child(3){

height: 120px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

</div>

</body>

<img src="https://pic4.zhimg.com/v2-c8e0552448e952058b93b9f30317cc3b_b.gif" data-caption="" data-size="normal" data-rawwidth="1606" data-rawheight="906" data-thumbnail="https://pic4.zhimg.com/v2-c8e0552448e952058b93b9f30317cc3b_b.jpg" class="origin_image zh-lightbox-thumb" width="1606" data-original="https://pic4.zhimg.com/v2-c8e0552448e952058b93b9f30317cc3b_r.jpg"/>

 

 

5)stretch(默认值)轴线占满整个交叉轴

为了实现效果,去除高度

<style type="text/css">

.box{

width: 500px;

height: 400px;

border: 5px solid #00008B;

margin: 50px auto;

display: flex;

align-items: stretch;

}

.chl_box{

width: 100px;

line-height: 100px;

text-align: center;

background: #00C2DE;

margin: 5px;

}

</style>

<body>

<div class="box">

<div class="chl_box">1</div>

<div class="chl_box">2</div>

<div class="chl_box">3</div>

</div>

</body>

<img src="https://pic4.zhimg.com/v2-4fc9edc20aede12baa8ba80e2d5ed54f_b.jpg" data-caption="" data-size="normal" data-rawwidth="741" data-rawheight="578" class="origin_image zh-lightbox-thumb" width="741" data-original="https://pic4.zhimg.com/v2-4fc9edc20aede12baa8ba80e2d5ed54f_r.jpg"/>

 

5、align-content(纵轴对齐方式)多根轴线的对齐方式

如果项目只有一根轴线,该属性不起作用。

flex-start:与交叉轴的起点对齐。 向上对齐

flex-end:与交叉轴的终点对齐。 向下对齐

center:与交叉轴的中点对齐。 居中对齐

space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。 

space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间 隔比轴线与边框的间隔大一倍。

stretch(默认值):轴线占满整个交叉轴

1)flex-start:与交叉轴的起点对齐。 向上对齐

<img src="https://pic1.zhimg.com/v2-802e823523bf498d05d92ccd2995e7d8_b.jpg" data-caption="" data-size="small" data-rawwidth="706" data-rawheight="564" class="origin_image zh-lightbox-thumb" width="706" data-original="https://pic1.zhimg.com/v2-802e823523bf498d05d92ccd2995e7d8_r.jpg"/>

 

2)flex-end:与交叉轴的终点对齐。 向下对齐

<img src="https://pic4.zhimg.com/v2-fe388491e7f46f4793fbed1e38ca24c3_b.jpg" data-caption="" data-size="small" data-rawwidth="692" data-rawheight="548" class="origin_image zh-lightbox-thumb" width="692" data-original="https://pic4.zhimg.com/v2-fe388491e7f46f4793fbed1e38ca24c3_r.jpg"/>

 

3)center:与交叉轴的中点对齐。 居中对齐

<img src="https://pic2.zhimg.com/v2-f650eb8686edffcc795270ef8b3d5ec1_b.jpg" data-caption="" data-size="small" data-rawwidth="693" data-rawheight="563" class="origin_image zh-lightbox-thumb" width="693" data-original="https://pic2.zhimg.com/v2-f650eb8686edffcc795270ef8b3d5ec1_r.jpg"/>

 

4)space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。

<img src="https://pic4.zhimg.com/v2-4a329989cc96711b1dfbd5a20ccdb247_b.jpg" data-caption="" data-size="small" data-rawwidth="677" data-rawheight="549" class="origin_image zh-lightbox-thumb" width="677" data-original="https://pic4.zhimg.com/v2-4a329989cc96711b1dfbd5a20ccdb247_r.jpg"/>

 

5)space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间 隔比轴线与边框的间隔大一倍。

<img src="https://pic2.zhimg.com/v2-f43c229e1b02bdcfd45cc280e9a62c81_b.jpg" data-caption="" data-size="small" data-rawwidth="676" data-rawheight="556" class="origin_image zh-lightbox-thumb" width="676" data-original="https://pic2.zhimg.com/v2-f43c229e1b02bdcfd45cc280e9a62c81_r.jpg"/>

 

https://www.bilibili.com/video/BV13b411g78D

 

www.bilibili.com/video/BV13b411g78D

 

 

from:https://zhuanlan.zhihu.com/p/163217760

 

posted @ 2023-02-13 21:39  imxiangzi  阅读(161)  评论(0编辑  收藏  举报