0
0
CSSmarkup~5 mins

Align items in CSS

Choose your learning style9 modes available
Introduction

Align items helps you control how things line up inside a box. It makes your page look neat and organized.

When you want to center buttons or text inside a menu bar.
When you have a list of pictures and want them all lined up nicely.
When you want to move items to the top, bottom, or middle inside a container.
When you want to create a row or column layout with items spaced evenly.
When you want to fix alignment issues on different screen sizes.
Syntax
CSS
.container {
  display: flex;
  align-items: value;
}

The align-items property works only if the container uses display: flex or display: grid.

The value can be flex-start, flex-end, center, baseline, or stretch.

Examples
This centers all items vertically inside the container.
CSS
.container {
  display: flex;
  align-items: center;
}
This aligns all items to the top of the container.
CSS
.container {
  display: flex;
  align-items: flex-start;
}
This aligns all items to the bottom of the container.
CSS
.container {
  display: flex;
  align-items: flex-end;
}
This makes all items stretch to fill the container's height.
CSS
.container {
  display: flex;
  align-items: stretch;
}
Sample Program

This example shows three colored boxes inside a container. The container uses align-items: center to line up the boxes vertically in the middle, even though they have different heights.

CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Align Items Example</title>
  <style>
    .container {
      display: flex;
      height: 10rem;
      border: 2px solid #333;
      align-items: center;
      background-color: #f0f0f0;
      gap: 1rem;
      padding: 1rem;
    }
    .box {
      width: 4rem;
      background-color: #4a90e2;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      border-radius: 0.5rem;
    }
  </style>
</head>
<body>
  <main>
    <section class="container" aria-label="Example of align-items center">
      <div class="box">A</div>
      <div class="box" style="height: 6rem;">B</div>
      <div class="box" style="height: 3rem;">C</div>
    </section>
  </main>
</body>
</html>
OutputSuccess
Important Notes

If you want to align items horizontally, use justify-content instead.

Using align-items: stretch makes items fill the container's height if they don't have a fixed height.

Try changing the align-items value in the example to see how the boxes move.

Summary

Align items controls vertical alignment inside flex or grid containers.

Common values are center, flex-start, flex-end, and stretch.

It helps make layouts look neat and balanced.