如何使flexbox项目大小相同?

Che*_*het 129 css flexbox

我想使用具有一些宽度相同的项目的flexbox.我注意到flexbox会均匀地分布空间,而不是空间本身.

例如:

.header {
  display: flex;
}

.item {
  flex-grow: 1;
  text-align: center;
  border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)
<div class="header">
  <div class="item">asdfasdfasdfasdfasdfasdf</div>
  <div class="item">z</div>
</div>
Run Code Online (Sandbox Code Playgroud)

第一项比第二项大很多.如果我有3个项目,4个项目或n个项目,我希望它们全部出现在同一行,每个项目的空间量相等.

有任何想法吗?

http://codepen.io/anon/pen/gbJBqM

Ada*_*dam 196

设置它们,这样它们flex-basis0(因此,所有元素都具有相同的起点),并允许他们成长:

the unit of measure 'px' is redundant

  • 应该注意的是,`flex`属性是`flex-grow`,`flex-shrink`和`flex-basis`属性的简写属性.建议使用单个属性的简写,因为速记正确地重置任何未指定的组件以适应常见用途.初始值为"0 1 auto". (50认同)
  • 我必须添加 `min-width: 0` 来实现使用带有溢出文本的子元素。请参阅 https://css-tricks.com/flexbox-truncated-text/ (20认同)
  • 注意IE11会忽略无单位的flex-basis值:https://github.com/philipwalton/flexbugs#flexbug-4 (2认同)
  • 我只想提一下,Internet Explorer (IE) 应该被忽略。这是一个过时的浏览器,例如 Netscape Navigator。IE正式被Microsoft Edge取代。最新版本还可以在 Chromium 上运行(如 Chrome 和其他现代浏览器)。作为 Web 开发人员,我们不应再支持 IE。你有责任摆脱这个拐杖。:) 永远不要再谈论 IE。 (2认同)

Jos*_*ier 58

您可以添加flex-basis: 100%以实现此目的.

更新的示例

.header {
  display: flex;
}

.item {
  flex-basis: 100%;
  text-align: center;
  border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)

对于它的价值,您也可以使用flex: 1相同的结果.

简写flex: 1是相同的flex: 1 1 0,相当于:

