Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flexbox - prevent increasing the width when specifying flex-grow value

Tags:

css

flexbox

I'm trying to use flexbox to build a responsive grid of equally sized cells. My code works if the contents of the cells is the same, but does not if the contents of the cells varies in length - the cells re-adjust to accommodate their content.

Here's what I'm after:
1. every cell is the same width
2. each cell's width auto-adjusts when the browser is resized.

Here's my code on fiddle: 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>

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;
}

Thanks!

like image 452
Moto Avatar asked Mar 02 '15 19:03

Moto


1 Answers

If you want all of the flexbox items to have the same width, you could add flex-basis: 100%:

Updated Example

.flex-item {
    background: tomato;
    margin: 10px 5px;
    color: white;
    font-weight: bold;
    font-size: 1.5em;
    text-align: center;
    flex-basis: 100%;
    overflow: hidden;
}
like image 56
Josh Crozier Avatar answered Oct 11 '22 16:10

Josh Crozier