I'm trying to use display: flex
, but some inline-block
s should never be shrunk. I'm not sure how to accomplish that.
Here's a repro of my situation:
.marker { width: 2em; height: 2em; background-color: #cef; border: 1px solid gray; margin-right: 5px; } label { display: flex; align-items: flex-start; }
<div class="container" style="width: 200px; border: 1px solid #ccc; padding: 10px;"> <p>The blue boxes should be 2em wide!</p> <label> <span class="marker"></span> <span class="text">Some rather long text which is the whole reason for using flexbox.</span> </label> <label> <span class="marker"></span> <span class="text">Short text.</span> </label> </div>
The problem is that .marker
is not of specified width: 2em
, but instead is rather narrow.
I've read through css-tricks' helpful Guide to Flexbox. The only property that seemed useful was flex-shrink
(I was expecting something of a "never shrink" property), but found no way to use it for my purpose. I've skimmed the MDN flex pages but couldn't find a solution there either. Lastly, I've also carefully reviewed the "duplicate" suggestions Stack Overflow gave me when I wrote this question, but no solution there either.
How can you specify a flex item to never shrink beyond a certain minimum width?
A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.
By default, the child elements of a flexbox container will stretch vertically to fill the height of the container. This can be prevented by using the align-self property on the child element that you do not want to stretch. This Pen is owned by dev on CodePen.
flex-basis is limited by both max-width/max-height and min-width/min-height. When declared, flex-basis will override the width / height property set on a flex container.
You can use flex-basis
and the flex-shrink
properties.
The CSS flex-basis property specifies the flex basis which is the initial main size of a flex item. The property determines the size of the content-box unless specified otherwise using box-sizing.
Change your .marker
as follows:
.marker { flex: 0 0 2em; height: 2em; background-color: #cef; border: 1px solid gray; } label { display: flex; align-items: flex-start; }
<div class="container" style="width: 200px; border: 1px solid #ccc; padding: 10px;"> <p>The blue box should be 2em wide!</p> <label> <span class="marker"></span> <span class="text">Some rather long text which is the whole reason for using flexbox.</span> </label> </div>
Alternatively you could continue using width: 2em
and use just this flex-shorthand:
.marker { flex: 0 0 auto; }
The problems you are experiencing are caused by the default values of the flex
property. Each child of a flex-container is becoming a flex item with: flex-grow:1; flex-shrink: 1; flex-basis: 0%;
(See here for more info).
This properties allows your flex item to shrink, which is not wanted in your implementation.
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