📖 Scenario: You are creating a simple webpage section that shows three colored boxes side by side. You want to control how these boxes line up vertically inside their container.
🎯 Goal: Build a horizontal container with three boxes inside. Use Tailwind CSS to align these boxes vertically in the center of the container.
📋 What You'll Learn
Create a container with flex layout in a row direction
Add three boxes inside the container with distinct background colors
Use Tailwind CSS class to align the boxes vertically centered (cross axis)
Ensure the container and boxes have visible sizes to see alignment clearly
💡 Why This Matters
🌍 Real World
Aligning items vertically in a row is common in navigation bars, card layouts, and toolbars on websites.
💼 Career
Understanding flexbox alignment with Tailwind CSS is essential for front-end developers to build responsive and accessible user interfaces.
Progress0 / 4 steps
1
Create the HTML structure with a flex container and three boxes
Write the HTML code to create a <div> container with the Tailwind class flex. Inside it, add three <div> boxes with the classes w-24, h-24, and background colors bg-red-500, bg-green-500, and bg-blue-500 respectively.
Tailwind
Hint
Use flex on the container and create three child divs with width and height 6rem (24 * 0.25rem) and different background colors.
2
Add a taller height to the container to see vertical alignment
Add the Tailwind class h-48 to the container <div> to make it taller so vertical alignment is visible.
Tailwind
Hint
Use h-48 on the container to make it 12rem tall so you can see vertical alignment clearly.
3
Align the boxes vertically centered using Tailwind
Add the Tailwind class items-center to the container <div> to align the child boxes vertically in the center (cross axis).
Tailwind
Hint
Use items-center on the flex container to align children vertically in the center.
4
Add accessible labels and responsive design
Add role="region" and aria-label="Color boxes container" to the container <div> for accessibility. Also add the Tailwind class sm:h-64 to make the container taller on small screens and above.
Tailwind
Hint
Use role="region" and aria-label="Color boxes container" on the container for screen readers. Add sm:h-64 for taller height on small screens and larger.
Practice
(1/5)
1. What does the Tailwind class items-center do in a flex container?
easy
A. Stretches flex items to fill the container width
B. Aligns flex items horizontally at the start
C. Aligns flex items at the bottom of the container
D. Aligns flex items vertically centered on the cross axis
Solution
Step 1: Understand cross axis in flexbox
The cross axis is perpendicular to the main axis. For a row flex container, it is vertical.
Step 2: Identify what items-center does
items-center centers items along the cross axis, so vertically center in a row.
Final Answer:
Aligns flex items vertically centered on the cross axis -> Option D
Quick Check:
items-center = vertical center [OK]
Hint: Cross axis alignment centers vertically with items-center [OK]
Common Mistakes:
Confusing main axis with cross axis
Thinking items-center aligns horizontally
Mixing items-center with justify-center
2. Which Tailwind class correctly aligns flex items to the bottom on the cross axis?
easy
A. items-end
B. items-stretch
C. items-center
D. items-start
Solution
Step 1: Recall cross axis alignment classes
items-start aligns at the top/start, items-end aligns at the bottom/end, items-center centers, items-stretch stretches.
Step 2: Match bottom alignment
Bottom alignment on cross axis is done by items-end.
Final Answer:
items-end -> Option A
Quick Check:
items-end = bottom align [OK]
Hint: items-end aligns items at the bottom on cross axis [OK]