(大白话)一口气带你学完 flex 布局
flex布局
一、参考资料
https://www.runoob.com/w3cnote/flex-grammar.html
二、什么是flex布局
Flex是Flexible Box的缩写,意为”弹性布局;任何一个容器都可以指定为Flex布局。
十分简单灵活,几行代码就可以实现各种页面的的布局;彻底摆脱各种 float、display、position 。
采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。
它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称”项目”
基础代码
<html lang="">
<body>
<div class="container">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
<div class="box4">box4</div>
<div class="box5">box5</div>
</div>
</body>
</html>
<style>
.container{
width: 1200px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* TODO 可以添加 flex 布局属性,控制容器内项目的各种布局 */
}
.box1{
font-size: 30px;
width: 200px;
height: 200px;
background-color: royalblue;
}
.box2{
font-size: 30px;
width: 200px;
height: 200px;
background-color: green;
}
.box3{
font-size: 30px;
width: 200px;
height: 200px;
background-color: lightcoral;
}
.box4{
font-size: 30px;
width: 200px;
height: 200px;
background-color: cyan;
}
.box5{
font-size: 30px;
width: 200px;
height: 200px;
background-color: #e6db74;
}
</style>
image.png
三、容器的属性
容器的属性有 6个 :
● flex-direction
● flex-wrap
● flex-flow
● justify-content
● align-items
● align-content
(一)flex-direction
flex-direction(布局方向): 决定主轴的方向(即项目的排列方向)
.container{
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* 决定主轴的方向(即项目的排列方向) */
flex-direction: row | row-reverse | column | column-reverse;
}
image.png
(二)flex-wrap
flex-wrap: 决定容器内项目是否可换行
.container{
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* 决定容器内项目是否可换行 */
flex-wrap: nowrap | wrap | wrap-reverse;
}
image.png
(三)flex-flow
flex-flow: flex-direction 和 flex-wrap 的简写形式,建议还是直接写 flex-direction 和 flex-wrap
.container{
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* flex-direction 和 flex-wrap 的简写形式 */
flex-flow: <flex-direction> <flex-wrap>;
}
(四)justify-content
justify-content:定义了项目在主轴(水平方向)的对齐方式
这个就和 word 文档中的 文字对齐是一样的
.container {
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* 定义了项目在主轴(水平方向)的对齐方式 */
justify-content: flex-start | flex-end | center | space-between | space-around;
}
image.png
(五)align-items
align-items: 定义了项目在垂直轴(垂直方向)上的对齐方式
.container {
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
/* 定义了项目在垂直轴(垂直方向)上的对齐方式 */
align-items: flex-start | flex-end | center | baseline | stretch;
}
.box3{
font-size: 30px;
width: 200px;
height: 400px;
background-color: lightcoral;
}
.box4{
font-size: 30px;
width: 200px;
height: 450px;
background-color: cyan;
}
.box5{
font-size: 30px;
width: 200px;
height: 600px;
background-color: #e6db74;
}
image.png
(六)align-content
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
.container {
width: 1000px;
height: 1000px;
display: flex; /* 设置为 flex 布局*/
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
比如当容器属性设置为 flex-wrap: nowrap,如图,
image.png
就只有一根线,但是如果设置为 flex-wrap: wrap,如图:
image.png
就存在两个线,align-content 才会生效
可设置的六个属性如下:
四、项目的属性
有六种属性可设置在 item 项目上:
● order
● flex-grow
● flex-shrink
● flex-basis
● flex
● align-self
(一)order
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0
.box5{
order: -1;
font-size: 30px;
width: 200px;
height: 600px;
background-color: #e6db74;
}
image.png
(二)flex-grow
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
没设置前:
image.png
给 box3 设置:
.box3{
flex-grow: 2; // 设置 放大2,比别的项目item 大一倍
font-size: 30px;
width: 200px;
height: 400px;
background-color: lightcoral;
}
image.png
(三)flex-shrink
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
该属性和 flex-grow(放大) 属性是反着来的
(四)flex-basis
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
.item {
flex-basis: <length> | auto; /* default auto */
}
它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
(五)flex
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
(六)align-self
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
该属性可取6个值,除了auto,其他都与align-items属性完全一致。