您的位置
主页 > 网站技术 > CSS/HTML > » 正文

CSS3 Flexbox中flex-shrink属性的用法示例介绍

来源: 站长圈 点击:

点评:当flex items的大小超过了flex container时, 各个flex item的压缩比例,下面给大家介绍个不错的教程,有需要的朋友可以参考下!

在CSS3 Flexbox中flex-shrink属性定义为: 

This <number> component sets ‘flex-shrink’ longhand and specifies the flex shrink factor, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when negative free space is distributed. When omitted, it is set to ‘1’. The flex shrink factor is multiplied by the flex basis when distributing negative space. 

通俗来讲就是当flex items的大小超过了flex container时, 各个flex item的压缩比例, 请看下面的示例: 

复制代码代码如下:

<style> 

#container p { 

height: 200px; 

width: 60px; 

#test1 { 

background-color: blue; 

flex-shrink: 1; 

#test2 { 

background-color: yellow; 

flex-shrink: 0.5; 

</style> 

<p id="container"> 

<p id="test1"></p> 

<p id="test2"></p> 

</p> 

<p id="test1">与<p id="test2">的宽度总和是120px, 超过了<p id="container">的宽度100px, 超过的大小为20px, 那么container为了装下两个子p,两个子p的宽度就必须减少20px,那么每个子p的宽度减少多少呢? 这个时候就需要flex-shrink属性来分配了,每个子p的实际显示宽度计算方法公式为: 

实际值 = 计划值 - 总差值 * flex-shrink/(flex-shrink和) 

根据上面的公式我们可以计算出<p id="test1">与<p id="test2">的实际宽度值分别为: 

复制代码代码如下:

<p id="test1">: width = 60 - 20 * 1 / (1 + 0.5) = 47px 

<p id="test2">: width = 60 - 20 * 0.5 / (1 + 0.5) = 53px 

根据以上结果可知flex-shrink值越大,flex item的实际结果就会越小。




首页  - 关于站长圈  - 广告服务  - 联系我们  - 关于站长圈  - 网站地图  - 版权声明