About flex-grow / flex-shrink / flex-basis in flex

Reasons to use flex

  • To create layouts

  • To make it easier to place elements


A shorthand for flex's containers

flex-flow : /* column nowrap; /* Position the items in the container vertically, does not wrap */




flex-grow

  • Actions when there is extra space in the container

  • flex-grow defaults to 0

  • flex-glow works when the setting is greater than 0

  • The higher the number, the larger the scale ratio

  • When the screen is small, it will keep the set width, but when the screen gets bigger and there is extra space, it will make the element wider by that ratio

  • Only one item can flex-grow:1, the width of that item will take up all the extra space (same as flex:1)

  • You often come across the flex-grow:1 pattern

grow(The image above has the browser set to wide

Numbers 3 and 4 are fixed at 50px

And number 1 is a larger percentage than 50px, and number 2 is even larger than that)

flex-.shrink

  • Activated when there is no extra space in the container

  • When it becomes smaller than the default width, it shrinks by a percentage of flex-shrink

  • flex-shrink defaults to 1.

  • flex-shrink works for values greater than 0.

  • The higher the number, the larger the shrink ratio

  • When the screen is large, it will keep the set width

  • But when the screen is small and there is no extra space, it will make the element narrower by that ratio

shrink( The image above is set to narrow in the browser

Numbers 3 and 4 are fixed at 50px

Number 2 is a smaller percentage than 50px and number 1 is even smaller than that )

flex-basis

  • Default is auto

  • Indicates the default width of the item in flex

  • 100px,60%, etc. (useful to use % for layout)

  • The difference with width is that it depends on the axis direction (width when flex-direction is row, height when flex-direction is column)


A shorthand for flex items

flex : flex-grow flex-shrink flex-basis

flex : 1 is often used in the form of 1


flex: 0 0 200px; /* cannot be larger or smaller than 200px */
.item{
   flex: 1; /* flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
   flex: 1 500px; /* flex-grow: 1; flex-shrink: 1; flex-basis: 500px; */
}

basis( Sets the percentage of each item at the beginning of the screen.

1 uses 10% , 2 uses 20% , 3 uses 30% , 4 uses 40% as the width of the item

This will be useful in layouts )



Other

1. nth-child

li:nth-child(2) /* 2nd element of child li tag */
li:nth-child(2n) /* Even numbered element of child li tag */


2. Difference between vh and %

height:100vh (based on the current screen viewport)

height:100% (based on the parent, so if the parent's height is not specified, it cannot be used)

So there are cases where html,body tags cannot be used unless height:100% is used


3. border abbreviated

border: 1px solide black;



References

YouTube1*

YouTube2

YouTube3

References

Apr 13, 2024 Views 109