Skip to main content

CSS align-items Property

Flex items are positioned vertically, along the cross-axis, by the align-items property.

It accepts these values:

  • normal (default)
  • stretch
  • center
  • flex-start
  • flex-end
  • baseline

Center

Using center places the flex items midway down the container.

Prompt
Create a flex container with a height of 200px and three numbered flex items. Use align-items: center to vertically center the items. Give the container a light background and each item a distinct background color.
Output: complete HTML page

Flex-start

Using flex-start lines the flex items up along the container's top edge.

Prompt
Create a flex container with a height of 200px and three numbered flex items. Use align-items: flex-start to align the items at the top of the container. Give the container a light background and each item a distinct background color.
Output: complete HTML page

Flex-end

Using flex-end lines the flex items up along the container's bottom edge.

Prompt
Create a flex container with a height of 200px and three numbered flex items. Use align-items: flex-end to align the items at the bottom of the container. Give the container a light background and each item a distinct background color.
Output: complete HTML page

Stretch

Using stretch makes the flex items grow until they fill the container. It behaves the same as the default, normal.

Prompt
Create a flex container with a height of 200px and three numbered flex items. Use align-items: stretch so the items fill the full height of the container. Give the container a light background and each item a distinct background color. Do not set a height on the items.
Output: complete HTML page

CSS align-content Property

Where align-items moves items, align-content moves the flex lines themselves, and you only see its effect once flex items wrap onto several lines.

It accepts these values:

  • stretch (default)
  • center
  • flex-start
  • flex-end
  • space-between
  • space-around
  • space-evenly

Center

Here center gathers the flex lines toward the middle of the container.

Prompt
Create a flex container with a height of 400px, flex-wrap: wrap, and align-content: center. Add nine numbered flex items with a width of about 33% so they wrap into multiple lines. Give the container a light background and each item a distinct background color.
Output: complete HTML page

Space-between

Here space-between leaves equal space from one flex line to the next.

Prompt
Create a flex container with a height of 400px, flex-wrap: wrap, and align-content: space-between. Add nine numbered flex items with a width of about 33% so they wrap into multiple lines. Give the container a light background and each item a distinct background color.
Output: complete HTML page

True Centering With Flexbox

Centering an item in both directions takes two declarations on the flex container: justify-content: center and align-items: center. Horizontally and vertically, the flex item then sits perfectly in the middle.

Example

Prompt
Create a flex container with a height of 400px that uses both justify-content: center and align-items: center to perfectly center a single flex item. The item should display the text "True Centering". Give the container a light background and the item a distinct background color.
Output: complete HTML page