<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.main{
width: 500px;
height:300px;
background-color: skyblue;
display: flex;
}
.main div{
width: 100px;
height: 100px;
background-color: pink;
/* flex-grow: 0; 默认值 */
}
</style>
<body>
<div class="main">
<div>1</div>
</div>
</body>
</html>
上述运行结果:
若修改 flex-grow: 1;
上述运行结果:
若修改 flex-grow: 0.5;
上述运行结果:
如果 flex-grow: 1;或者flex-grow大于1时,则会占满所有剩余空间
如果 flex-grow: 0.5;则占满的空间为100(自己本身)+(500-100)*0.5(剩余部分的0.5)=300
当有多个元素的时候:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.main2{
width: 500px;
height:300px;
background-color: skyblue;
display: flex;
}
.main2 div:nth-of-type(1){
width:200px;
height: 100px;
background-color: pink;
flex-grow: 2;
}
.main2 div:nth-of-type(2){
width:100px;
height: 100px;
background-color: pink;
flex-grow: 1;
}
</style>
<body>
<div class="main2">
<div>1</div>
<div>2</div>
</div>
</body>
</html>
上述运行结果:
以上是将剩余部分分成了3份,
第一个div块的大小是200+ (500-300)*2/3
第一个div块的大小是100+ (500-300)*1/3
如果flex-grow < 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.main2{
width: 500px;
height:300px;
background-color: skyblue;
display: flex;
}
.main2 div:nth-of-type(1){
width:200px;
height: 100px;
background-color: pink;
flex-grow: 0.2;
}
.main2 div:nth-of-type(2){
width:100px;
height: 100px;
background-color: pink;
flex-grow: 0.1;
}
</style>
<body>
<div class="main2">
<div>1</div>
<div>2</div>
</div>
</body>
</html>
上述运行结果:
第一个div块的大小是200+ (500-300)*0.2
第一个div块的大小是100+ (500-300)*0.1