.item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 0;
  text-align: center;
  border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)

  • @ r.sendecky不,你是不正确的人.正如我在我的回答中所说,`flex:1`的简写导致`flex:1 1 0`******flex:1 1 auto`就像你声称的那样.看看['flex shorthand'部分]下的官方W3规范(https://www.w3.org/TR/css-flexbox-1/#flex-property):当`flex-basis`是"从flex简写中省略,其指定值为"0",而不是"auto".感谢随机的downvote. (10认同)
  • @JoshCrozier确认“ flex:1”具有与“ flex:1 1 0”相同的行为,而“ flex:0 1 auto”具有不同的行为。 (3认同)
  • 这是不正确的。默认的 flex 值为 `flex: 0 1 auto`。这意味着设置 `flex: 1` 将导致 `flex: 1 1 auto`。这是行不通的。正确答案是`flex: 1 1 0`,正如亚当所说 (2认同)
  • @ r.sendecky-另外,请看一下我创建的[此示例](https://jsfiddle.net/o25wto2d/)以可视化这些变化。 (2认同)

小智 39

width: 0如果项目的内容使其变大,则需要添加以使列相等.

.item {
  flex: 1 1 0;
  width: 0;
}
Run Code Online (Sandbox Code Playgroud)

这是帮助我解决问题的链接: 总是等于flexbox列

  • 如果`.item`的内容使它比`.item`更宽,那么这似乎是必要的. (5认同)
  • 这应该是公认的答案! (4认同)
  • 仅供参考,链接已损坏 (3认同)
  • 我认为你的意思是“flex: 1 1 0;”,而不是“flex-grow: 1 1 0;” (2认同)
  • 这是唯一适合我的情况的解决方案。似乎就我而言,孩子们比自然生长得更大,并且“flex:1 1 0”没有任何效果 (2认同)

sol*_*xel 7

这些解决方案都不适合我,但这确实有效:

.header {
  display: flex;
}

.item {
  width: 100%;
}


/* Demo styles, for aesthetics. */

.demo {
  margin: 3rem;
}

.demo .item {
  text-align: center;
  padding: 3rem;
  background-color: #eee;
  margin: 0 1.5rem;
}
Run Code Online (Sandbox Code Playgroud)
<div class="demo">
  <div class="header">
    <div class="item">
      1
    </div>
    <div class="item">
      2
    </div>
    <div class="item">
      3
    </div>
  </div>
</div>

<div class="demo">
  <div class="header">
    <div class="item">
      1
    </div>
    <div class="item">
      2
    </div>
  </div>
</div>

<div class="demo">
  <div class="header">
    <div class="item">
      1
    </div>
    <div class="item">
      2
    </div>
    <div class="item">
      3
    </div>
    <div class="item">
      4
    </div>
    <div class="item">
      5
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Bre*_*ald 6

Adam(flex: 1 1 0)接受的答案非常适合宽度固定或由祖先确定的flexbox容器。您希望孩子们装满容器的情况。

但是,在某些情况下,您可能希望容器适合孩子,并且孩子的大小与最大孩子相同。您可以通过以下任一方法使flexbox容器适合其子容器:

  • 设置position: absolute,而不设置widthright,或
  • 把它放在一个包装里 display: inline-block

对于这样的Flexbox的容器,接受的答案确实工作,孩子们不是同等大小。我认为这是flexbox的局限性,因为它在Chrome,Firefox和Safari中的行为相同。

解决方案是使用网格而不是flexbox。

演示:https : //codepen.io/brettdonald/pen/oRpORG

<p>Normal scenario — flexbox where the children adjust to fit the container — and the children are made equal size by setting {flex: 1 1 0}</p>

<div id="div0">
  <div>
    Flexbox
  </div>
  <div>
    Width determined by viewport
  </div>
  <div>
    All child elements are equal size with {flex: 1 1 0}
  </div>
</div>

<p>Now we want to have the container fit the children, but still have the children all equally sized, based on the largest child. We can see that {flex: 1 1 0} has no effect.</p>

<div class="wrap-inline-block">
<div id="div1">
  <div>
    Flexbox
  </div>
  <div>
    Inside inline-block
  </div>
  <div>
    We want all children to be the size of this text
  </div>
</div>
</div>

<div id="div2">
  <div>
    Flexbox
  </div>
  <div>
    Absolutely positioned
  </div>
  <div>
    We want all children to be the size of this text
  </div>
</div>

<br><br><br><br><br><br>
<p>So let's try a grid instead. Aha! That's what we want!</p>

<div class="wrap-inline-block">
<div id="div3">
  <div>
    Grid
  </div>
  <div>
    Inside inline-block
  </div>
  <div>
    We want all children to be the size of this text
  </div>
</div>
</div>

<div id="div4">
  <div>
    Grid
  </div>
  <div>
    Absolutely positioned
  </div>
  <div>
    We want all children to be the size of this text
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
body {
  margin: 1em;
}

.wrap-inline-block {
  display: inline-block;
}

#div0, #div1, #div2, #div3, #div4 {
  border: 1px solid #888;
  padding: 0.5em;
  text-align: center;
  white-space: nowrap;
}

#div2, #div4 {
  position: absolute;
  left: 1em;
}

#div0>*, #div1>*, #div2>*, #div3>*, #div4>* {
  margin: 0.5em;
  color: white;
  background-color: navy;
  padding: 0.5em;
}

#div0, #div1, #div2 {
  display: flex;
}

#div0>*, #div1>*, #div2>* {
  flex: 1 1 0;
}

#div0 {
  margin-bottom: 1em;
}

#div2 {
  top: 15.5em;
}

#div3, #div4 {
  display: grid;
  grid-template-columns: repeat(3,1fr);
}

#div4 {
  top: 28.5em;
}
Run Code Online (Sandbox Code Playgroud)

  • @Davorin`grid-template-columns:重复(自动填充,minmax(100px,auto));`更多详细信息请访问https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill -vs-自动调整/ (3认同)

Ste*_*eve 5

我不是 flex 方面的专家,但我是通过将我正在处理的两个项目的基础设置为 50% 来实现的。增长到 1 并收缩到 0。

内联样式: flex: '1 0 50%',


And*_*rew 5

这些答案都没有解决我的问题,即当我的临时弹性盒表缩小到太小的宽度时,项目的宽度不一样。

对我来说,解决方案就是简单地放置overflow: hidden;细胞flex-grow: 1;