Flex and Grid in CSS
Flex
Cheat Sheet provide by CSS-tricks.com
display
|
|
flex-direction
|
|
flex-direction
flex-warp
|
|
flex-wrap
flex-flow
This is a shorthand for the flex-direction and flex-wrap properties.
|
|
order
|
|
order
flex-grow
This defines the ability for a flex item to grow when there is extra space from container.
0
means fix
|
|
In this example, container is 1100px,
item base width is 100px
. The left space is 1100 - 100 * 3 = 800
. This 800px
will base on the proportion 1 : 2 : 1
to distribute to each item.
800 / 4 = 200
the final with will be 300px (100+200); 500px (100+400); 300px
flex-grow
flex-shrink
This defines the ability for a flex item to shrink when item size overflow.
0
means fix
|
|
In this example, item will overflow 300 * 2 - 300= 300px
.item will shrink base on the proportion 1 : 2
. Item will be 200px (300-100); 100px(300-200)
flex-basis
This defines the default size of an element before the remaining space is distributed.
If set to auto, it will take width as starting size to distribute space. If set to a number, it will take the given number as starting size to distribute space.
|
|
If set to 0, the item actual width will be decided by space distribution only. Because this item is no space at beginning.
|
|
0 vs auto after distribute space
If set to 0, the extra space around content isn’t factored in. If set to auto, the extra space is distributed based on its flex-grow value.
All space distributed vs Extra space distributed
flex
This is the shorthand for flex-grow, flex-shrink and flex-basis combined.
Default is 0 1 auto
|
|
flex: 1
|
|
This is a common styling that given to an item which can take all the extra space for a flexbox.
We analysis this line and see why it is work. item starting from 0 size, Not shrink and take extra space for 1 proportion. If no other item give flex-grow
(default is 0), this item will take all extra space
justify-content
This defines the alignment along the main axis.
|
|
justify-content
align-content
This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content
aligns individual items within the main-axis.
align-content
align-items
|
|
align-items
align-self
This allows the alignment to be overridden for individual flex items.
|
|
align-self
grid
Cheat Sheet provide by CSS-tricks.com
display
|
|
grid-template-columns & grid-template-rows
values:
- <track-size> – can be a length, a percentage, or a fraction of the free space in the grid (using the fr unit)
- <line-name> – an arbitrary name of your choosing
|
|
Example:
|
|
grid example
If assign name to each line:
|
|
grid example
grid-column & grid-row
Determines a grid item’s location within the grid by referring to specific grid lines.
|
|
Example:
|
|
grid-column-row
grid-template-areas & grid-area
|
|
grid-template-area
grid-template
A shorthand for setting grid-template-rows
, grid-template-columns
, and grid-template-areas
in a single declaration.
|
|
gap (grid-gap)
|
|
grid-gap
justify-items
|
|
justify-items-center
justify-items-start
justify-items-end
justify-items-stretch
align-items
|
|
align-items-center
align-items-start
align-items-end
align-items-stretch
place-items
place-items
sets both the align-items
and justify-items
properties in a single declaration.
justify-content
|
|
justify-content-center
justify-content-start
justify-content-end
justify-content-stretch
justify-content-space-around
justify-content-space-between
justify-content-space-evenly
align-content
|
|
align-content-center
align-content-start
align-content-end
align-content-stretch
align-content-space-around
align-content-space-between
align-content-space-evenly
place-content
place-content
sets both the align-content
and justify-content
properties in a single declaration.
justify-self
|
|
justify-self-center
justify-self-start
justify-self-end
justify-self-stretch
grid-auto-columns & grid-auto-rows
Specifies the size of any auto-generated grid tracks (aka implicit grid tracks).
Example:
|
|
explicit grid
If use grid-auto-columns
:
|
|
implicit grid
grid-auto-flow
If you have grid items that you don’t explicitly place on the grid, the auto-placement algorithm kicks in to automatically place the items.
|
|
Example:
|
|
implicit grid
|
|
implicit grid
grid
A shorthand for setting all of the following properties in a single declaration: grid-template-rows
, grid-template-columns
, grid-template-areas
, grid-auto-rows
, grid-auto-columns
, and grid-auto-flow
The Most Powerful Lines in Grid
1 2 3
.container { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }