简体   繁体   English

flex项忽略宽度

[英]flex items ignoring width

I've reordered some elements in my html using flexbox in the responsive design of a website which works fine but the elements then won't resize properly. 我在一个网站的响应式设计中使用flexbox在我的html中重新排序了一些元素,这些元素工作正常,但元素则不会正确调整大小。

At a breakpoint I have applied a class of flex to the home-promos div and reordered the elements. 在断点处,我已经将一类flex应用于home-promos div并重新排序元素。 This works correctly. 这工作正常。

The problem then arises when I try to resize the div's to percentage widths. 当我尝试将div的大小调整为百分比宽度时,就会出现问题。 They will only resize up to a certain point, such as 50% and then won't get any bigger. 它们只会调整到某个点,例如50%,然后不会变大。

Is anyone who is better with flexbox than myself able to tell me what the issue is? Flexbox比我更好的人能告诉我这是什么问题吗?

 .home-promos { display: flex; } .home-promo-center { order: 1; } .home-promo-left { order: 2; } .home-promo-right { order: 3; } 
 <div class="home-promos"> <div class="home-promo-left"> <div class="promo-left-content"> *content* </div> </div> <div class="home-promo-center"> <div class="promo-center-content"> *content* </div> </div> <div class="home-promo-right"> <div class="promo-right-content"> *content* </div> </div> </div> 

When you create a flex container ( display: flex or display: inline-flex ), it comes with several default settings. 当您创建Flex容器( display: flexdisplay: inline-flex )时,它会带有几个默认设置。 Among them are: 其中包括:

  • flex-direction: row - flex items will align horizontally flex-direction: row - flex项目将水平对齐
  • justify-content: flex-start - flex items will stack at the start of the line justify-content: flex-start - flex项将在行的开头堆叠
  • flex-wrap: nowrap - flex items are forced to stay in a single line flex-wrap: nowrap - flex项目被强制保留在一行中
  • flex-shrink: 1 - flex items are allowed to shrink flex-shrink: 1 - 允许flex项缩小

Note the last two settings. 请注意最后两个设置。

Your three divs are forced to remain in a single line. 你的三个div被迫留在一条线上。 Hence, their combined width is limited to the width of the container. 因此,它们的组合宽度限于容器的宽度。

Also, they are allowed to shrink, which prevents them from overflowing the container. 此外,它们可以收缩,从而防止它们溢出容器。 This also limits their width. 这也限制了它们的宽度。

To apply whatever width you want to each flex item you can override these initial settings with: 要为每个弹性项目应用任何宽度,您可以使用以下方式覆盖这些初始设置:

  1. flex-wrap: wrap - now there's more space because flex items can break to new lines flex-wrap: wrap - 现在有更多空间,因为flex项可以分解为新行
  2. flex-shrink: 0 - now there's more space because flex items will not shrink and can overflow their container if necessary flex-shrink: 0 - 现在有更多空间,因为flex项目不会缩小,并且必要时可以溢出容器

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

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