ICT Button with Arrow Green Leaf Toucan Extended

We help businesses stand out, so they significantly increase their chance of converting more leads

+ 0 %
Increase in conversion off a high base - Manufacturer
0 %
Increase on conversion rate - B2B Service Business
+ 0 %
Increase on leads with a simple 1 page UX/UI revamp - B2B
+ 0
Awards & mentions across 4 different industries since 2009

Need a strategy?
Let’s point you in theย right direction

Required fields

Call us curious cats...

Blog

30 Jan 25

How To Center An Image In HTML?

Joseph Cheok | Web Development

If youโ€™ve ever tried to center an image in HTML, you probably found yourself thinking, “This should be simple.” And yet, somehow, it often isnโ€™t. Different devices, screen sizes, layout structures โ€” they all seem to conspire against you.

After nearly two decades building websites for clientsโ€”from tiny startups to government portalsโ€”Iโ€™ve run into this issue more times than I can count. Let me walk you through the methods that actually work, when to use them, and a few things to watch out for.

Because honestly? Itโ€™s not one-size-fits-all.

ย 

What Do We Even Mean By โ€œCenteringโ€ an Image?

Letโ€™s get on the same page first.

When most people say โ€œI need to center this image,โ€ they usually mean horizontallyโ€”getting it to sit nicely in the middle of its container, left to right.ย Sometimes you also want vertical centering, especially if youโ€™ve got banners, splash screens, or those fullscreen hero sections everyone loves these days.

The method you choose depends on:

  • What type of layout you’re building

  • How flexible it needs to be

  • What browsers or devices you’re targeting

  • And frankly, how lazy you feel that day

ย 

Method 1: The Old School text-align: center Hack

Youโ€™ve probably seen this one floating around Stack Overflow since about 2004. And yes, it still works โ€” in certain cases.

<div style="text-align: center;">
ย  <img src="your-image.jpg" alt="Centered Image">
</div>
ย 

Basically, since images are inline elements by default, text-align: center on the parent block will center them horizontally.

When do I use this?

  • Quick prototypes

  • Simple blogs or landing pages

  • Situations where you donโ€™t care about vertical centering

But โ€” and this is a big but โ€” it wonโ€™t help with vertical alignment. So if you need full centering, youโ€™ll have to look elsewhere.

ย 

Method 2: Flexbox (My Default Go-To)

80% of the time, I reach for Flexbox.

<div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
ย  <img src="your-image.jpg" alt="Centered Image">
</div>
  • justify-content: center handles horizontal

  • align-items: center handles vertical

  • height: 100vh makes the parent full screen (you can adjust this)

Flexbox is incredibly forgiving. Whether you’re dealing with fixed sizes, responsive images, or dynamic content, Flexbox handles it like a pro.

Use Flexbox when:

  • You want both horizontal and vertical centering

  • The layout might change later

  • Youโ€™re working with multiple elements inside the container

Real story: last year, I built an onboarding page for a fintech client. They kept changing image sizes during QA. Thanks to Flexbox, I didnโ€™t touch a single media query. Saved me hours.

ย 

Method 3: The Power of CSS Grid

Grid is like Flexboxโ€™s more sophisticated cousin. Bit more setup, but gives you ridiculous control.

<div style="display: grid; place-items: center; height: 100vh;">
ย  <img src="your-image.jpg" alt="Centered Image">
</div>
ย 

The place-items: center shorthand handles both axes in one go. Super clean.

Use Grid when:

  • Youโ€™re building complex layouts

  • You need both row and column control

  • You’re already using Grid elsewhere on the page

I had a dashboard project for a healthcare client last Octoberโ€”tons of little image cards. Grid made those layouts bulletproof across mobile, tablet, desktop. No headaches.

ย 

Method 4: Margin Auto (If You Know The Width)

Sometimes youโ€™re dealing with images that have a fixed width. Here, margin: auto still shines.

<div style="width: 100%; text-align: center;">
ย  <img src="your-image.jpg" alt="Centered Image" style="display: block; margin: 0 auto; width: 50%;">
</div>
ย 

Breakdown:

  • display: block turns the image into a block element.

  • margin: 0 auto centers it horizontally.

  • width: 50% keeps it responsive-ish.

When to use:

  • Product images

  • Portfolio thumbnails

  • Places where you can control image sizing ahead of time

 

Centering Images In Responsive Layouts

Hereโ€™s where people trip up.

Responsive layouts mean your container size changes based on device. If you donโ€™t handle that properly, youโ€™ll end up with stretched images, overflow, or weird white space.

Simple responsive example:

<div style="display: flex; justify-content: center;">ย 
ย  <img src="your-image.jpg" alt="Centered Image" style="max-width: 100%; height: auto;">
</div>
  • max-width: 100% ensures your image never spills outside its container.

  • height: auto keeps the aspect ratio locked.

Always test on mobile, tablet, desktop. I like using Chrome DevTools device toolbar โ€” it’s not perfect but gets you 90% there.

ย 

Common Issues (And How To Fix Them)

After 20 years, Iโ€™ve seen all the weird edge cases. Here are the most common:

  • Image not centering at all: You forgot display: flex or display: grid on the parent.

  • Fixed-width image not aligning: Double-check you added display: block and margin: 0 auto.

  • Images overflowing containers: You probably missed max-width: 100%.

  • Vertical centering failing: Check you actually applied align-items: center on Flexbox or Grid.

  • Still misaligned? Look for sneaky padding or margins on parent containers.

 

Quick Best Practices

A few final rapid-fire tips:

  • Test on real devices โ€” donโ€™t trust emulators 100%.

  • Always use descriptive alt text for accessibility.

  • Compress images โ€” tools like TinyPNG save load times.

  • Lock aspect ratios with height: auto.

  • Use relative units like %, vw, rem wherever you can.

 

Just Pick The Right Tool For The Job

At the end of the day, thereโ€™s no perfect method. Each option solves a slightly different problem.

I usually start with Flexbox. If the layout’s more complex, I bring in Grid. And if it’s something simple and static, margin: auto still gets plenty of use.

Honestly? Donโ€™t overthink it. The key is knowing which tool fits your layout needs.

Google Review Image