EthicsComputerCourses
Join training +91 90532 10052

Align Items & Align Content

CSS · Lesson 61 of 240 Beginner 10 min read Updated

By the end of this lesson you will be able to line up flex items on the cross axis exactly as a design asks: equal-height cards, prices that sit on the same text line, one item that breaks the pattern, and wrapped rows that stay packed together instead of drifting apart. You will also know the one question that tells you whether you need align-items or align-content.

You saw the basic values in CSS Flexbox Introduction and the axes in Flex Direction. Justify Content handled the main axis. This lesson handles the cross axis, using the product row of TazaTokri, our imaginary grocery app.

Items, lines and the container

A flex container arranges its items into one or more flex lines. A line is one row of items (or one column, in a column layout). Without wrapping there is always exactly one line. With flex-wrap: wrap there can be several.

Think of a steel cupboard with shelves. Each shelf is a flex line and the jars are the items.

  • align-items decides where each jar sits on its shelf: standing at the bottom, hanging from the top, or stretched to touch both.
  • align-content decides how the shelves themselves are spread out inside the cupboard.

So the question to ask is: am I positioning items inside a line, or lines inside the container? Items inside a line means align-items. Lines inside the container means align-content.

align-items: each item in its line

align-items takes these main values:

ValueWhat the item does on the cross axis
stretchGrows to fill the line (the default behaviour)
flex-start / startSits at the cross-start edge (the top of a row)
flex-end / endSits at the cross-end edge (the bottom of a row)
centerSits in the middle of the line
baselineLines up by the first line of text

stretch has a condition: it only works if the item's cross size is auto. If a card in a row has height: 12rem, it keeps that height and does not stretch. A max-height is respected too: the item stretches up to that limit and stops.

Step 1: compare stretch, flex-start and center

