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!
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With