LEARNING OBJECTIVES โต
- Master all values of the
align-itemsproperty:stretch(initial default),flex-start,flex-end,center, andbaseline. - Override container-level cross-axis alignment on individual child items using
align-self. - Understand typographic baseline alignment and explain how the browser aligns the baseline of the first text line across different font sizes.
- Implement the classic modern 2-line absolute center pattern and diagnose how
margin: autooverrides cross-axis properties.
๐ The Mental Model & Story (Intuitive Foundation)
Imagine a symphony orchestra arranged on a concert hall stage. The musicians (flex items) sit on the stage (flex container) along a primary row from left to right (main axis).
Stage Ceiling (Cross-Start)
|
+--------------------------------|-----------------------------------+
| [Trombone] (Tall) | |
| +-------+ +-------v-------+ |
| | | | Violin (Solo) | |
| | | | (align-self: | +-----------+ |
| | | | flex-start) | | Flute | |
| | | +---------------+ | | |
| +-------+ +-----------+ |
+--------------------------------------------------------------------+
Stage Floor (Cross-End)
align-itemsis the general stage direction rule: should all musician chairs stretch to match the tallest player's stand (stretch), sit flush against the floor (flex-end), or hang suspended in the vertical middle (center)?align-selfis the soloist's privilege: even if all orchestral players are aligned along the floor, the first violinist can choose to step forward and align with the top spotlight (align-self: flex-start).
Technical Deep Dive & Specifications
Cross-Axis Alignment Values
1. stretch (Default) 2. flex-start 3. center
+---------------------+ +---------------------+ +---------------------+
| [Item 1] [Item 2] | | [Item 1] [Item 2] | | |
| [ ] [ ] | | [ ] | | [Item 1] [Item 2] |
| [ ] [ ] | | | | [ ] |
+---------------------+ +---------------------+ +---------------------+
4. flex-end 5. baseline 6. align-self: flex-start
+---------------------+ +---------------------+ +---------------------+
| | | | | [Item 2] |
| [Item 2] | | [Item 1] Item 2 | | [Item 1] (Overridden|
| [Item 1] [ ] | | Aa Ag | | [ ] solo) |
+---------------------+ +---------------------+ +---------------------+
| Value | Behavior on Cross Axis | Interaction with Explicit Dimensions |
|---|---|---|
stretch (Default) |
Stretches item to fill the entire cross-axis line height. | Ignored if child has explicit height (in row mode) or width (in column mode). |
flex-start / start |
Packs item against the cross-start edge. |
Item shrinks to its intrinsic content height. |
flex-end / end |
Packs item against the cross-end edge. |
Item shrinks to its intrinsic content height. |
center |
Centers item between cross-start and cross-end. |
Item shrinks to its intrinsic content height. |
baseline |
Aligns the typographic baseline of all items in the line. | Critical for pairing icons with text or pairing text of different font sizes. |
The stretch Gotcha: Why Items Don't Stretch
When align-items: stretch is active, the flex item will automatically expand to match the tallest sibling in the row. However, stretching is immediately disabled if the item defines:
- An explicit cross-axis size (e.g.,
height: 100pxinrowmode). - A max-dimension constraint (e.g.,
max-height: 80px).
To restore automatic height matching, remove the hardcoded height or set height: auto.
Typographic Baseline Alignment Deep Dive
When aligning text elements of different sizes (e.g., a huge $49 price next to a small /month label), centering them vertically (align-items: center) causes the visual bottom of the letters to look uneven and jittery.
Setting align-items: baseline instructs the browser to:
- Locate the baseline of the first line of text inside each flex item.
- Align all those baselines along a single, perfectly straight horizontal guide.
- Shift the boxes up or down to satisfy this typographic alignment.
align-items: center (Bad for text): align-items: baseline (Typographic perfection):
+----+ +----+
| $ | +------+ | $ |
| 49 | | /mo | | 49 | +------+
+----+ +------+ +----+ | /mo |
--------------------------------- --------------------------------- (Baseline Line)
The align-self Property
align-self allows an individual flex item to override the container's align-items rule.
.flex-item-solo {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
- Default value is
auto, which computes to the parent container'salign-itemsvalue.
The Modern 2-Line Absolute Center
Before Flexbox, centering an element both horizontally and vertically required complex position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); hacks.
With Flexbox, centering any content perfectly takes exactly two declarations:
.center-box {
display: flex;
justify-content: center; /* Main axis center */
align-items: center; /* Cross axis center */
}
The margin: auto Super-Power
In Flexbox, declaring margin: auto (or margin-top: auto, margin-left: auto) on a child item causes that margin to consume all available space in that direction.
margin: autoon a flex item overrides bothjustify-contentANDalign-items/align-self.- Setting
margin: autoon a single child inside adisplay: flexcontainer centers the item perfectly in both axes without declaringjustify-contentoralign-items.
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Lines 24โ32 (
.pricing-display): Usesdisplay: flex; align-items: baseline;. Even though$,99,/ user / month, andSave 20%have drastically different font sizes (ranging from 0.75rem to 3.5rem) and line heights, their text baselines line up in a clean horizontal plane. - Lines 61โ69 (
.profile-card): Setsalign-items: centeron the container, which centers the avatar circle and bio text vertically. - Lines 84โ92 (
.status-tag): Declaresalign-self: flex-start. It breaks away from the container's center alignment and anchors itself directly at the top-right corner.
Expected Browser Render Output
1. Typographic Baseline Alignment
+--------------------------------------------------------------------------------------+
| $ 99 / user / month [Save 20%] |
| (All font text bottoms sit on the exact same horizontal baseline) |
+--------------------------------------------------------------------------------------+
2. align-self: flex-start Override
+--------------------------------------------------------------------------------------+
| ( SR ) Samantha Reed [ On Call ] |
| Senior Site Reliability Engineer managing high-availability... (Pinned Top) |
| (Centered) |
+--------------------------------------------------------------------------------------+๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Build an Enterprise Pricing Matrix Header
Instructions:
- Configure
.pricing-card-headeras a flex container where cards stretch to equal height by default (align-items: stretch). - Inside
.price-tag, align the currency symbol (.currency), amount (.val), and cadence (.cadence) along their typographicbaseline. - In the second card (the "Enterprise" tier), use
align-self: flex-endon.badge-popularto anchor the badge to the bottom corner.
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Applying
height: 100%on Children whenstretchis Already Active: Settingheight: 100%is unnecessary and often breaks cross-browser layout calculations whenalign-items: stretch(the default) is enabled. - Using
align-items: centerfor Mixed Text Sizes: Centering currency symbols vertically next to big numerals makes the currency symbol float awkwardly in the middle of the number. Always usealign-items: baseline. - Unintended Button Stretching in Column Flexbox: When
flex-direction: columnis active, defaultalign-items: stretchcauses all buttons to stretch to 100% width. Applyalign-self: flex-startto buttons to shrink them to content width.
๐ก Pro Tips
- The 1-Line Flexbox Centering Trick: If a container has
display: flex;, applyingmargin: auto;to a single child element centers it horizontally AND vertically without needingjustify-contentoralign-items. - Baseline with Icons: If you have SVG icons next to text, give the SVG
vertical-align: middleor wrap the pair in aninline-flexcontainer withalign-items: center. - Combine
align-itemswith CSS Custom Properties: In component libraries, define--item-align: center;on the component and assignalign-items: var(--item-align);for dynamic consumer overrides.
๐ Key Takeaways
align-itemsgoverns alignment of flex items along the Cross Axis (vertical inrowmode, horizontal incolumnmode).stretchis the default value, causing items to expand to match the cross-axis size of the tallest sibling unless restricted by explicit dimensions.align-selfallows individual child items to break away from the container'salign-itemsrule.align-items: baselinealigns the typographic baseline of the first line of text across all children, essential for currency tags and label-input pairs.margin: autoon a flex item absorbs all available space along that vector, taking precedence overalign-itemsandalign-self.- --