Emmanuel Akinfulubi

HTML and CSSPractical II

Equal height columns with flexbox in CSS

21 Columns of equal height

fair

Put three cards of different content lengths side by side so all three are the same height, and pin each card’s action to the bottom regardless.

Stuck? Three hints, smallest first
  1. 1. Flex items stretch to the tallest by default, so equal height is what you already have unless you break it.
  2. 2. Make each card a column flex container, and give the middle part flex: 1 so it absorbs the slack.
  3. 3. margin-top: auto on the action does the same job from the other end.

index.html

<div class="cards">
  <article class="card">
    <h3>Abuja</h3>
    <p>Twelve hours, one stop.</p>
    <a class="cta" href="#a">Book</a>
  </article>
  <article class="card">
    <h3>Kano</h3>
    <p>Eighteen hours with two rest stops, arriving early the next morning at the
      central terminal.</p>
    <a class="cta" href="#k">Book</a>
  </article>
  <article class="card">
    <h3>Enugu</h3>
    <p>Nine hours, direct.</p>
    <a class="cta" href="#e">Book</a>
  </article>
</div>

style.css

.cards {
  display: flex;
  align-items: stretch;    /* the default, stated so it is deliberate */
  gap: 10px;
}

.card {
  display: flex;
  flex-direction: column;  /* so the parts inside can be pushed apart */
  flex: 1 1 0%;
  box-sizing: border-box;
  padding: 12px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
  background: #f7f7f8;
  font-size: 0.82rem;
}

.card h3 { margin: 0 0 6px; font-size: 0.95rem; }

/* Absorbs whatever height is spare, which pushes the action down. */
.card p { margin: 0 0 10px; flex: 1; }

.cta {
  margin-top: auto;        /* belt and braces: sticks to the bottom edge */
  align-self: flex-start;
  padding: 6px 12px;
  border-radius: 6px;
  background: #3055d8;
  color: #ffffff;
  text-decoration: none;
}

.cta:focus-visible {
  outline: 2px solid #17171a;
  outline-offset: 2px;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 21

test('the row stretches its children', /align-items:\s*stretch/.test(css), true);
test('each card is itself a column', /\.card\s*\{[^}]*flex-direction:\s*column/.test(css), true);
test('the cards share the width equally, from a zero basis', /flex:\s*1 1 0%/.test(css), true);
test('the body text absorbs the spare height', /\.card p\s*\{[^}]*flex:\s*1/.test(css), true);
test('and the action is pinned to the bottom', /margin-top:\s*auto/.test(css), true);
test('no fixed height was needed to make them match', /[^-]height:\s*\d/.test(css), false);
test('three cards, three actions', countTag(html, 'article') === 3 && countTag(html, 'a') === 3, true);
test('the action keeps a visible focus ring', /\.cta:focus-visible/.test(css), true);

Output


      

Equal-height columns used to be a genuine puzzle and are now the default: align-items: stretch is what a flex container already does, so the work is not making the cards match but making their insides line up. That needs each card to be a column flex container, at which point flex: 1 on the text or margin-top: auto on the action pushes the button to the bottom of every card. Note the sixth check: no fixed height anywhere, so a longer description just makes all three taller.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.