WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

CSS 3中flex-basis属性用法详解

更新时间:2014-07-22 12:12 作者:佚名点击:
下面我对于flex-basis属性不了解我们就一起来看看CSS3中flex-basis属性用法吧,希望例子能帮助到各位哦。

WebPlatform1上对flex-basis的解释是:

The flex-basis CSS property describes the initial main size of the flex item before any free space is distributed according to the flex factors described in the flex property (flex-grow and flex-shrink).
在flex container分配剩余空间前,flex-basis决定flex item在主方向上的大小。
它的取值有两种2:

Percentages refer to the flex container’s inner main size
Computed value as specified, with lengths made absolute
百分比 – 根据flex container的主方向大小计算

计算值 – 绝对数
计算值#

flex-grow跟flex-shrink取默认值时,计算值的flex-basis类似max-width。
在flex-container主方向大小不足以容纳flex items的flex-basis总和时,浏览器会自动缩小它们。
举一段代码说明:

 代码如下  
<div class='Grid'>
  <div class='first'>我曾在天上见过地的繁华</div>
  <div class='last'>陈三说的</div>
</div>
<style>
.example-Grid{
  color: white;
  display: flex;
  width: 400px;
  border: 1px solid red;
}
.example-first{
  background: green;
  flex-basis: 400px;
}
.example-last{
  background: orange;
  flex-basis:200px;
}
</style>

样式结果如下:

CSS3中flex-basis属性用法详解

这里,.example-first的宽度是267px,.example-last的宽度是133px,它们是这样计算的:

 代码如下  
.example-first(宽度) = 400 * (400 / (400 + 200)) = 266.666666667
.example-last(宽度) = 400 * (200 / (400 + 200)) = 133.333333333

也就是说,flex container按比例分配flex items的大小。

百分比#

百分比的情况与计算值是一样的,如果flex container足够包含flex items的flex-basis总值,则10%的意思就是flex container在主方向的大小乘以10%。
如果flex container不足以包含flex items的flex-basis总值,比如:

 代码如下  
<div class="example2-Grid">
 <div></div>
 <div></div>
 <div></div>
 <div></div>
</div>
<style>
.example2-Grid{
     border: 3px solid black;
     display: flex;
     height: 200px;
}
.example2-Grid div:nth-of-type(1){
     background: rgb(0, 137, 250);
     flex-basis: 100%;
}
.example2-Grid div:nth-of-type(2){
     background: rgb(105, 0, 88);
     flex-basis: 20%;
}
.example2-Grid div:nth-of-type(3){
     background: rgb(255, 59, 0);
     flex-basis: 30%;
}
.example2-Grid div:nth-of-type(4){
     background: rgb(0, 197, 73);
     flex-basis: 40%;
}
</style>

代码的样式如下:

CSS3中flex-basis属性用法详解

其中第一个flex item的flex-basis取值为100%,则计算时,它的main size占比是:

 代码如下  
100% / (100% + 20% + 30% + 40%) = 52.631578947%

真正设计或实现页面时,我们通常不可能做这样的计算,但了解计算过程的话,心里有底,碰上问题,就知道怎么解决。

顶一下
(5)
55.6%
踩一下
(4)
44.4%
------分隔线----------------------------
你可能感兴趣的内容