文章目录
一、flex简介及兼容
2009年,W3C 提出了一种新的方案----Flex 布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了 几乎 所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。但仍有部分老版本浏览器仍旧不支持使用,见下图:
二、基本概念
开启flex布局的元素称为 Flex 容器(flex container),简称"容器",其内部的子元素称为 Flex 项目(flex item),简称"项目"。
设置 display 属性为 flex 或者 inline-flex 均可以成为 flex container。
<style>
.box{
display: flex; /*常用:将 容器 以 block-level 形式 单行存在*/
display: inline-flex; /*少用:将 容器 以 inline-level 形式可与别的行内元素同行存在*/
}
</style>
<body>
<div class="box"> <!-- box 盒子为 flex container -->
<div class="item"></div>
<div class="item"></div> <!-- item 盒子为 flex item -->
<div class="item"></div>
</div>
</body>
三、容器的属性(用的最多)
应用于 flex container 上的属性:
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
3.1 flex-direction属性
flex-direction属性决定主轴的方向(即项目的排列方向)。
<style>
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
</style>
它可有4个值:
属性 | 功能介绍 |
---|---|
row(默认值) | 主轴为水平方向,起点在左端。 |
row-reverse | 主轴为水平方向,起点在右端。 |
column | 主轴为垂直方向,起点在上沿。 |
column-reverse | 主轴为垂直方向,起点在下沿。 |
3.2 justify-content属性
justify-content属性定义了项目在主轴上的对齐方式。
<style>
.box {
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
</style>
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。
属性 | 功能介绍 |
---|---|
flex-start(默认值) | 左对齐 |
flex-end | 右对齐 |
cente | 居中 |
space-between | 两端对齐,项目之间的间隔都相等。 |
space-around | items 之间的距离相等; main star、main end之间的距离 等于 flex items 之间的距离的一半 |
space-evenly | items 之间的距离相等; main star、main end之间的距离 等于 flex items 之间的距离 |
3.3 align-items属性
align-items属性定义项目在交叉轴上如何对齐。
<style>
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
</style>
它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。
属性 | 功能介绍 |
---|---|
flex-star | 与 cross start 对齐 |
flex-end | 与 cross end 对齐 |
center | 居中对齐 |
baseline | 与第一个项目的第一行文字的基线对齐 |
stretch(默认值) | 如果项目未设置高度或设为auto,将占满整个容器的高度 |
3.4 flex-wrap属性
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
<style>
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
</style>
它可能取三个值。
(1)nowrap(默认):不换行。
(2)wrap:换行,第一行在上方。
(3)wrap-reverse:换行,第一行在下方。
3.5 flex-flow属性
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
<style>
.box{
flex-flow: <flex-direction> || <flex-wrap>; /* 知道这个属性便可以了 */
}
</style>
3.6 align-content属性
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
<style>
.box{
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
</style>
该属性可能取6个值。
属性 | 功能介绍 |
---|---|
flex-star | 与 cross start 对齐 |
flex-end | 与 cross end 对齐 |
center | 居中对齐 |
space-between | 两端对齐,items 之间的距离相等 |
space-around | items 之间的距离相等; cross start、cross end之间的距离 等于 flex items 之间的距离的一半 |
space-evenly | items 之间的距离相等; main start、cross end之间的距离 等于 flex items 之间的距离 |
四、项目的属性(用的很少)
应用于 flex item上的属性:
- order
- flex-grow
- flex-shrink
- flex-basis
- flex
- align-self
4.1 order属性
order属性定义项目的排列顺序。可以设置任意整数(正整数、负整数、0)数值越小越靠前,默认为0。
这个属性理解就行
<style>
.item {
order: <integer>;
}
</style>
4.2 align-self属性
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
这个属性理解就行
<style>
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
</style>
4.3 align-grow属性
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
<style>
.item {
flex-grow: <number>; /* default 0 */
}
</style>
flex - grow 决定了 flex items 如何扩展:
- 可以设置任意非负数字(正小数、正整数、0),默认值是0
- 当 flex container 在 main axis 方向上有剩余 size 时, flex - grow 属性才会有效
如果所有 flex items 的 flex - grow 总和 sum 超过1,每个 flex item 扩展的 size 为:
- flex container 的剩余 size * flex - grow / sum
如果所有 flex items 的 flex - grow 总和不超过1,每个 flex item 扩展的 size 为:
- flex container 的剩余 size * flex - grow
4.4 flex-shrink属性(少用)
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
<style>
.item {
flex-shrink: <number>; /* default 1 */
}
</style>
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。
4.5 flex-basis属性(少用)
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
<style>
.item {
flex-shrink: <number>; /* default 1 */
}
</style>
flex - basis 用来设置 flex items 在 main axis 方向上的 base size:
- auto (默认值)、具体的宽度数值(100px)
决定 flex items 最终 base size 的因素,从优先级高到低:
- max - width \ max - height \ min - width \ min - height flex - basis
- width \ height
- 内容本身的 size
4.6 flex属性(少用)
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
<style>
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
</style>
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。