flexbox - 在指定flex-grow值时阻止增加宽度

Mot*_*oto 9 css css3 flexbox

我正在尝试使用flexbox来构建一个大小相同的响应网格.如果单元格的内容相同,我的代码可以工作,但如果单元格的内容长度不同,则代码不起作用 - 单元格会重新调整以适应其内容.

这就是我所追求的:
1
.每个单元格宽度相同2.每个单元格的宽度在浏览器调整大小时自动调整.

这是我的小提琴代码:https://jsfiddle.net/v0erefnd/1/

HTML:

<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">100</div>
    <div class="flex-item">10,000</div>
    <div class="flex-item">1,000,000</div>
    <div class="flex-item">100,000,000</div>
    <div class="flex-item">10,000,000,000</div>
    <div class="flex-item">1,000,000,000,000</div>
</div>
<div class="flex-container">
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
    <div class="flex-item">100</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.flex-container {
    padding: 0;
    margin: 0;
    list-style: none;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row;
    justify-content: space-around;
    height: 50px;
    line-height:30px;
}

.flex-item {
    /*todo: how to prevent the flexbox to resize to accommodate the text?*/
    background: tomato;
    margin: 10px 5px;
    color: white;
    font-weight: bold;
    font-size: 1.5em;
    text-align: center;
    flex-grow: 1;
    overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Jos*_*ier 10

如果您希望所有flexbox项具有相同的宽度,可以添加flex-basis: 100%:

更新的示例

.flex-item {
    background: tomato;
    margin: 10px 5px;
    color: white;
    font-weight: bold;
    font-size: 1.5em;
    text-align: center;
    flex-basis: 100%;
    overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)

  • 我会使用`flex-basis:0px`代替(更有意义)或短缺`flex:1 0 0px`,但+1 (4认同)