How to make a flex item to shrink in secondary axis?
For example, I have container which has lots of items inside. They all are positioned in column, but I want they width also can be shrinked:
.container {
display: flex;
flex-direction: column;
.item {
width: 200px;
flex: 0 1 @item-height;
//flex-shrink here means Y-axis. How to set flex-shrink for X-axis??
}
}
Changing flex-direction Setting flex-direction: row-reverse will keep the items displaying along the row, however the start and end lines are switched. If we change flex-direction to column the main axis switches and our items now display in a column. Set column-reverse and the start and end lines are again switched.
Flex-basis overrules any specified CSS width value, so if you set flex-basis to 0, it doesn't fall back to the CSS "width" value.
Approach: To create a two-column layout, first we create a <div> element with property display: flex, it makes that a div flexbox and then add flex-direction: row, to make the layout column-wise. Then add the required div inside the above div with require width and they all will come as columns.
The cross space or cross size is calculated according to the layout algorithm:
Determine the available main and cross space for the flex items. For each dimension, if that dimension of the flex container’s content box is a definite size, use that; if that dimension of the flex container is being sized under a min or max-content constraint, the available space in that dimension is that constraint; otherwise, subtract the flex container’s margin, border, and padding from the space available to the flex container in that dimension and use that value. This might result in an infinite value.
https://www.w3.org/TR/css-flexbox-1/#layout-algorithm
The cross-axis is affected by align-items
and can be affected by width
, max-width
, height
, max-height
, etc: Cross-axis MDN notes
So for the equivalent of flex-shrink on the cross axis, you could try using align-items
and for the equivalent of flex-basis
you can use percentage max-width
/max-height
.
You can simply use width: 100%; max-width: 200px;
on .item
. This should cause your items to shrink to the width of the parent element when it's below 200px. Otherwise the items stay at 200px width.
Works at least as long as you don't use flex-wrap.
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