flex属性中的flex-grow、flex-shrink、flex-basis

flex-grow 属性

flex-grow 属性用于设置或检索弹性盒子的扩展比率。

默认值为0,表示不伸展。

flex-grow属性值为0时,不伸展:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-grow: 0;">1</div>
			<div class="content">2</div>
		</div>
	</body>
</html>

flex-grow属性值为1时伸展铺满整个容器:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-grow: 1;">1</div>
			<div class="content">2</div>
			<div class="content">3</div>
		</div>
	</body>
</html>

flex-grow属性值为2时,该项目的伸展比例是flex-grow属性值为1的项目的两倍:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-grow: 1;">1</div>
			<div class="content" style="flex-grow: 2;">2</div>
			<div class="content">3</div>
		</div>
	</body>
</html>
flex-shrink

flex-shrink 属性指定了 flex 元素的收缩规则。flex 元素仅在默认宽度之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。

默认值为1,代表当项目空间不足时子item等比例收缩,设置为0时则不会收缩,item会超出容器范围。

flex-shrink属性值为0时,超出容器不收缩,其余item平均分配剩余空间:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-shrink: 0;">1</div>
			<div class="content">2</div>
			<div class="content">3</div>
			<div class="content">4</div>
			<div class="content">5</div>
		</div>
	</body>
</html>

flex-shrink属性值为2时,该项目的收缩比例是flex-shrink属性值为1的项目的两倍:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-shrink: 0;">1</div>
			<div class="content" style="flex-shrink: 1;">2</div>
			<div class="content" style="flex-shrink: 1;">3</div>
			<div class="content" style="flex-shrink: 2;">4</div>
			<div class="content" style="flex-shrink: 2;">5</div>
		</div>
	</body>
</html>
flex-basis

flex-basis 属性用于设置或检索弹性盒伸缩基准值。

默认值为auto,可设置一个长度单位或者一个百分比。

决定item最终size的因素,从优先级高到低:

max-width\max-height\min-width\min-height

flex-basis

width\height

内容本身的 size

flex-basis属性值设置:

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex-basis: 50%;">1</div>
			<div class="content" style="flex-basis: 50px;">2</div>
			<div class="content" style="flex-basis: 200px;">3</div>
		</div>
	</body>
</html>
flex

flex 属性用于设置或检索弹性盒模型对象的子元素如何分配空间。

flex 属性是 flex-grow、flex-shrink 和 flex-basis 属性的简写属性。

CSS语法:

flex: flex-grow flex-shrink flex-basis|auto|initial|inherit;

auto 与 1 1 auto 相同。(伸展、收缩) :

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: auto;">flex: auto;</div>
			<div class="content"></div>
		</div>
		<div class="d-flex">
			<div class="content" style="flex: 1 1 auto;">flex: 1 1 auto;</div>
			<div class="content"></div>
		</div>
	</body>
</html>

none 与 0 0 auto 相同。(不伸展、不收缩):

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: none;">flex: none;</div>
			<div class="content"></div>
		</div>
		<div class="d-flex">
			<div class="content" style="flex: 0 0 auto;">flex: 0 0 auto;</div>
			<div class="content"></div>
		</div>
	</body>
</html>

initial 设置该属性为它的默认值,即为 0 1 auto。(不伸展、收缩):

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: initial;">flex: initial;</div>
			<div class="content"></div>
			<div class="content"></div>
			<div class="content"></div>
			<div class="content"></div>
		</div>
		<div class="d-flex">
			<div class="content" style="flex: 0 1 auto;">flex: 0 1 auto;</div>
			<div class="content"></div>
			<div class="content"></div>
			<div class="content"></div>
			<div class="content"></div>
		</div>
	</body>
</html>
flex单值语法

无单位数(flex: <number>): 它会被当作flex:<number> 1 0 的值。flex-shrink的值被假定为1,即可以伸缩。flex-basis的值被假定为0,默认是没有宽度的。

有效的宽度值: 它会被当作 <flex-basis> 的值。

给一个item设置flex: <number>

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 1;">flex: 1;</div>
			<div class="content"></div>
			<div class="content"></div>
		</div>
	</body>
</html>

给多个item设置flex: <number>

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 1;">flex: 1;</div>
			<div class="content" style="flex: 2;">flex: 2;</div>
			<div class="content" style="flex: 3;">flex: 3;</div>
		</div>
	</body>
</html>

给item设置有效宽度值(item总长度大于父容器,所以item会等比列收缩):

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 500px;">flex: 500px;</div>
			<div class="content"></div>
			<div class="content"></div>
		</div>
	</body>
</html>
flex双值语法

第一个值必须为一个无单位数,并且它会被当作 <flex-grow> 的值。

第二个值必须为以下之一:

一个无单位数:它会被当作 <flex-shrink> 的值。

一个有效的宽度值: 它会被当作 <flex-basis> 的值。

flex: <number> <number>

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 0 0;">flex: 0 0;</div>
			<div class="content" style="flex: 1 0;">flex: 1 0;</div>
			<div class="content" style="flex: 1 1;">flex: 1 1;</div>
			<div class="content"></div>
		</div>
	</body>
</html>

flex: <number> 有效宽度值

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 0 0;">flex: 0 0;</div>
			<div class="content" style="flex: 0 150px;">flex: 0 150px;</div>
			<div class="content" style="flex: 1 150px;">flex: 1 150px;</div>
			<div class="content"></div>
		</div>
	</body>
</html>
flex三值语法

第一个值必须为一个无单位数,并且它会被当作 <flex-grow> 的值。

第二个值必须为一个无单位数,并且它会被当作 <flex-shrink> 的值。

第三个值必须为一个有效的宽度值, 并且它会被当作 <flex-basis> 的值。

html 复制代码
<!doctype html>
<html lang="en">
	<head>
		<style>
			.d-flex {
				display: flex;
				width: 800px;
				height: 100px;
				border: 1px solid black;
			}
		
			.content {
				width: 200px;
				height: 100px;
				background-color: skyblue;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="d-flex">
			<div class="content" style="flex: 0 0 100px;">flex: 0 0 100px;</div>
			<div class="content" style="flex: 1 0 150px;">flex: 1 0 150px;</div>
			<div class="content" style="flex: 1 1 200px;">flex: 1 1 200px;</div>
			<div class="content"></div>
		</div>
	</body>
</html>
相关推荐
前端工作日常2 分钟前
H5 实时摄像头 + 麦克风:完整可运行 Demo 与深度拆解
前端·javascript
韩沛晓13 分钟前
uniapp跨域怎么解决
前端·javascript·uni-app
前端工作日常14 分钟前
以 Vue 项目为例串联eslint整个流程
前端·eslint
程序员鱼皮16 分钟前
太香了!我连夜给项目加上了这套 Java 监控系统
java·前端·程序员
Rubin9337 分钟前
TS 相关
javascript
该用户已不存在42 分钟前
这几款Rust工具,开发体验直线上升
前端·后端·rust
前端雾辰1 小时前
Uniapp APP 端实现 TCP Socket 通信(ZPL 打印实战)
前端
无羡仙1 小时前
虚拟列表:怎么显示大量数据不卡
前端·react.js
云水边1 小时前
前端网络性能优化
前端
用户51681661458411 小时前
[微前端 qiankun] 加载报错:Target container with #child-container not existed while devi
前端