Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a flexbox layout, is there a `flex-shrink: 0` declaration for the cross axis?

I want to prevent elements in a flex container from shrinking in the dimension that is not the flex-direction. The following example has <article> elements side by side in a row. When the available vertical space is reduced, these elements do not force their flex container to display a scrollbar; instead the content overflows the element boundary.

Screenshot 1 - there is enough horizontal and vertical space to display everything:

flex screenshot 1

Screenshot 2 - the reduced vertical space pushes the element border up:

flex screenshot 2

Screenshot 3 - vertical space further reduced, container finally gets a scrollbar:

flex screenshot 3

Screenshot 4 - without flex-shrink:0, the element widths (main flex axis) will also be reduced:

flex screenshot 4

flex-shrink:0 can prevent horizontal shrinking, but how can I prevent the elements from shrinking vertically?

Giving the <article> elements overflow: auto or something similar does not give the desired result (= scrollbar on the container). Ideally, the display would look like this montage:

flex montage

If I knew the elements' height in advance, I could give them a min-height, but that is not always the case.

FIDDLE: http://jsfiddle.net/twdan8u8/

HTML:

<main>
    <article>article<br>article<br>article</article>
    <article>article<br>article<br>article</article>
</main>

CSS:

* {
    box-sizing: border-box; /* not the culprit */
}
html {
    height: 100%;
}
body {
    position: relative;
    height: 100%;
    margin: 0;
    background: #999;
}
main {
    overflow: auto;
    background: gold;
    display: flex;
    height: 80%;
    padding: 50px 30px;
}
article {
    flex-shrink: 0;
    font-size: 28px;
    border: 2px solid red;
    margin-right: 30px;
    padding: 10px;
}
like image 861
Zilk Avatar asked Mar 18 '15 13:03

Zilk


People also ask

Does Flex grow work on cross axis?

To make a flex item "grow" in the cross axis direction, align-self: stretch; on that item should work. This is only useful if the container already has align-items set to something like center .

Which of the following properties does the flexbox layout have?

Alignment, justification and distribution of free space between items. A key feature of flexbox is the ability to align and justify items on the main- and cross-axes, and to distribute space between flex items. Note that these properties are to be set on the flex container, not on the items themselves.

How are the main and cross axes related in flexbox?

The cross axis in flexbox runs perpendicular to the main axis, therefore if your flex-direction is either row or row-reverse then the cross axis runs down the columns. If your main axis is column or column-reverse then the cross axis runs along the rows.

Which property do you use to specify how Flex items are positioned along the cross axis?

The flex item's main size property is either the 'width' or 'height' property, whichever is in the main dimension. cross axis – The axis perpendicular to the main axis is called the cross axis. Its direction depends on the main axis direction.


1 Answers

As is so often the case, I found the (or rather a) solution just when I finished writing the question. Since this might help somebody else, here's what I found out:

If the flex container is given the style align-items: flex-start, element heights are not reduced and the container gets a scrollbar when necessary (assuming a suitable overflow value).

The default for this property is "stretch". It can also be set on individual flex elements using align-self. The drawback is that the elements are now no longer equally high (i.e., they don't stretch to the full available height anymore).

like image 139
Zilk Avatar answered Sep 29 '22 12:09

Zilk