How to size each square the same???
Preview Issue on JSBIN | View Issue Code on JSBIN
关于JSBIN的预览问题|查看JSBIN上的问题代码
I am using flexbox to create a grid layout. Most grid cells do not have content and some grid cells do have content. When a cell has content it throws everything off. how can I make sure all cells are even regardless of content?
我正在使用flexbox创建网格布局。大多数网格单元没有内容,一些网格单元确实有内容。当一个单元格有内容时,它会抛弃一切。如何确保所有细胞均匀,无论内容如何?
HTML
I have three rows, each with three cells. Only the center row of the center cell contains children.
我有三行,每行有三个单元格。只有中心单元格的中心行包含子项。
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div><span>contains span</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
CSS
In the css the center space is larger than the other squares.
在css中,中心空间比其他方格大。
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body>div {
display: flex;
}
body>div>div {
border: solid 1px blue;
}
div {
flex-grow: 1;
}
body>div:nth-child(2)>div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
3 个解决方案
#1
7
You shall use flex-basis
to specify the initial length of the flexible item.
您应使用flex-basis指定灵活项目的初始长度。
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body>div {
display: flex;
}
body>div>div {
border: solid 1px blue;
}
div {
flex-grow: 1;
flex-basis: 80px; /*set the initial length*/
}
body>div:nth-child(2)>div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div><span>contains span asdsa asd asd asd asdsad ads asd sad</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
#2
6
You are using flex-grow: 1
. That means that the initial width of the flex items will be the width of its content, and then the available space will be distributed equally.
您正在使用flex-grow:1。这意味着flex项的初始宽度将是其内容的宽度,然后可用空间将平均分配。
However, you want the flex items to have the same width, so you want to ignore the width of their content. You can achieve this with
但是,您希望弹性项具有相同的宽度,因此您希望忽略其内容的宽度。你可以实现这一目标
flex: 1;
Additionally, Flexbox introduces auto
as the initial value of min-width
. This might produce different widths in some cases, so to be safe better add min-width: 0
or set overflow
to anything but visible
此外,Flexbox引入auto作为min-width的初始值。在某些情况下,这可能会产生不同的宽度,因此为了更安全,最好添加min-width:0或将溢出设置为除可见之外的任何内容
body,
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body > div {
display: flex;
}
body > div > div {
border: solid 1px blue;
}
div {
flex: 1;
min-width: 0;
}
body > div:nth-child(2) > div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div><span>contains span</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
#3
1
It's just a matter of setting a 33% flex-basis
这只是设置33%弹性基础的问题
body>div>div {
border: solid 1px blue;
flex: 0 0 33%;
}
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body>div {
display: flex;
}
body>div>div {
border: solid 1px blue;
flex: 0 0 33%;
}
div {
flex-grow: 1;
}
body>div:nth-child(2)>div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div><span>contains span</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
#1
7
You shall use flex-basis
to specify the initial length of the flexible item.
您应使用flex-basis指定灵活项目的初始长度。
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body>div {
display: flex;
}
body>div>div {
border: solid 1px blue;
}
div {
flex-grow: 1;
flex-basis: 80px; /*set the initial length*/
}
body>div:nth-child(2)>div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div><span>contains span asdsa asd asd asd asdsad ads asd sad</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
#2
6
You are using flex-grow: 1
. That means that the initial width of the flex items will be the width of its content, and then the available space will be distributed equally.
您正在使用flex-grow:1。这意味着flex项的初始宽度将是其内容的宽度,然后可用空间将平均分配。
However, you want the flex items to have the same width, so you want to ignore the width of their content. You can achieve this with
但是,您希望弹性项具有相同的宽度,因此您希望忽略其内容的宽度。你可以实现这一目标
flex: 1;
Additionally, Flexbox introduces auto
as the initial value of min-width
. This might produce different widths in some cases, so to be safe better add min-width: 0
or set overflow
to anything but visible
此外,Flexbox引入auto作为min-width的初始值。在某些情况下,这可能会产生不同的宽度,因此为了更安全,最好添加min-width:0或将溢出设置为除可见之外的任何内容
body,
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body > div {
display: flex;
}
body > div > div {
border: solid 1px blue;
}
div {
flex: 1;
min-width: 0;
}
body > div:nth-child(2) > div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div><span>contains span</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
#3
1
It's just a matter of setting a 33% flex-basis
这只是设置33%弹性基础的问题
body>div>div {
border: solid 1px blue;
flex: 0 0 33%;
}
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
body>div {
display: flex;
}
body>div>div {
border: solid 1px blue;
flex: 0 0 33%;
}
div {
flex-grow: 1;
}
body>div:nth-child(2)>div:nth-child(2) {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<div></div>
<div><span>contains span</span></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>
<div>
<div></div>
<div></div>
<div></div>
</div>