简体   繁体   English

如何计算 flexbox 容器的子元素的实际宽度?

[英]How to calculate the actual width of child elements of a flexbox container?

I have such html and css code.我有这样的 html 和 css 代码。 I have two questions:我有两个问题:

  1. When the window width is 600px, in theory, the left box should be of 150px width, because it's the 25% of the parent(600px), but the width is 120px.当 window 宽度为 600px 时,理论上左框应为 150px 宽度,因为它是父框(600px)的 25%,但宽度为 120px。
  2. The right box can not reach 100vw.右框不能达到100vw。 It just takes the widthOfParent - widthOfLeft .它只需要widthOfParent - widthOfLeft I suppose it should somehow overflow and reach 100vw.我想它应该以某种方式溢出并达到100vw。
 <div class="container">
        <div class="left"></div>
        <div class="right"></div>
 </div>
* {
  margin: 0;
  padding: 0;
}
.container {
  height: 300px;
  /* your code here */
  display: flex;
  justify-content: start;
}

.left {
  background-color: #f44336;
  height: 100%;
  width: 25%;
  min-width: 100px;
}

.right {
  background-color: #2973af;
  height: 100%;
  width: 100vw;
}

codesanbox: https://codesandbox.io/s/admiring-darkness-cu99x?file=/src/styles.css:0-293代码框: https://codesandbox.io/s/admiring-darkness-cu99x?file=/src/styles.css:0-293

  1. use "flex" property instead of width for the flex items对弹性项目使用“flex”属性而不是宽度
.container { height: 300px; width: 600px; /* added fixed width for testing */ display: flex; justify-content: start; }.left { background-color: #f44336; min-width: 100px; flex: 1; /* 1/4 ratio within the parent element, as the other flex item in the parent is "flex: 3;" */ }.right { background-color: #2973af; flex: 3; /* 3/4 ratio within the parent element, as the other flex item in the parent is "flex: 1;" */ }
  1. The right element cannot take 100% of the width, as it is together with the left div inside the flex parent and we assigned the width parameter as "flex: value" for both右侧元素不能占据 100% 的宽度,因为它与 flex 父级中的左侧 div 一起,我们将宽度参数分配为“flex: value”

CSS flex Property https://www.w3schools.com/cssref/css3_pr_flex.asp CSS flex 属性https://www.w3schools.com/cssref/css3_pr_flex.asp

You are facing the shrink effect.你正面临收缩效应。 In all the cases the total width 100vw + 25% is bigger than 100% so both items will shrink equally.在所有情况下,总宽度100vw + 25%大于100% ,因此两个项目将同等收缩。

Since your container is also full width, the overflow will always be equal to 25% or 25vw .由于您的容器也是全宽的,因此溢出将始终等于25%25vw Both element have the default shrink value 1 so we have a sum equal to 125vw .这两个元素都有默认的收缩值1所以我们的总和等于125vw

The first element width will be equal to: 25vw - (25vw * 25vw/125vw) = 20vw and the width of the second item will be: 100vw - (25vw * 100vw/125vw) = 80vw第一个元素的宽度将等于: 25vw - (25vw * 25vw/125vw) = 20vw ,第二个元素的宽度将是: 100vw - (25vw * 100vw/125vw) = 80vw

You can logically see that the total is 100vw ( 20vw + 80vw ) and when the screen width is equal to 600px , 20vw is equal 120px .你可以从逻辑上看到总数是100vw ( 20vw + 80vw ),当屏幕宽度等于600px时, 20vw等于120px

To avoid this, disable the shrink effect on the first item by setting flex-shrink:0为避免这种情况,请通过设置flex-shrink:0禁用第一项的收缩效果

 * { margin: 0; padding: 0; }.container { height: 300px; /* your code here */ display: flex; }.left { background-color: #f44336; width: 25%; min-width: 100px; flex-shrink:0; }.right { background-color: #2973af; width: 100vw; }
 <div class="container"> <div class="left"></div> <div class="right"></div> </div>

Related: Why is a flex item limited to parent size?相关:为什么弹性项目仅限于父尺寸?

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

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