Explain Codes LogoExplain Codes Logo

Center image in table td in CSS

html
responsive-design
css
accessibility
Nikita BarsukovbyNikita Barsukov·Sep 10, 2024
TLDR

To center an image inside a td element, use text-align: center; on the td itself and apply display: block; with margin: auto; on the image.

td { text-align: center; /* Like a mom pulling you to the center of a photo. */ } .centered-img { display: block; margin: auto; /* A bit like a well-behaved teenager. Stays put. */ }
<td><img src="astonishing-masterpiece.jpg" alt="" class="centered-img"></td>

This approach ensures the image stays centered horizontally and vertically.

Centering an image like never before

Enough of the insipid "Hello, World!" examples and theory-based answers; real-life scenarios are where the challenges, surprises, and fun are found. Centering appears basic until you're faced with a range of contexts and exceptions.

Get responsive with Flexbox

For more flexibility in your layout – and to earn instant street cred among developers – use Flexbox to center your image. It grants you control over the horizontal and vertical axes simultaneously:

td { display: flex; /* Flexbox to the rescue, like a Clark Kent turning into Superman */ justify-content: center; /* Aligns the child elements in the center, horizontal axis */ align-items: center; /* Yep! This one tackles vertical alignment */ }

Media queries: Your responsive sidekick

When responsiveness knocks on your door, media queries become your trusted sidekick, maintaining the image's centered alignment across different devices:

@media screen and (max-width: 600px) { td { justify-content: center; /* Because we care how our site looks on your mobile too */ } }

Vertically align to perfection

To perfectly vertically center the image in a td, CSS exposes the vertical-align property:

td { vertical-align: middle; /* Brings the content to the center as quickly as a laser-guided missile */ }

You might need to add display: block; and assign a height to the td if you want the align property to start behaving.

Centering nightmares: Common Pitfalls

No pot of gold without crossing a few pitfalls:

  • Inflated TD size: Hefty margin or padding values can stretch table cells, messing up your layout. Remember, moderation is key!
  • Destroyed aspect ratio: Absolute positioning and size overrides can butcher layout fluidity and image aspect ratio. Cross-check across multiple devices!

Keep Good habits: Clean Code and Accessibility

While crafting HTML and CSS, it pays (by saving you from hair-pulling debugging sessions) to invest in good practices from the start:

Create maintainable styles

Inline styles can make your code harder to manage. Adopt external stylesheets or internal style blocks for a neater, more maintainable code base:

<!-- The 'NoNo' way --> <td><img src="image.jpg" alt="" style="margin:auto;"></td> <!-- The 'Pro' way --> <td><img src="image.jpg" alt="" class="centered-img"></td>

Play nice with alt tags

Alt attributes aren't decoration, they're essential for accessibility. Screen readers rely on accurate alt text to convey the image context to users with visual impairments:

<td><img src="astonishing-masterpiece.jpg" alt="Monet's amazing Water Lilies" class="centered-img"></td>