简体   繁体   English

为什么flex-basis属性被覆盖?

[英]why is flex-basis property getting overridden?

Fiddle: http://jsfiddle.net/y8d2q3ww/2/ 小提琴: http//jsfiddle.net/y8d2q3ww/2/

.flex-container{display:flex;width:100%}
.square{flex-basis:55px;background:yellow}

<div class='flex-container'>
    <div class="square">M</div>
    <div class="text">some text some text some text some text some text some text some text some text </div>    
</div>

Only when the text doesn't wrap, the .square's width will be 55px as the flex-basis property sets; 只有当文本没有换行时,.square的宽度将是55px,因为flex-basis属性设置; but if the text is more than one line, the actual width will be scaled down. 但如果文本不止一行,则实际宽度将缩小。 So what is behind this? 那么这背后是什么? And how do I make the .square area have a fixed width(be it px or %) no matter what content the other flex items hold? 并且如何使.square区域具有固定宽度(无论是px还是%),无论其他弹性项目具有什么内容?

.square shrinks if .text overflows, because you did not specify flex-shrink , which defaults to 1. 如果.text溢出,.square会收缩,因为您没有指定flex-shrink ,默认为1。

Set it to 0 to get the desired result: 将其设置为0以获得所需的结果:

 .flex-container { display: flex; width: 100% } .square { flex-basis: 20%; flex-shrink: 0; background: yellow } 
 <div class='flex-container'> <div class="square">M</div> <div class="text">some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM