html - 如何在 flex-direction 为 column 和 flex-wrap 为 wrap 的 flex box 中将容器宽度设置为 child

标签 html css flexbox

<分区>

这是工作代码:

.container{
  display: flex;
  background-color: lightgreen;
  justify-content: center;
  alighn-item: center;
}

.child{
  margin-left: 10px;
  height: 200px;
  background-color: yellow;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.childItem{
  height: 50px;
  width: 100px;
  background-color: red;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0 0 10px;
}
<div class='container'>
  <div>Test</div>
  <div class='child'>
    <div class="childItem">Amounts</div>
    <div class="childItem">Amounts</div>
    <div class="childItem">Amounts</div>
    <div class="childItem">Amounts</div>
    <div class="childItem">Amounts</div>
  </div>
</div>

当前输出

enter image description here

预期输出

enter image description here

我们希望这个子容器(黄色)宽度应该等于子项(红色)元素

最佳答案

.test-container{
  background-color: lightgreen;
  padding: 20px 0px;
}
.container{
  background-color: lightgreen;
  width:300px;
  margin:auto;
}

.child{
  margin-left: 10px;
  height: 200px;
  background-color: yellow;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: center;
}

.childItem{
  height: 50px;
  width: 100px;
  background-color: red;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0 0 10px;
}
<div class="test-container">
  <div class='container'>
    <div>Test</div>
    <div class='child'>
      <div class="childItem">Amounts</div>
      <div class="childItem">Amounts</div>
      <div class="childItem">Amounts</div>
      <div class="childItem">Amounts</div>
      <div class="childItem">Amounts</div>
    </div>
  </div>
</div>

关于html - 如何在 flex-direction 为 column 和 flex-wrap 为 wrap 的 flex box 中将容器宽度设置为 child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962844/

相关文章:

在函数中返回未定义的 Javascript 原型(prototype)属性

html - CSS 网格环绕

css - 对齐内容不工作 Internet Explorer 11

javascript - 强制 Iframe 进入折叠 Bootstrap 组件以适合屏幕

javascript - 如何更改脚本中的 CHANGE ME 文本

html - 循环中的剪辑路径在 Safari 浏览器中无法正常工作

HTML:如何设置子元素宽度=浏览器窗口宽度?

html - 使用*ngIf时如何防止闪烁?

java - HttpGet 无法识别网址

html - 使用 CSS Flex 使行中的元素等高