How To Center Image in HTML and CSS [Complete Guide]

Advertisement

Have you ever wondered how to perfectly center an image on a webpage using CSS?

Centering an image can be a bit tricky, especially for beginners. But fear not! In this complete guide, we will walk you through the best practices to achieve a beautifully centered image on your website.

Method 1: Using CSS Flexbox

CSS Flexbox is a powerful layout module that simplifies centering elements. Here’s how you can use it to center an image:

  1. Create a container element for your image. For example, you can use a <div> element with a class name like image-container.
  2. Apply the following CSS properties to the image-container:
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

  1. Place your image inside the container element, like this:
<div class="image-container">
  <img src="path/to/your/image.jpg" alt="Description of the image">
</div>

  1. That’s it! Your image will now be perfectly centered both horizontally and vertically within the image-container.

Method 2: Using CSS Grid

CSS Grid is another powerful layout module that allows you to create complex grid-based layouts. Here’s how you can center an image using CSS Grid:

  1. Create a container element for your image, similar to the previous method.
  2. Apply the following CSS properties to the image-container:
.image-container {
  display: grid;
  place-items: center;
}

  1. Place your image inside the container element, just like before.
<div class="image-container">
  <img src="path/to/your/image.jpg" alt="Description of the image">
</div>

  1. Voila! Your image will now be centered both horizontally and vertically within the image-container.

Method 3: Using Margin Auto

If you prefer a simpler approach, you can center an image using the margin property. Here’s how:

  1. Add the following CSS properties to your image:
.image {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

  1. Now, when you place the image on your webpage, it will be centered horizontally.
<img class="image" src="path/to/your/image.jpg" alt="Description of the image">

Method 4: Using Text-Align Center (for inline images)

If you have an inline image within a block of text and you want to center it horizontally, you can use the text-align property. Here’s how:

  1. Wrap your image with a <span> element and give it a class name, such as center-image.
<span class="center-image">
  <img src="path/to/your/image.jpg" alt="Description of the image">
</span>

  1. Apply the following CSS properties to the center-image class:
.center-image {
  display: block;
  text-align: center;
}

  1. Your inline image will now be centered horizontally within the text block.

Conclusion

Centering images in CSS doesn’t have to be a daunting task. By using CSS Flexbox, CSS Grid, margin auto, or text-align center, you can easily achieve beautiful image centering on your website. Experiment with these methods and find the one that best fits your design needs. Happy coding!

author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.