我想使用具有一些宽度相同的项目的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个项目,我希望它们全部出现在同一行,每个项目的空间量相等.
有任何想法吗?
Ada*_*dam 196
设置它们,这样它们flex-basis是0(因此,所有元素都具有相同的起点),并允许他们成长:
the unit of measure 'px' is redundant
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)
小智 39
width: 0如果项目的内容使其变大,则需要添加以使列相等.
.item {
flex: 1 1 0;
width: 0;
}
Run Code Online (Sandbox Code Playgroud)
这是帮助我解决问题的链接: 总是等于flexbox列
这些解决方案都不适合我,但这确实有效:
.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)
Adam(flex: 1 1 0)接受的答案非常适合宽度固定或由祖先确定的flexbox容器。您希望孩子们装满容器的情况。
但是,在某些情况下,您可能希望容器适合孩子,并且孩子的大小与最大孩子相同。您可以通过以下任一方法使flexbox容器适合其子容器:
position: absolute,而不设置width或right,或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)
这些答案都没有解决我的问题,即当我的临时弹性盒表缩小到太小的宽度时,项目的宽度不一样。
对我来说,解决方案就是简单地放置overflow: hidden;细胞flex-grow: 1;。