CSS:flexbox
lastmod 2025-09-24created 2019-01-1657 edits
flexbox is a one-dimensional layout method for laying out groups of items in rows or columns. Items shrink to fit into smaller spaces or flex to fill extra space.
Container / Item (Box)
Flex properties include properties applied to the container that wraps the boxes, and properties applied to individual boxes.
Properties applied to the Container
- #display
- #flex-direction
- #flex-wrap
- #flex-flow
- #justify-content - "main axis (direction of flow)"
- #align-items - "perpendicular axis (cross axis)"
- #align-content - "multiple lines, cross axis"
Properties applied to the Box
flex
The flex CSS property sets how a flex item will grow or shrink to fit the space its container takes up.
It is a shorthand property for #flex-grow, #flex-shrink, and #flex-basis.
flex-direction
Depending on the column or row value, #align-content, #justify-content, #align-items, and #align-self all behave differently.
In the Chrome debugging tools, after selecting the Elements tab and the Styles section, a button for checking the detailed properties below is added where display: flex; is set.
row
| |
column
| |
justify-content
justify-content example
Controls the alignment and spacing of flex items along the "main axis (direction of flow)".
-
flex-start(default) -
Justify-content_flex-start.png - Flex items are packed toward the start of the line.
-
flex-end -
Justify-content_flex-end.png - Flex items are packed toward the end of the line.
-
center -
Justify-content_center.png - Flex items are packed toward the center of the line.
-
space-between -
Justify-content_space-between.png - Flex items are evenly spaced, with the first item aligned to one edge of the container and the last item aligned to the opposite edge.
- The edges used for the first and last items depend on #flex-direction and CSS:direction (
direction: ltrordirection: rtl).
-
space-around -
Justify-content_space-around.png - Same as
space-between, except for half-size spaces at both ends.
-
space-evenly - Still being implemented?
Auto Margin
Using auto margins (margin: auto), you can center flex items, space them apart, or group them into subgroups.
Unlike #justify-content, which is applied to the flex container, auto margins are applied to flex items.
They work by consuming all free space in the specified direction.
Another way is to set #flex-grow to 1 only on the flex-item where the space is needed, to claim the space.
align-items
align-items example
Controls the alignment of flex items along the "perpendicular axis (cross axis)".
- flex-start
- flex-end
- center
- baseline
- stretch (default)
align-content
align-content example
Controls the "multi-line cross axis" alignment of flex items and the spacing between lines.
It applies only when there are multiple lines, that is, only with flex-wrap: wrap,
and when there are multiple lines, it takes precedence over the cross-axis alignment of #align-items.
This property determines the vertical alignment when the items span two or more rows.
- flex-start
- flex-end
- center
- space-between
- space-around
- stretch
align-self
flex-grow
The flex-grow CSS property declares how much of the available space inside the flex-container element a flex-item element can take.
flex-grow is usually used together with the #flex-shrink and #flex-basis properties. And generally, the shorthand #flex property is used to ensure all values are set.
The following is the class applied to vuetify's v-spacer.
flex-shrink
A property that sets the flex-shrink value of a flex-item element. The flex-shrink property is used when the size of the flex-item elements is larger than the size of the flex-container element, and the flex-item elements shrink inside the flex-container element according to the number set.
flex-grow vs flex-shrink
flex-basis
The flex-basis CSS property sets the initial size of a flex item. Unless box-sizing is set otherwise, it changes the size of the content box.
inline-flex
- (HTML, CSS) Creating scrollbars with overflow (auto, scroll, hidden)
- CSS display: flex and inline-flex
In responsive layouts, it also affects the container's child elements
Inline-flex_example.gif
gap
Adjusts the spacing between items.
Why there are no justify-items and justify-self properties
The author says:
Because they are not needed.
Examples
See also
Favorite site
- MDN - Flexbox
- MDN - Basic concepts of flexbox
- [Recommended] 10 layouts you can build with flexbox 1
- [Recommended] NAVER D2 - 10 layouts you can build with flexbox
- The start of coding, TCP School - Flexible box layout
- (CSS3) flex Box - justify-content, align-items
- Flexbox : Difference between align-items and align-content
- (HTML/CSS) Making a grid with display (inline-block, flex)
- [Recommended] The complete guide to CSS Flex (Flexible Box)
- [Recommended] Characteristics and differences of align-items / align-content / align-self
- [Recommended] An Interactive Guide to Flexbox in CSS - Interactive guide to Flexbox
- Learn Flexbox, a powerful layout mode, the easy way
- Build layouts that flow smoothly on fluid principles, without media/container queries
Online game
- Flexbox Froggy - a game where you move frogs using flex-box