当前位置:网站首页>弹性布局(display:flex;)属性详解
弹性布局(display:flex;)属性详解
2022-06-25 14:27:00 【AnnGreen】
原文链接:https://www.cnblogs.com/hellocd/p/10443237.html
弹性布局(display:flex;)属性详解
Flexbox 是 flexible box 的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式。它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来。
它之所以被称为 Flexbox ,是因为它能够扩展和收缩 flex 容器内的元素,以最大限度地填充可用空间。与以前布局方式(如 table 布局和浮动元素内嵌块元素)相比,Flexbox 是一个更强大的方式:
- 在不同方向排列元素
- 重新排列元素的显示顺序
- 更改元素的对齐方式
- 动态地将元素装入容器
一、基本概念
采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称"项目"。

在 Flexbox 模型中,有三个核心概念:
– flex 项(注:也称 flex 子元素),需要布局的元素
– flex 容器,其包含 flex 项
– 排列方向(direction),这决定了 flex 项的布局方向
二、容器属性

2.1 flex-direction:
- row(默认值):主轴为水平方向,起点在左端。
- row-reverse:主轴为水平方向,起点在右端。
- column:主轴为垂直方向,起点在上沿。
- column-reverse:主轴为垂直方向,起点在下沿。

2.2 flex-wrap:
- nowrap(默认):不换行。
- wrap:换行,第一行在上方。
- wrap-reverse:换行,第一行在下方。
2.3 justify-content:
- flex-start(默认值):左对齐
- flex-end:右对齐
- center: 居中
- space-between:两端对齐,项目之间的间隔都相等。
- space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

2.4 align-items:
- flex-start:交叉轴的起点对齐。
- flex-end:交叉轴的终点对齐。
- center:交叉轴的中点对齐。
- baseline: 项目的第一行文字的基线对齐。
- stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

2.5 align-content:
定义了多根轴线的对齐方式,如果项目只有一根轴线,那么该属性将不起作用
- flex-start:与交叉轴的起点对齐。
- flex-end:与交叉轴的终点对齐。
- center:与交叉轴的中点对齐。
- space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
- space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
- stretch(默认值):轴线占满整个交叉轴。

结合 justify-content和align-items,看看在 flex-direction 两个不同属性值的作用下,轴心有什么不同:

三、项目属性

3.1 order属性

3.2 flex-grow属性
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

3.3 flex-shrink属性
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
.item {
flex-shrink: <number>; /* default 1 */
}

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。
3.4 align-self属性
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

弹性布局默认不改变项目的宽度,但是它默认改变项目的高度。如果项目没有显式指定高度,就将占据容器的所有高度。
参考:http://www.ruanyifeng.com/blog/2018/10/flexbox-form.html
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
边栏推荐
- China has made major breakthroughs in battery technology. Japan, South Korea and the United States are lagging behind. China has consolidated its leading edge
- Tencent cloud builds a Socks5 multi IP proxy server to realize the perfect building of a game with a single window and a single IP. Tutorial attached tool "suggestions collection"
- Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
- ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析
- Realization of neural networks with numpy
- Supplementary inheritance and strict mode
- The best screenshot tool in the world, color absorption tool snipaste
- Two methods to rollback the code in pycharm to the specified version (with screenshot)
- 广发易淘金和同花顺哪个更好,更安全一些
- Typescript and go --- essence
猜你喜欢

Test your earning power? What will you do in the future?

还没弄明白微服务数据架构事务管理+ACID+一致性+CAP+BASE理论吗,看完彻底解决疑惑

To make pytorch faster, you need to master these 17 methods

About the problem of kicad stuck in win10 version, version 6 x

英語中的九大詞性與九大時態

Kubernetes 理解kubectl/调试

Logistic Regression VS Linear Regression

Pourquoi les programmeurs devraient - ils être plus doux?

Beego--- notes

多台云服务器的 Kubernetes 集群搭建
随机推荐
[Ocean University of China] Data Sharing for retest of initial Examination
API encapsulation of uniapp applet
Share the code technology points and software usage of socket multi client communication
Shell array
Differences between member variables and local variables
JVM 用工具分析OOM经典案例
Heavyweight! The domestic IDE is released and developed by Alibaba. It is completely open source! (high performance + high customization)
JS functions
[untitled] the CMD command window displays' NPM 'which is not an internal or external command
2020-03-20
Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
Async await to achieve sleep waiting effect
Page 112 machine learning - review of fundamentals of mathematics pptx
What is the safest app for stock account opening? Tell me what you know
‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
One time summary: 64 common terms for data analysis!
【世界历史】第一集——石器时代的人们
JGG | 河北大学杜会龙组综述植物泛基因组学研究
Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts
[HBZ sharing] use of locksupport