【问题标题】:How to have a flex container take the space of its elements? [duplicate]如何让弹性容器占据其元素的空间? [复制]
【发布时间】:2017-11-25 20:17:48
【问题描述】:

我想要一个有两行的盒子,使用flexbox。我无法解决的问题是容器占用了页面的整个宽度,而不是紧密地适应元素:

.container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border-style: solid;
}

.header {
  background-color: black;
  color: white;
}

.body {
  background-color: blue;
  color: white;
}
<div class="container">
  <div class="header">
    the header
  </div>
  <div class="body">
    the body
  </div>
</div>

应该设置什么以使容器只是元素的宽度?我知道我可以强制使用它的 width,但我希望它受到元素内容的驱动。

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    您正在寻找inline-flex

    https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes

    .container {
      display: inline-flex;
      flex-direction: column;
      align-items: flex-start;
      border-style: solid;
    }
    
    .header {
      background-color: black;
      color: white;
    }
    
    .body {
      background-color: blue;
      color: white;
    }
    <div class="container">
      <div class="header">
        the header
      </div>
      <div class="body">
        the body
      </div>
    </div>

    【讨论】:

    猜你喜欢
    • 2013-08-05
    • 2016-01-08
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2021-10-22
    相关资源
    最近更新 更多