Three product rows, each with a fixed height so you can see the line. The dashed outline is the container; the cards have different amounts of text.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>align-items</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    p { margin: 0.8rem 0 0.3rem; font-size: 0.9rem; }
    .row {
      display: flex;
      gap: 0.5rem;
      height: 8rem;
      padding: 0.4rem;
      outline: 2px dashed #98a2b3;
      background: #f9fafb;
    }
    .card {
      width: 8rem;
      padding: 0.5rem;
      background: #ecfdf3;
      border: 1px solid #12b76a;
      border-radius: 8px;
    }
    .card h3 { margin: 0; font-size: 0.95rem; }
    .card small { color: #475467; }
  </style>
</head>
<body>
  <p><code>align-items: stretch</code> (default)</p>
  <div class="row" style="align-items: stretch">
    <div class="card"><h3>Tomatoes</h3></div>
    <div class="card"><h3>Alphonso mangoes</h3><small>Ratnagiri, box of 12</small></div>
    <div class="card"><h3>Paneer</h3><small>200 g</small></div>
  </div>
  <p><code>align-items: flex-start</code></p>
  <div class="row" style="align-items: flex-start">
    <div class="card"><h3>Tomatoes</h3></div>
    <div class="card"><h3>Alphonso mangoes</h3><small>Ratnagiri, box of 12</small></div>
    <div class="card"><h3>Paneer</h3><small>200 g</small></div>
  </div>
  <p><code>align-items: center</code></p>
  <div class="row" style="align-items: center">
    <div class="card"><h3>Tomatoes</h3></div>
    <div class="card"><h3>Alphonso mangoes</h3><small>Ratnagiri, box of 12</small></div>
    <div class="card"><h3>Paneer</h3><small>200 g</small></div>
  </div>
</body>
</html>

In the first row every card is as tall as the line, whatever its content. This is why cards in a flex row are equal height without any extra work. In the second row the cards keep their natural height and hang from the top. In the third they float in the middle. The inline style attributes are only there so you can compare the rows; in a real project, use classes.

Baseline: lining up text, not boxes

center lines up the middles of the boxes. That looks wrong when the items contain text of different sizes, because the eye follows the baseline, the invisible line that letters sit on.

The TazaTokri price line shows a big price, a small unit and a struck-out old price. Watch the difference.

Step 2: center versus baseline

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Baseline alignment</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    p { margin: 0.8rem 0 0.3rem; font-size: 0.9rem; }
    .price {
      display: flex;
      gap: 0.4rem;
      padding: 0.3rem 0.5rem;
      outline: 2px dashed #98a2b3;
      inline-size: max-content;
    }
    .mid { background: linear-gradient(transparent calc(50% - 1px), #f04438 0 calc(50% + 1px), transparent 0); }
    .now { font-size: 3rem; line-height: 1.4; font-weight: 800; color: #027a48; }
    .unit { font-size: 0.85rem; }
    .old { font-size: 1rem; color: #667085; text-decoration: line-through; }
    .price span { outline: 1px solid #b2ddff; }
  </style>
</head>
<body>
  <p><code>align-items: center</code> (red line = middle of the container)</p>
  <div class="price mid" style="align-items: center">
    <span class="now">₹62</span><span class="unit">per kg</span><span class="old">₹80</span>
  </div>
  <p><code>align-items: baseline</code></p>
  <div class="price" style="align-items: baseline">
    <span class="now">₹62</span><span class="unit">per kg</span><span class="old">₹80</span>
  </div>
</body>
</html>

With center the small words float halfway up the big price, which looks careless. With baseline the bottoms of the letters share one line, the way printed text does. Use baseline whenever items are mostly text in different sizes: price lines, a heading with a "See all" link beside it, or a label next to a large number on a dashboard.

By default baseline uses each item's first line of text. last baseline lines items up by their last line instead, which helps when one item wraps onto two lines. last baseline has worked in all major browsers since 2022.

align-self: one item breaks the pattern

align-self goes on a single item and overrides the container's align-items for that item only. It takes the same values, plus auto, which means "use the container's value". Flex Grow, Shrink & Basis showed it briefly; here is a typical use.

In a TazaTokri cart row, the image, the name and the quantity buttons are centred, but a small "Remove" link should sit at the top corner:

CSS
.cart-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.cart-row .remove {
  align-self: flex-start; /* only this item moves to the top */
  margin-inline-start: auto;
}

The auto margin pushes the link to the far end of the row, as you learned in Justify Content, and align-self lifts it to the top of the line.

align-content: spacing the lines

align-content only works on a multi-line flex container, one with flex-wrap: wrap or wrap-reverse. That is true even if the items happen to fit on one line: once wrapping is on, the container counts as multi-line. With flex-wrap: nowrap, the single line always fills the container and align-content does nothing.

It takes the same distribution values as justify-content (flex-start, center, space-between, space-around, space-evenly and so on), plus stretch. The default, normal, behaves like stretch: the container's extra cross space is shared out between the lines, so each line grows taller.

That default causes a classic surprise. The TazaTokri delivery slot picker is a panel 14rem tall. The slot buttons wrap onto three lines, and suddenly there are big gaps between the rows. Adding align-items: flex-start does not close it, because the gap is between the lines, not inside them.

Step 3: fix the drifting rows

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>align-content</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .demo { display: inline-block; vertical-align: top; margin: 0 1rem 1rem 0; }
    .demo p { margin: 0 0 0.3rem; font-size: 0.85rem; }
    .slots {
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
      gap: 0.4rem;
      width: 13rem;
      height: 14rem;
      padding: 0.4rem;
      outline: 2px dashed #98a2b3;
      background: #f9fafb;
    }
    .slots button {
      font: inherit;
      font-size: 0.85rem;
      padding: 0.4rem 0.6rem;
      background: white;
      border: 1px solid #12b76a;
      border-radius: 6px;
    }
  </style>
</head>
<body>
  <div class="demo">
    <p>Default (<code>normal</code>, acts as stretch)</p>
    <div class="slots">
      <button>7-9 am</button><button>9-11 am</button><button>11-1 pm</button>
      <button>4-6 pm</button><button>6-8 pm</button>
    </div>
  </div>
  <div class="demo">
    <p><code>align-content: flex-start</code></p>
    <div class="slots" style="align-content: flex-start">
      <button>7-9 am</button><button>9-11 am</button><button>11-1 pm</button>
      <button>4-6 pm</button><button>6-8 pm</button>
    </div>
  </div>
  <div class="demo">
    <p><code>align-content: center</code></p>
    <div class="slots" style="align-content: center">
      <button>7-9 am</button><button>9-11 am</button><button>11-1 pm</button>
      <button>4-6 pm</button><button>6-8 pm</button>
    </div>
  </div>
</body>
</html>

In the first panel the rows are spread down the whole panel, because each line was stretched to a third of the panel's height. In the second panel the rows are packed at the top with only the gap between them. In the third they sit together in the middle. Try space-between as well: the first row goes to the top and the last row to the bottom.

place-content: both at once

place-content is a shorthand for align-content followed by justify-content. With one value it sets both:

CSS
.slots {
  place-content: center;            /* align-content: center; justify-content: center; */
}

.slots-alt {
  place-content: flex-start space-between; /* align-content, then justify-content */
}

There is a trap here. On a container without wrapping, align-content is ignored, so place-content: center only centres horizontally, and the items still stretch to full height. To centre a single item in both directions in Flexbox, use justify-content: center with align-items: center, or turn wrapping on. place-items has a similar limit in Flexbox: it sets align-items and justify-items, but flex containers ignore justify-items, so only the vertical part works.

Common mistakes

  • Using align-content on a single line. Without flex-wrap: wrap it has no effect. Use align-items.
  • Trying to close the gap between wrapped rows with align-items. The space belongs to the lines. Set align-content: flex-start.
  • Expecting a fixed-height item to stretch. stretch only works when the item's cross size is auto.
  • Centring text of different sizes with center. Use baseline so the letters share one line.
  • Relying on place-content: center to centre in both directions without wrapping. It only centres on the main axis there.

Interview questions

1. What is the difference between align-items and align-content?

align-items positions each item inside its flex line on the cross axis. align-content positions the flex lines inside the container and only works when the container is multi-line, meaning flex-wrap is wrap or wrap-reverse. On a single-line container only align-items has an effect.

2. Why are flex items equal height by default?

The default align-items behaviour is stretch, so each item grows to fill the cross size of its line. This only happens when the item's cross size is auto; an explicit height stops it. That is why cards in a flex row match the tallest card.

3. When would you use align-items: baseline?

When items contain text of different font sizes, such as a large price beside a small unit. Baseline alignment lines up the bottoms of the letters, which looks right to readers, while center lines up box middles. last baseline uses the last line of text instead of the first.

4. Wrapped flex rows have a large gap between them. Why, and how do you fix it?

The container is taller than its content and align-content defaults to stretch, so the extra height is shared between the lines. Setting align-content: flex-start (or start) packs the lines together, leaving only the gap between them.

5. What does place-content: center do on a flex container?

It sets align-content: center and justify-content: center. On a wrapping container this centres the lines both ways. On a non-wrapping container align-content is ignored, so only the main-axis centring happens.

Key points

  • align-items positions items inside their line; align-content positions lines inside the container.
  • Stretch is the default and gives equal-height items, but only when their cross size is auto.
  • baseline lines up text of different sizes by the line the letters sit on.
  • align-self overrides align-items for one item; auto means "use the container's value".
  • align-content needs flex-wrap: wrap, and its stretch default can push wrapped rows apart.
  • place-content sets align-content then justify-content; without wrapping only the second part works.

Exercise

Build a TazaTokri "Deal of the day" strip 10rem tall. It holds three items in a row: a product name in a large heading, the price "₹149" in a big bold font with a small "per pack" beside it, and a "Grab it" button. All three should line up by their text baseline. The button alone should sit at the bottom of the strip. Below it, build a filter panel 12rem tall with eight wrapping filter buttons (Organic, Under ₹100, Fresh today and so on) whose rows stay packed at the top with a 0.5rem gap.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Deal strip</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .deal { display: flex; align-items: baseline; gap: 1rem; height: 10rem; padding: 0.75rem; background: #fef0c7; }
    .deal h2 { margin: 0; font-size: 1.6rem; }
    .deal .price { font-size: 2rem; font-weight: 800; }
    .deal .price small { font-size: 0.8rem; font-weight: 400; }
    .deal button { align-self: flex-end; margin-inline-start: auto; font: inherit; padding: 0.5rem 1rem; }
    .filters { display: flex; flex-wrap: wrap; align-content: flex-start; gap: 0.5rem; height: 12rem; width: 18rem; margin-top: 1rem; outline: 1px dashed #98a2b3; }
  </style>
</head>
<body>
  <div class="deal">
    <h2>Basmati rice</h2>
    <span class="price">₹149 <small>per pack</small></span>
    <button type="button">Grab it</button>
  </div>
  <div class="filters">
    <button>Organic</button><button>Under ₹100</button><button>Fresh today</button><button>Local farms</button>
    <button>Bestsellers</button><button>New</button><button>Combo packs</button><button>Discounts</button>
  </div>
</body>
</html>

align-items: baseline lines up the heading and price text, and align-self: flex-end sends only the button to the bottom. In the filter panel, align-content: flex-start stops the default stretch from pushing the wrapped rows apart.

Quick Quiz

1. A flex container has flex-wrap: nowrap. Which property has no effect on it?

2. A card in a stretched flex row has height: 10rem. What happens?

3. Which value lines up a large price and a small unit label by their letters?

4. What does place-content: flex-start space-between set?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call