html - 以不同宽度呈现的一行中的 Flex 元素

标签 html css google-chrome flexbox

我在 html 中使用 css flexbox 创建了 3 个盒子,但 Chrome 正在渲染不同尺寸的盒子。

enter image description here

拖动浏览器的边框使 View 变小。

感谢您的帮助!

代码笔:https://codepen.io/labanino/pen/rGaNLP

body {
  font-family: Arial;
  font-size: 2rem;
  text-transform: uppercase;
}

.flex {
  height: 200px;
  display: flex;
  flex-direction: row;
}

.flex>section {
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
}

.flex>section:nth-child(1) {
  background: brown;
  margin-right: 20px;
  border: 1px solid #999;
}

.flex>section:nth-child(2) {
  background: pink;
  margin-right: 20px;
  border: 1px solid #999;
}

.flex>section:nth-child(3) {
  background: green;
  margin-right: 0;
  border: 1px solid #999;
}
<div class="flex">
  <section>Brown</section>
  <section>Pink</section>
  <section>Green</section>
</div>

最佳答案

不要在元素的 flex 属性中使用 auto。使用 100%,这将调整 flex box 的宽度,并根据您添加的任意数量的元素进行调整(以防您要添加超过 3 个)。

.flex > section {
  flex: 1 1 100%;  
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
}

它们大小不同的原因是因为当您使用 auto 时,它的宽度基于框的内容。这就是中间那个没那么宽的原因,因为“粉色”这个词比“绿色”这个词占用的空间更少。

关于html - 以不同宽度呈现的一行中的 Flex 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46207618/

相关文章:

javascript - 在调整浏览器窗口大小时防止 jQuery 弹出窗口换行

javascript - 带滚动条的 HighChart 多重导出问题

angularjs - 显示柔性动画

php - 使用 __Secure-/__Host- 前缀重命名 PHP session cookie

css - 伪元素的 3D 转换

javascript - 如何更改 Swiper Cover Flow Slider 中幻灯片的渲染流程

html - @media 屏幕(最小宽度 :1200px) not working in Chrome

html - 当窗口大小改变时保持元素定位

c++ - Qt QGraphicsView 和样式表

jquery - 为什么位置:fixed is not working in Chrome version 59?