css flex布局 —— 项目属性 flex-grow

flex-grow 属性定义项目的放大比例,解决的问题是:在空间有多余时把多余空间分配给各个子元素。

flex-grow 的值默认为 0,也就是说,如果存在剩余空间,也不放大。

flex-grow 取值为非负数(如果取值为负数那么和0的取值效果相同)。

语法

.item {
  flex-grow: <number>; /* default 0 */
}

flex-grow 默认为0

flex-grow 默认为 0 时:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 900px;
      height: 200px;
      background-color: #e6e6e6;
    }
    .item {
      width: 50px;
      height: 50px;
    }
    .container div:nth-of-type(1) {background-color:coral;}
    .container div:nth-of-type(2) {background-color:lightblue;}
    .container div:nth-of-type(3) {background-color:khaki;}
    .container div:nth-of-type(4) {background-color:pink;}
    .container div:nth-of-type(5) {background-color:lightgrey;}
    .container div:nth-of-type(6) {background-color:orange;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
</body>
</html>

可以看到,flex-grow 默认值为 0 时,并不会占用剩余的空白间隙扩展自己的宽度,页面效果如下:
在这里插入图片描述

flex-grow 值为正整数

如果flex-grow大于0,则flex容器剩余空间的分配就会发生。

概念:将所有正整数相加得到分母 sum,每个属性的数值作为分子,然后乘以剩余空间。

公式:分子/分母*剩余空间

平均分配空间

如果所有项目的 flex-grow 属性都为1,则它们将等分剩余空间(如果有的话)。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 900px;
      height: 200px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
      flex:1;
    }
    .container div:nth-of-type(1) {background-color:coral;}
    .container div:nth-of-type(2) {background-color:lightblue;}
    .container div:nth-of-type(3) {background-color:khaki;}
    .container div:nth-of-type(4) {background-color:pink;}
    .container div:nth-of-type(5) {background-color:lightgrey;}
    .container div:nth-of-type(6) {background-color:orange;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
</body>
</html>

页面效果:
在这里插入图片描述

不平均分配空间

如果一个项目的 flex-grow属性不为1,则按照公式来计算每个元素占有的空间。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 900px;
      height: 200px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
    }
    .container div:nth-of-type(1) {flex-grow: 1;background-color:coral;}
    .container div:nth-of-type(2) {flex-grow: 3;background-color:lightblue;}
    .container div:nth-of-type(3) {flex-grow: 1;background-color:khaki;}
    .container div:nth-of-type(4) {flex-grow: 2;background-color:pink;}
    .container div:nth-of-type(5) {flex-grow: 4;background-color:lightgrey;}
    .container div:nth-of-type(6) {flex-grow: 1;background-color:orange;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
</body>
</html>

页面效果:
在这里插入图片描述

flex-grow 值为小数

flex-grow 属性值为小数,分两种情况:
1)所有 flex 项的 flex-gorw 属性值之和大于1,仍然按照上面正整数方式进行计算;
2)所有 flex 项的 flex-gorw 属性值之和小于1,基值按照1来进行计算。

例子:项目1为 0.1, 项目2为 0.3,项目3为 0.1, 项目4为 0.2,项目5为 0.1,则它们分配到的剩余空间分别为:

项目1: 900px * (0.1 / 1) = 94.7px;
项目2: 900px * (0.3 / 1) = 265.31px;
项目3: 900px * (0.1 / 1) = 94.7px;
项目4: 900px * (0.2 / 1) = 180px;
项目5: 900px * (0.1 / 1) = 94.7px;

实例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 900px;
      height: 200px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
    }
    .container div:nth-of-type(1) {flex-grow: 0.1;background-color:coral;}
    .container div:nth-of-type(2) {flex-grow: 0.3;background-color:lightblue;}
    .container div:nth-of-type(3) {flex-grow: 0.1;background-color:khaki;}
    .container div:nth-of-type(4) {flex-grow: 0.2;background-color:pink;}
    .container div:nth-of-type(5) {flex-grow: 0.1;background-color:lightgrey;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>
</body>
</html>

页面效果:
在这里插入图片描述

flex-grow 值为小数+正整数

flex-grow 值为小数+正整数时,计算方式与正整数一样,如分母取值:sum=1+0.6+0.5+0.2+2=4.3,分子分别为:1/4.3、0.6/4.3、0.5/4.3、0.2/4.3、2/4.3

实例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 900px;
      height: 200px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
    }
    .container div:nth-of-type(1) {flex-grow: 1;background-color:coral;}
    .container div:nth-of-type(2) {flex-grow: 0.6;background-color:lightblue;}
    .container div:nth-of-type(3) {flex-grow: 0.5;background-color:khaki;}
    .container div:nth-of-type(4) {flex-grow: 0.2;background-color:pink;}
    .container div:nth-of-type(5) {flex-grow: 2;background-color:lightgrey;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>
</body>
</html>

页面效果:
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值