Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set flex-basis on secondary axis

Tags:

css

flexbox

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??
  }
}
like image 668
VB_ Avatar asked Jul 21 '16 17:07

VB_


People also ask

Does Flex-direction change main 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.

What is Flex basis 0%?

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.

How do you set a flex column?

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.


2 Answers

Flexbox Terms

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.

like image 127
Alex W Avatar answered Oct 19 '22 09:10

Alex W


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.

like image 20
Johannes Avatar answered Oct 19 '22 09:10

Johannes