Course contentsShow
← Web Development
Lesson 304 of 2,9825. FlexboxFree lesson

Icon and Text Alignment

Perfectly align icons with text using flex and align-items.

Icon and Text Alignment

What You'll Learn

You'll learn how to perfectly align icons next to text so they sit on the same visual baseline, using display: flex and align-items.

The Alignment Problem

When you place an icon (like an SVG or icon font) next to text, they often look misaligned—the icon might float too high or sit too low. This happens because inline elements follow text baseline rules, and icons don't always cooperate.

Flexbox solves this elegantly.

The Flex Solution

Wrap your icon and text in a container with display: flex. Then use align-items: center to vertically center both elements within that container:

.icon-text-wrapper {
  display: flex;
  align-items: center;
  gap: 8px; /* spacing between icon and text */
}
<div class="icon-text-wrapper">
  <img src="icon.svg" alt="" class="icon">
  <span>Download File</span>
</div>

The flex container treats the icon and text as flex items. align-items: center aligns them along the cross axis (vertically, when flex-direction is the default row), creating perfect visual balance.

Why This Works

Think of it like organizing items on a shelf. Without flexbox, the icon and text follow different rules about where they "sit." Flexbox creates a shared reference point—the center line of the container—and aligns both items to it.

You can also use align-items: flex-start or align-items: baseline depending on your design needs, but center is most common for icon-text pairs.

Key Takeaway: Use display: flex with align-items: center on a wrapper to perfectly align icons with adjacent text, creating polished, professional-looking UI components.