🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Align-items

AI & DATA SCIENCE // align-items

The align-items property aligns flex items along a flex container's cross axis, controlling vertical alignment in a row-direction container, applying to every item at once.

Syntax

align-items: stretch | flex-start | flex-end | center | baseline;

Deep Dive Course

stretch, the default, stretches items to fill the container's full cross-axis size, unless an item has an explicit size set on that axis, flex-start and flex-end align items toward the corresponding edge of the cross axis, center centers them, and baseline aligns items so their text baselines line up with each other, useful when items have differently-sized content or font sizes but should still visually align on the same text line. align-items sets the alignment for every flex item in the container at once; the related align-self property overrides this default for one specific individual item.

1Understanding Align-items

stretch, the default, stretches items to fill the container's full cross-axis size, unless an item has an explicit size set on that axis, flex-start and flex-end align items toward the corresponding edge of the cross axis, center centers them, and baseline aligns items so their text baselines line up with each other, useful when items have differently-sized content or font sizes but should still visually align on the same text line. align-items sets the alignment for every flex item in the container at once; the related align-self property overrides this default for one specific individual item.

💡

align-items: stretch is the default cross-axis behavior — if flex items with no explicit height look like they're unexpectedly filling the container's full cross-axis size, that's exactly this default stretching behavior at work, not a mistake.

editor.html
.navbar {
  display: flex;
  align-items: center;
}
localhost:3000

2Practical Example

Here is a real-world application of Align-items showing how it is used in production CSS code.

editor.html
.price-row {
  display: flex;
  align-items: baseline;
}

<div class="price-row">
  <span style="font-size: 32px;">$99</span>
  <span style="font-size: 14px;">/month</span>
</div>
localhost:3000

3Best Practices

Follow these guidelines when working with Align-items:

1. Use align-items: center as one of the most common patterns for vertically centering flex items relative to each other in a row-direction container

2. Use align-items: baseline specifically when items contain differently-sized text that should still visually align along a shared text baseline

3. Use align-self on an individual item when just that one item needs different cross-axis alignment than the rest, rather than restructuring the whole container's align-items

⚠️

Tip: align-items: stretch is the default cross-axis behavior — if flex items with no explicit height look like they're unexpectedly filling the container's full cross-axis size, that's exactly this default stretching behavior at work, not a mistake.

editor.html
.navbar {
  display: flex;
  align-items: center;
}
localhost:3000

Examples

Example 01Basic Usage
.navbar {
  display: flex;
  align-items: center;
}
Example 02Advanced Example
.price-row {
  display: flex;
  align-items: baseline;
}

<div class="price-row">
  <span style="font-size: 32px;">$99</span>
  <span style="font-size: 14px;">/month</span>
</div>

Best Practices

  • Use align-items: center as one of the most common patterns for vertically centering flex items relative to each other in a row-direction container
  • Use align-items: baseline specifically when items contain differently-sized text that should still visually align along a shared text baseline
  • Use align-self on an individual item when just that one item needs different cross-axis alignment than the rest, rather than restructuring the whole container's align-items

Interview Question

Why do flex items with no explicit height set often appear to stretch and fill the container's full cross-axis size by default, even though nothing was explicitly told to make them that size?

Hint: Think about align-items' own default value, and what that default value specifically does to an item that hasn't been given its own explicit cross-axis size.

align-items' default value is specifically stretch, not something more neutral like flex-start, which means unless a flex item has an explicit height, in a row-direction container, or width, in a column-direction container, set on it, or unless the container's own align-items has been explicitly overridden to something else, flexbox automatically stretches that item to fill the container's entire available size along the cross axis. This default stretching behavior is a deliberate flexbox design choice, intended to produce visually consistent, equal-height, or equal-width, items by default in many common layouts, like a row of cards that should all match the tallest one's height automatically, without needing any extra CSS to explicitly force that matching. It's precisely this default, rather than any accidental behavior, that's responsible for flex items appearing to fill the full cross-axis size when no explicit size was given, and switching align-items to a value like flex-start instead would let each item size itself based purely on its own content instead.

Exercises

MediumPractice using Align-items in a real scenario.
View Solution
.navbar {
  display: flex;
  align-items: center;
}

Frequently Asked Questions

Why do flex items with no explicit height set often appear to stretch and fill the container's full cross-axis size by default, even though nothing was explicitly told to make them that size?

align-items' default value is specifically stretch, not something more neutral like flex-start, which means unless a flex item has an explicit height, in a row-direction container, or width, in a column-direction container, set on it, or unless the container's own align-items has been explicitly overridden to something else, flexbox automatically stretches that item to fill the container's entire available size along the cross axis. This default stretching behavior is a deliberate flexbox design choice, intended to produce visually consistent, equal-height, or equal-width, items by default in many common layouts, like a row of cards that should all match the tallest one's height automatically, without needing any extra CSS to explicitly force that matching. It's precisely this default, rather than any accidental behavior, that's responsible for flex items appearing to fill the full cross-axis size when no explicit size was given, and switching align-items to a value like flex-start instead would let each item size itself based purely on its own content instead.

Related Functions

Justify-contentAlign-selfFlex-direction