Align Items and Align Content – Cross Axis Alignment
Quick Answer: Use align-items on a flex container to position its items on the cross axis, inside each flex line. The common values are stretch (the default behaviour), flex-start, flex-end, center and baseline. Use align-content to share the free cross-axis space between the lines of a wrapping container. In a row, the cross axis is vertical. In a column, it is horizontal.
S1. Why Do We Need Cross Axis Alignment?
Imagine you are building a navigation bar that is 80px tall. It has a logo, a menu and a login button. You want all three to sit in the vertical middle of the bar.
In the previous article, we learned how justify-content shares the free space along the main axis. But a container also has free space on the other axis, and the items need a rule for that too.
Would you add a top margin to every item to push it down? That works for one bar height, but it breaks as soon as the height or an item changes.
CSS Flexbox solves this with one property on the parent. Let's first see what happens without it.
S2. The Problem: Free Space on the Cross Axis
Cross axis = the axis that is perpendicular to the main axis. In a row, it runs from top to bottom.
Imagine a 360px wide and 80px tall bar. It holds three items, each 120px wide and 40px tall. They use the full width, but only half of the height, so 40px of cross-axis space is left over. The developer makes the bar a flex container, but gives it no cross-axis rule.
Example 1: A bar with the default cross-axis behaviour
Assumption: In the output diagrams, one character is about 20px wide and one row is about 20px tall. A, B and C stand for Logo, Menu and Login.
<!-- HTML -->
<nav class="bar">
<div class="bar-item">Logo</div>
<div class="bar-item">Menu</div>
<div class="bar-item">Login</div>
</nav>
/* CSS */
.bar {
display: flex;
width: 360px;
height: 80px;
}
.bar-item {
width: 120px;
height: 40px;
background: #E2E8F0;
}
Expected output
|AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | | | | All three items sit against the top edge. The 40px of cross-axis space is left empty at the bottom.
The page renders, but it is not the bar we wanted. Items with a fixed height are placed at the start of the cross axis unless we say otherwise.
What goes wrong?
- Top-heavy layout: The items hug the top edge, and the bottom stays empty.
- Manual margins: Developers add a top margin to every item to push it down.
- Fragile numbers: A different bar height or a taller item makes every margin wrong.
- Layout hacks: Line-height tricks and absolute positioning are added just to center one item.
- Poor clarity: The CSS does not say where the items should sit on the cross axis.
Now think about many navigation bars, card rows and galleries on one website. Repeating hand-made offsets becomes harder to maintain.
Remember: The container is not broken. It is using its default cross-axis behaviour. The real problem is that nobody told it where the items should sit.
Animation 1: How unused cross-axis space creates workarounds
Three items, no cross-axis rule
The red highlights show items stuck at the top while the cross-axis space sits unused.
The animation highlights the plain container and the items packed against its top edge.
S3. The Solution: Position Items with align-items and align-content
Flexbox gives us a simpler design.
align-items = a property on the flex container that positions every item on the cross axis, inside its flex line.
align-content = a property on the flex container that shares the free cross-axis space between the lines of a wrapping container.
Instead of calculating margins, add one rule to the parent.
align-items: centerputs each item in the middle of its line.align-items: flex-startandflex-endput items at the start or end of the cross axis.align-items: stretchmakes items with an automatic cross size fill the line.align-items: baselinelines up the text baselines of the items.align-contentpositions the lines themselves when the container wraps.
Now the same three items sit in the vertical middle of the bar.
Example 2: A bar with align-items: center
Assumption: This example uses standard CSS supported by all modern browsers, and assumes a row direction in a left-to-right language.
.bar {
display: flex;
align-items: center;
width: 360px;
height: 80px;
}
.bar-item {
width: 120px;
height: 40px;
background: #E2E8F0;
}
Expected output
| | |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | | Each item has 20px above and 20px below it. The 40px of free cross-axis space is split evenly.
Notice the difference. The HTML stays the same, and one rule on the parent centers every item.
Animation 2: The align-items design
One rule, shared cross-axis space
The container measures the cross-axis space and positions the items inside it.
The animation highlights the cross-axis space and the container rule that positions the items.
Compare both approaches
| Without alignment rules | With align-items and align-content |
|---|---|
| Fixed-height items stay at the top. | You choose start, end, center, stretch or baseline. |
| Centering needs margins or positioning hacks. | Centering is one declaration. |
| A new height breaks the numbers. | The layout adapts to the container height. |
| Wrapped lines are placed by default rules only. | Lines can be packed, centered or spaced. |
| Layout intent is hidden. | Layout intent is clear. |
The idea to remember
- align-items = positions items on the cross axis, inside each line.
- align-content = positions the lines themselves, and only works on wrapping containers.
- Cross axis = perpendicular to the main axis.
flex-directiondecides which one that is. - Default =
normalfor both, which behaves likestretchin a flex container. - Free cross-axis space = needed before either property shows a visible effect.
S4. Step-by-Step Code Breakdown
1. Start with a flex container
Both properties only work on a flex container. Add display: flex or display: inline-flex to the parent first. Without it, they have no effect.
2. Find the cross axis
The cross axis always runs at a right angle to the main axis. When you change flex-direction, the cross axis changes with it.
| flex-direction | Main axis | Cross axis | align-items moves items |
|---|---|---|---|
row |
Horizontal | Vertical | Up and down |
row-reverse |
Horizontal | Vertical | Up and down |
column |
Vertical | Horizontal | Left and right |
column-reverse |
Vertical | Horizontal | Left and right |
3. Know the five align-items values
Each value places the items differently inside their line. The table assumes a row direction.
| Value | Where the items go | Item size | Typical use |
|---|---|---|---|
stretch |
Fill the whole line | Grows if the cross size is auto | Equal-height cards |
flex-start |
At the top of the line | Keeps its own size | Top-aligned items |
flex-end |
At the bottom of the line | Keeps its own size | Bottom-aligned items |
center |
In the middle of the line | Keeps its own size | Vertical centering |
baseline |
Text baselines line up | Keeps its own size | Mixed font sizes |
4. Compare the main values
The same three items behave differently under each value. Each container below has the same children. Only align-items changes. In the first container, the items have no fixed height, so they can stretch.
Example 3: stretch, flex-start, flex-end and center
<!-- HTML (four containers with the same children) -->
<div class="demo stretch"><span>A</span><span>B</span><span>C</span></div>
<div class="demo start"><span>A</span><span>B</span><span>C</span></div>
<div class="demo end"><span>A</span><span>B</span><span>C</span></div>
<div class="demo center"><span>A</span><span>B</span><span>C</span></div>
/* CSS */
.demo {
display: flex;
width: 360px;
height: 80px;
}
.demo span {
width: 120px;
height: 40px;
}
.stretch { align-items: stretch; }
.stretch span { height: auto; }
.start { align-items: flex-start; }
.end { align-items: flex-end; }
.center { align-items: center; }
Expected output
stretch (items have height: auto): |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| flex-start: |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | | | | flex-end: | | | | |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| center: | | |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | |
Notice that stretch only changed the items because their height was auto. An item with a fixed height keeps that height, and sits at the start of the line.
5. Use baseline for mixed text sizes
With baseline, the items are shifted so that the baselines of their first lines of text sit on one shared line. This helps when items hold text of different sizes.
Example 4: Aligning text of different sizes
<!-- HTML -->
<div class="row">
<span class="big">Course</span>
<span class="small">Beginner</span>
</div>
/* CSS */
.row {
display: flex;
align-items: baseline;
gap: 8px;
}
.big { font-size: 32px; }
.small { font-size: 14px; }
Expected output
flex-start: "Beginner" is placed at the top of the line, so it
sits higher than the bottoms of the letters in "Course".
baseline: The bottoms of the letters in "Course" and "Beginner"
sit on one shared line, so the small text drops down.
6. Share space between lines with align-content
align-content positions the lines of a wrapping container. It only affects a container with flex-wrap: wrap or wrap-reverse, and it needs free space on the cross axis, so the container must be taller than its lines. The default is normal, which behaves like stretch: the lines grow to fill the extra space.
Example 5: A wrapped gallery with align-content
<!-- HTML -->
<div class="gallery">
<span>A</span><span>B</span><span>C</span><span>D</span>
</div>
/* CSS */
.gallery {
display: flex;
flex-wrap: wrap;
align-content: space-between;
width: 360px;
height: 200px;
}
.gallery span {
width: 180px;
height: 40px;
}
Expected output
space-between (200px tall, two lines of 40px): |AAAAAAAAABBBBBBBBB| |AAAAAAAAABBBBBBBBB| | | | | | | | | | | | | |CCCCCCCCCDDDDDDDDD| |CCCCCCCCCDDDDDDDDD| Free cross-axis space: 200px - 2 x 40px = 120px flex-start: 120px after the last line flex-end: 120px before the first line center: 60px before and 60px after space-between: 120px between the two lines space-around: 30px at the top, 60px between, 30px at the bottom space-evenly: 40px at the top, between and at the bottom stretch: each line grows by 60px, items keep their 40px height
The values work like the ones in justify-content, but they share the space between lines instead of between items. Notice the stretch case. The lines grow, but items with a fixed height stay the same size, so extra space appears below each item.
7. Remember that it follows the direction
In a column, the cross axis is horizontal, so align-items moves items left and right. Items that are not stretched take only the width of their content.
Example 6: align-items in a column
<!-- HTML -->
<div class="panel">
<span>Title</span>
<span>Enroll Now</span>
</div>
/* CSS */
.panel {
display: flex;
flex-direction: column;
align-items: center;
width: 360px;
}
Expected output
| [Title] | | [Enroll Now] | The cross axis is horizontal, so each item is centered across the panel and keeps only the width of its content.
S5. Architecture and Real-World Trade-Offs
The alignment rules are set per container, so each part of a page can position its items in its own way. This is useful in navigation bars, card rows, media objects and galleries.
How the design fits a website
Imagine a website with a navigation bar, a row of cards and a tall wrapped gallery. Each one has its own parent with its own cross-axis rule.
A focused cross-axis flow
Each website area positions its items in the way that suits its content.
The animation traces each website area to its own cross-axis rule.
Benefits for larger websites
- Maintainability: The alignment rule lives on the parent, so changes happen in one place.
- Easy centering: Vertical centering needs no margins, line-height tricks or absolute positioning.
- Equal heights: The default
stretchmakes cards in a row the same height. - Text alignment:
baselinelines up text of different sizes. - Line control:
align-contentcontrols how wrapped lines use extra height.
Important trade-offs
- Without extra cross-axis space,
flex-start,centerandflex-endlook the same, because the line is only as tall as its tallest item. stretchonly changes items whose cross size isauto. A fixed height or width stops it.- The default
stretchcan stretch images or buttons that you wanted to keep at their natural size. align-contentdoes nothing on a container that does not wrap.- With the default
align-content, extra height is shared among the lines, which can open up unexpected space.
Important: align-items positions items inside their line, and align-content positions the lines inside the container. Both apply to the whole container. Positioning one single item uses a different, item-level property that is covered later in the series.
S6. Common Mistakes and Best Practices
Mistake 1: Using align-content on a container that does not wrap
Wrong: Add align-content: center to a flex container with the default nowrap.
Why it fails: The property only affects wrapping containers. A single-line container ignores it.
Better: Use align-items: center for a single line, or add flex-wrap: wrap when you need several lines.
Rule: align-items is for items. align-content is for lines.
Mistake 2: Expecting alignment without extra cross-axis space
Wrong: Add align-items: center to a container with automatic height and expect the items to move.
Why it fails: The container is only as tall as its tallest item, so there is no free space to center within.
Better: Give the container a height or min-height.
Rule: Free cross-axis space must exist before it can be shared.
Mistake 3: Expecting stretch to work on fixed-height items
Wrong: Give every card a fixed height and expect align-items: stretch to make them fill the row.
Why it fails: Stretching only applies when the cross size is auto.
Better: Remove the fixed height, or use min-height so the item can still grow.
Rule: A fixed size beats stretch.
Mistake 4: Using align-items to position wrapped lines
Wrong: Use align-items: center to center a whole wrapped gallery in a tall container.
Why it fails: It positions each item inside its own line. With the default align-content, the lines still grow to fill the container.
Better: Use align-content: center for the lines.
Rule: To move the lines, use align-content.
Mistake 5: Using align-items to move only one item
Wrong: Add align-items: flex-end to a single button to push just that button down.
Why it fails: The property belongs on the container. On the button, it only affects the button's own children, and only if the button is a flex container.
Better: Set the rule on the parent for all items. To change one item, use an item-level property, which is covered later in the series.
Rule: Container properties position all items. Item properties position one.
S7. How This Comes Up in Interviews
Question 1: What is the difference between justify-content and align-items?
What the interviewer wants: They want to know whether you understand the two axes.
Strong answer:
"justify-content works along the main axis, and align-items works along the cross axis. Both follow flex-direction, so in a row the first is horizontal and the second is vertical, and in a column they swap."
Question 2: What is the difference between align-items and align-content?
Strong answer:
"align-items positions each item inside its own flex line. align-content positions the lines themselves inside the container, and it only has an effect when the container wraps and has extra cross-axis space."
Question 3: What is the default align-items, and why can it fail to stretch items?
Strong answer:
"The initial value is normal, which behaves like stretch in a flex container. Items with an automatic cross size fill the line. If an item has a fixed height in a row, it keeps that height, so it does not stretch."
Question 4: How do you center an item both vertically and horizontally with Flexbox?
Strong answer:
"I make the parent a flex container, set justify-content: center for the main axis and align-items: center for the cross axis, and give the parent a height so there is free space to center within."
What should different experience levels explain?
- Junior developer: Explain
align-items: centerand show a vertically centered bar. - Senior developer: Explain the cross axis,
stretchand fixed sizes,baseline, and the difference betweenalign-itemsandalign-content. - Technical architect: Explain how alignment defaults and conventions keep components consistent across a design system.
S8. Hands-On Practice
Beginner: Align a bar
Create an 80px tall flex container with three 40px tall items. Try flex-start, center and flex-end one at a time and observe the result.
Expected result: The items sit at the top, in the middle and at the bottom of the bar.
Intermediate: Compare stretch and baseline
Build a row of three cards with different amounts of text. Observe how the default stretch makes them equal in height. Then give one card a fixed height. Finally, build a row with a large heading and a small label, and compare flex-start with baseline.
Hint: Check whether each item has a fixed height.
Advanced: Control wrapped lines
Build a 360px wide, 200px tall wrapping container with four 180px wide and 40px tall items. Try every align-content value, and work out the space between the lines by hand. Then build a column panel with align-items: center.
Expected result: With 120px of free space, space-between leaves 120px between the lines, and space-evenly leaves 40px at the top, between and at the bottom. In the column, the items are centered horizontally.
S9. Next Steps and Community
Previous article: Justify Content – Mastering Main Axis Alignment.
Next article: Group 3: Flex Item Properties and Alignment.
Continue the series with Group 3, where you move from the alignment rules you set on the container to the properties you set on individual flex items.
Try the examples in your own HTML and CSS project. Change the container height, switch between the align-items values, try align-content on a wrapped container, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Know the cross axis. Then align along it.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.