如何使用flexbox将div的宽度调整为内容

时间:2017-07-26 08:00:21

标签: html css css3 flexbox

我想调整.flexrow div的宽度到内容,但我不能用flex设置它。

HTML

<div class="fullwidth">
  <div class="sidebar">
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
  </div>
  <div class="center">
    <div class="flexrow">
      <div class="card">
        <p>card</p>
      </div>
    </div>
  </div>
</div>

CSS

.center {
  display: flex;
  justify-content: center;
}

.flexrow {
  display: inline-flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.flexrow .card {
  width: 300px;
  height: 60px;
  background-color: red;
  margin: 10px;
  padding: 10px;
  text-align: center;
}

我创建了一个代码段here

这是我想要的一个例子: example

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

这里的主要问题是,当项目换行时,无论是否使用Flexbox,其容器都不会将其宽度调整为新的内容宽度。

使用现有的标记,这是一个简单的修复,分两步实现:

  1. justify-content: center;添加到您的.flexrow规则

  2. 对于每个可能的列,您需要少一个 ghost 元素来将最后一行的元素一直推到左侧。添加的元素以及新的.flexrow .card:empty规则将起到魔力。

    .flexrow .card:empty {
      height: 0;
      margin: 0 10px;
      padding: 0 10px;
    }
    
  3. Updated codepen

    Stack snippet

    &#13;
    &#13;
    .fullwidth {
      background-color: grey;
      display: flex;
    }
    
    .sidebar {
      flex: 0 0 280px;
      margin: 10px;
      padding: 10px;
      background-color: black;
      color: white;
    }
    
    .flexrow {
      display: flex;
      flex-wrap: wrap;
      align-content: center;
      justify-content: center;
    }
    
    .flexrow .card {
      width: 300px;
      height: 60px;
      background-color: red;
      margin: 10px;
      padding: 10px;
      text-align: center;
    }
    
    .flexrow .card:empty {
      height: 0;
      margin: 0 10px;
      padding: 0 10px;
    }
    &#13;
    <div class="fullwidth">
      <div class="sidebar">
        <p>sidebar</p>
        <p>sidebar</p>
        <p>sidebar</p>
        <p>sidebar</p>
        <p>sidebar</p>
        <p>sidebar</p>
      </div>
      <div class="center">
        <div class="flexrow">
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
          <div class="card">
            <p>card</p>
          </div>
    
          <!-- Ghost elements, if max columns is 4 one need 3 -->
          <div class="card"></div>
          <div class="card"></div>
          <div class="card"></div>
    
        </div>
      </div>
    </div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

试试这个代码示例

.flexrow .card {
  height: 60px;
  background-color: red;
  margin: 10px;
  padding: 10px;
  text-align: center;
}

我删除了宽度,以便flexrow可以适应内容。 这是你想要的吗?如果没有请说明(也许是图片?)

答案 2 :(得分:0)

我想您想要的是,如果您希望侧边栏和内容并排,则需要在flex div上使用fullwidth,请检查以下代码:

&#13;
&#13;
.sidebar {
  background: black;
  color: white;
}

.fullwidth {
  display: flex;
}

.center {
  display: flex;
  justify-content: center;
  align-items:center;
  width:100%;
}

.flexrow {
  display: inline-flex;
  flex-direction: row;
  flex-wrap: wrap;
  width:100%;
}

.flexrow .card {
  width: calc(33.33% - 20px);
  box-sizing:border-box;
  height: 60px;
  background-color: red;
  margin: 10px;
  padding: 10px;
  text-align: center;
}
&#13;
<div class="fullwidth">
  <div class="sidebar">
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
    <p>sidebar</p>
  </div>
  <div class="center">
    <div class="flexrow">
      <div class="card">
        <p>card</p>
      </div>
      <div class="card">
        <p>card</p>
      </div>
      <div class="card">
        <p>card</p>
      </div>
      <div class="card">
        <p>card</p>
      </div>
      <div class="card">
        <p>card</p>
      </div>
      <div class="card">
        <p>card</p>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

<强>更新

试试这个:

 .flexrow {
  width: 90%;
  display: inline-flex;  
  flex-direction: row;
  flex-wrap: wrap;
  -webkit-align-content: center;
  align-content: center;
  -webkit-justify-content: space-between;
  justify-content: space-between;  
  margin: 0 auto;  
}
.flexrow:after {  
    content: "";  
    width: 300px;
    margin: 10px;
    padding: 10px;
    text-align: center;    
}

希望你正在寻找的东西:)