Explain Codes LogoExplain Codes Logo

Vertically align text next to an image?

html
responsive-design
css-pseudo-elements
vertical-align
Anton ShumikhinbyAnton Shumikhin·Mar 9, 2025
TLDR

Quickly center text next to an image using flexbox. Add display: flex and align-items: center; to their shared parent container.

Below is the quick-coded example:

.flex-center { display: flex; align-items: center; /* Magic alignment happens here! 🎩✨ */ }
<div class="flex-center"> <img src="your-image.jpg" alt="Just an awesome image."> <span>The text which sits perfectly next to the image.</span> </div>

This will align both items around their median line. Simple, right?

When tackling older browsers, you might use vertical-align: middle; directionally applied on the image in combination with display: table-cell; on the containing element.

Cater to element and their quirks

Different HTML elements can behave peculiarly, and understanding their nuances is crucial to balancing their alignment. Let's visit these alignment scenarios in greater detail.

Inline text with image

When dealing with brief text and an inline image, the vertical-align: middle; style can be applied directly onto the image tag:

<div> <img src="your-image.jpg" alt="" style="vertical-align: middle;"> <span>This text is as centered as a well-balanced seesaw.</span> </div>

Handling variable heights

Unsure of specific image and text heights? Consider using transform: translateY(-50%) paired with specific positioning. This gives granular control irrespective of unknown heights:

.valign-transform { position: relative; top: 50%; transform: translateY(-50%); /* It's like magic, only real! ;) */ }

Multiline text next to image

To correctly align multiline text alongside an image, the containing element's position should be set as relative, and absolute positioning should be applied to the text:

.multiline-container { position: relative; /* Home sweet home */ } .multiline-text { position: absolute; /* Fly, text, fly! */ top: 50%; transform: translateY(-50%); }

Beyond flexbox: alternatives and nuances

Though flexbox is a simple and powerful tool, it may not fit every situation. Let's learn the alternative alignment techniques and their use-cases:

Graceful degradation for older browsers

When catering to dated browsers, display table methods can come to the rescue:

.table-style { display: table; /* Old but gold */ } .cell-style { display: table-cell; vertical-align: middle; /* It's all about balance */ }

This translates into:

<div class="table-style"> <div class="cell-style"> <img src="your-image.jpg" alt=""> </div> <div class="cell-style"> This text could be a mini novel, and still, it'd be vertically centered. </div> </div>

Elevating the game

  • Say no to text distortion: Align single lines of text by setting line-height identical to the container height.
  • Invite responsive design: To maintain vertical centering on all viewport sizes, add margin: auto 0; within flex containers.
  • Hold on to the layout: Counteract layout shifts as your viewport scales by setting a min-width on images within flex containers.
  • Harness CSS pseudo-elements: Increase alignment control without excess HTML through the use of CSS pseudo-elements.

Browser compatibility and testing

Using these alignment techniques means ensuring their cross-browser compatibility. Check caniuse.com to determine cross-browser support for CSS properties. Test early and often to ensure consistent results!

Responsive adjustments

Responsive alignment is all about considering variable screen sizes. Leverage proportional units (percentages or viewport values) and media queries to ensure images and text remain vertically aligned across devices.