CSS Layout Flexbox
CSS Layout Flexbox
Flex Items
Main Axis: primary axis on which ßex
items are laid out (↔ or !)
Cross Axis:
perpendicular
to the main axis
(↔ or !)
Main Start
Cross End
Cross Start
Main End
Assumes flex-direction: column
Flex Container
Main Size
Flex Item
Main Size
Flex Item
Cross Size
Flex Container
Cross Size
Flex container
Flex items
Main axis
Cross axis
↔
↔
display: flex column-gap
display: inline-flex row-gap
gap
flex-direction
flex-wrap
flex-flow
justify-content
align-content
place-content
align-items
order
align-self
flex-grow
flex-shrink
flex-basis
flex
Triggering
Flexbox Layout
display: flex (<inside>)
display: block flex (<outside> <inside>)
flex 8* 12 28 9 9 29 4.4
inline-flex 11 12 28 9 9 29 4.4
flex-wrap
flex-flow
order
flex-direction
Values:
È row (default) !
È row-reverse !
È column !
È column-reverse !
flex-direction: row
Values:
È nowrap (default)
È wrap
È wrap-reverse
flex-wrap: nowrap
Value: <integer>
flex-direction 11 12 28 9 9 29 4.4
flex-wrap 11* 12 28 9 9 29 4.4
flex-flow 11 12 28 9 9 29 4.4
order 11 12 28 9 9.2 29 4.4
align-items column-gap
align-self row-gap
gap
Remember that distributed alignment focuses on
distributing space among aligned boxes, e.g., stretch,
space-around, space-between, & space-evenly
Flex items Flex lines*
Main axis justify-content
align-items
Cross axis align-self align-content
* Requires flex-wrap: wrap ** Alas, these terms have different relationships in grid layout
!PRO TIP
Never use width & height with ßexbox layout! Instead of
width & height, size items using these for the cross axis:
È align-items: stretch
È align-content: stretch
È align-self: stretch
È flex-basis
È flex-start (default)
È flex-end
È center
È space-between
È space-around
È space-evenly
È start
È end
justify-content: flex-start
Aligns ßex items to the start of the main axis of the flex
container
Aligns ßex items to the end of the main axis of the flex
container
justify-content: center
Empty space before the Þrst, and after the last, ßex
items equals half of the space between two adjacent
items
Space before 1st ßex item (&
after last ßex item) is 1/2 of
space between ßex items
justify-content: space-evenly
Empty space before the Þrst, and after the last, ßex
items equals the space between two adjacent items
Space before Þrst ßex item &
after last ßex item equal to
space between ßex items
justify-content:* 11 12 29 9 9.2 29 4.4
space-evenly Ð 79 52 11 11 60 Y
Aligns flex items in the cross axis of the current ßex line
Values for align-items:
È stretch (default)
È flex-start
È flex-end
È center
È baseline
È start
È end
align-items: stretch
Flex items fill the whole height (or width) from cross
start to cross end of the ßex container
Flex items stack from the cross end of the ßex container
align-items: center
Flex items stack from the center of the cross axis of the
ßex container
Horizontally
and vertically
centered!
" # $ %&
align-items: baseline
È auto (default)
È flex-start
È flex-end
È center
È baseline
È stretch
È start
È end
align-self: auto
Flex item stacks from the cross end of the ßex container
Flex item stacks from the center of the cross axis of the
ßex container
align-self: baseline
Flex item fills the whole height (or width) from cross
start to cross end of the ßex container
È stretch (default)
È flex-start
È flex-end
È center
È space-between
È space-around
È space-evenly
È start
È end
align-content: stretch
place-content
place-content: center;
align-content: 11 12 28 9 9.2 29 4.4
stretch Ð 79 52 9 9 57 &
space-evenly Ð 79 52 11 11 60 Y
place-content Ð 79 45 9 9.2 59 59
Scooby-Doo
& The Dangers
of Data Loss
Every property previously covered in this section Ñ
justify-content, align-content, place-content,
align-items, & align-self Ñ also supports 2 values
that work in conjunction with the other values: safe &
unsafe, e.g.:
div {
display: flex;
align-items: safe center;
justify-content: safe center;
}
The viewport is
easily big enough for
our centered image
Now make the viewport smaller than the centered
image
You can still scroll right & down, however, thanks to the
left-to-right direction & top-to-bottom writing mode of
English
LetÕs enable safe center for align-items & justify-
content
align-items: start;
justify-content: start;
Now we can see the top & left of the image (before, it was out
of reach)É
& the bottom & right, like we always could
align-items:
Ð 115 63 Ð Ð 115 115
safe & unsafe
justify-content:
Ð 115 63 Ð Ð 115 115
safe & unsafe
Since Safari doesnÕt support safe & unsafe for now, make
sure your design handles the problem via other responsive
behaviors such as image resizing & word wrapping
Marathon Man (1976)
Aligning
via Writing Mode
Every property previously covered in this section Ñ
justify-content, align-content, place-content,
align-items, & align-self Ñ also supports 2 values:
start & end, e.g.:
div {
display: flex;
justify-content: start;
}
start
end
Flex items are aligned at the logical end edge of the ßex
container relative reading direction or writing-mode
Latin- & Han-based Mongolian-based
Block start Inline start
Inline start
Block start
Inline end
Block end
Block end Inline end
Arabic-based Han-based
Block start Inline start
Inline start
Block start
Inline end
Block end
Block end Inline end
justify-content:
Ð Ð 45 9 9 Ð Ð
start & end
align-items:
Ð Ð 45 Ð Ð Ð Ð
start & end
align-self:
Ð 79 45 Ð Ð 57 Y
start & end
align-content:
Ð Ð 45 Ð Ð Ð Ð
start & end
place-content:
? ? ? ? ? ? ?
start & end
Gutters
column-gap
row-gap
flex-shrink
flex-basis
flex
By default, ßex items are sized to their content,
similarly to table cells
!PRO TIP
Never use width & height with ßexbox layout! Instead of
width & height, size items using these for the cross axis:
È align-items: stretch
È align-content: stretch
È align-self: stretch
È flex-basis
Value: <number>
È 0: do not grow (default)
È Positive <number>: ßex item can grow
Any positive number for flex-grow allows the ßex item
to grow to Þll all available space
All ßex items that have the same number grow the same
amount
Value: <number>
È 1: all ßex items can shrink to Þt (default)
È 0: do not shrink below the original size
È Positive <number>: ßex item can shrink
Any positive number for flex-shrink allows the ßex
item to shrink to Þt within its ßex container
All ßex items that have the same number shrink the
same amount
Values:
È auto (default)
È content
È max-content
È min-content
È <width>
What about width/height? Why use flex-basis
instead?
flex-basis: min-content
Why?
1 is a keyword or <number>
Of these, we recommend 1 or 3
Single value <flex-grow> <flex-shrink> <flex-basis>
initial 0 1 auto
auto 1 1 auto
none 0 0 auto
<number> <number> 1 0
<length> 1 1 <length>
<percentage> 1 1 <percentage>
flex: initial
Equivalent to 0 1 auto
È Do not grow
È Shrink if not enough space in container
È Size based on content
Equivalent to 1 1 auto
Equivalent to 0 0 auto
È Do not grow
È Do not shrink } Inßexible!
È Size based on content
flex: <number>
Equivalent to <number> 1 0
auto 11 12 18 7 7 22 Y
content Ð 12* 61 Ð Ð Ð Ð
max-content Ð Ð 66 Ð Ð Ð Ð
min-content Ð Ð 66 Ð Ð Ð Ð
[email protected]
www.granneman.com
ChainsawOnATireSwing.com
@scottgranneman
[email protected]
websanity.com
CSS Flexbox
Flexible, Robust Line-Based Layout
Attribution. You must give appropriate credit, provide a link to the license, and indicate if changes were made.
You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your
use. Give credit to:
Share Alike. If you remix, transform, or build upon the material, you must distribute your contributions under
the same license as the original.
No additional restrictions. You may not apply legal terms or technological measures that legally restrict others
from doing anything the license permits.