简体   繁体   English

Flex 容器使我的 div 无法转到下一行

[英]Flex container makes my divs not go to the next line

I'm new to flex containers, but I'm trying to have a container with justify: space-around, and have some divs inside it.我是 flex 容器的新手,但我试图拥有一个带有 justify: space-around 的容器,并且里面有一些 div。 If the screen gets too small, the inner divs should space out to the next line, but instead the inner divs stubbornly stay on the same line and just adjust their widths weirdly.如果屏幕太小,内部 div 应该间隔到下一行,但内部 div 固执地保持在同一行上,只是奇怪地调整它们的宽度。

.container{
  display: flex;
  justify-content: space-around;
}
.inner-div{
  width: 40%;
  display: inline-block;
}

You would think if I had three inner divs in the container, the third one would just go to the next line, considering they're all have width: 40%, but instead they stay on the same line and squish the first then second div.你会认为如果我在容器中有三个内部 div,第三个就会转到下一行,考虑到它们都有宽度:40%,但它们保持在同一行并挤压第一个然后第二个 div .

flex-wrap:wrap; will work in this case.在这种情况下会起作用。

The CSS flex-wrap property is used to specify whether flex items are forced into a single line or wrapped onto multiple lines. CSS flex-wrap属性用于指定是否将 flex 项强制为一行或多行。 The flex-wrap property allows enabling the control direction in which lines are stacked. flex-wrap属性允许启用线条堆叠的控制方向。 It is used to designate a single line or multi-line format to flex items inside the flex container.它用于指定单行或多行格式以在 flex 容器内弹性项目。

Syntax : flex-wrap: nowrap|wrap|wrap-reverse|initial;语法: flex-wrap: nowrap|wrap|wrap-reverse|initial;

Set your flex-wrap to wrap .将您的flex-wrap设置为wrap I just answered the same question like an hour ago.一个小时前我刚刚回答了同样的问题。 https://stackoverflow.com/a/59553958/1195615 https://stackoverflow.com/a/59553958/1195615

在其他方面,我建议您使用媒体查询而不是 flex,这样您就可以控制不同的分辨率/设备大小。

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

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