Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Align Items

The CSS align-items property controls how Flex Items are aligned along the cross axis of a Flex Container.

While justify-content controls the main axis, align-items controls the cross axis.

Main Axis and Cross Axis

When flex-direction: row; is used, the main axis is horizontal and the cross axis is vertical.

Therefore, justify-content controls horizontal positioning, while align-items controls vertical positioning.

When flex-direction: column; is used, these directions are reversed.

Syntax

.container {
    display: flex;
    align-items: value;
}

Align Items Diagram

Item 1
Item 2
Item 3

align-items controls alignment along the cross axis.

Values of align-items

Value Description
stretch Stretches items across the cross axis when possible.
flex-start Aligns items at the beginning of the cross axis.
flex-end Aligns items at the end of the cross axis.
center Centers items along the cross axis.
baseline Aligns items according to their text baselines.

1. align-items: flex-start

flex-start places Flex Items at the beginning of the cross axis.

.container {
    display: flex;
    align-items: flex-start;
}

Output

HTML
CSS
JavaScript

2. align-items: flex-end

flex-end places Flex Items at the end of the cross axis.

.container {
    display: flex;
    align-items: flex-end;
}

Output

HTML
CSS
JavaScript

3. align-items: center

center places the Flex Items in the center of the cross axis.

.container {
    display: flex;
    align-items: center;
}

Output

HTML
CSS
JavaScript

4. align-items: stretch

stretch stretches Flex Items across the cross axis when their cross-axis size is not explicitly defined.

.container {
    display: flex;
    align-items: stretch;
}

Output

HTML
CSS
JavaScript

5. align-items: baseline

baseline aligns Flex Items according to the baseline of their content, especially useful when the items contain text with different sizes.

.container {
    display: flex;
    align-items: baseline;
}

Output

HTML
CSS
JavaScript

Comparing align-items Values

flex-start

Places items at the beginning of the cross axis.

flex-end

Places items at the end of the cross axis.

center

Centers items along the cross axis.

stretch

Stretches items across the cross axis when possible.

baseline

Aligns items according to their content baseline.

justify-content vs align-items

Property Controls Axis
justify-content Distribution of Flex Items Main Axis
align-items Alignment of Flex Items Cross Axis

Example 🌍🤓

Consider a navigation bar containing a logo, menu items, and a button.

If the navigation bar uses display: flex;, the align-items: center; property can be used to vertically align these elements when the main direction is a row.

.navbar {
    display: flex;
    align-items: center;
}
Important: align-items works along the cross axis, while justify-content works along the main axis. The direction of both axes depends on the value of flex-direction.

Summary

The align-items property controls the alignment of Flex Items along the cross axis. Its commonly used values are flex-start, flex-end, center, stretch, and baseline. Together with justify-content, it provides precise control over Flexbox alignment.