Jomatter

How to Center an Image in HTML? (With CSS Examples)

To center an image in HTML, use CSS to control its alignment. The simplest method is to set display: block and margin: 0 auto on the image. This centers it horizontally within its parent container.

You can also use text-align: center on a parent element, or CSS Flexbox and Grid when you need to center an image both horizontally and vertically.

The right method depends on how your image is positioned within the webpage and whether you need horizontal or vertical alignment.

1. How to Center an Image Horizontally Using CSS

The easiest way to center a standalone image is to change its display property to block and set its left and right margins to auto.

HTML

<img src="image.jpg" alt="A beautiful landscape" class="center-image">

CSS

.center-image {
    display: block;
    margin: 0 auto;
}

How it works:

  • display: block; changes the image from its default inline display to a block-level element.
  • margin: 0 auto; allows the browser to distribute available horizontal space equally between the left and right margins.
  • The image appears in the horizontal center of its containing element.

This method is suitable for standalone images in blog posts, articles, website sections, and landing pages.

Make the Centered Image Responsive

To prevent an image from overflowing its parent container on smaller screens, add max-width: 100% and height: auto.

.center-image {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
}

The image can shrink to fit its container while maintaining its original aspect ratio.

2. How to Center an Image Using text-align: center

Another method is to apply the CSS text-align: center property to the image’s parent element.

This method works because an HTML image is inline by default. The parent element’s text alignment can therefore control its horizontal position.

HTML

<div class="image-container">
    <img src="image.jpg" alt="A beautiful landscape">
</div>

CSS

.image-container {
    text-align: center;
}

The image will appear in the horizontal center of the container.

When should you use this method?

Use text-align: center when you want to center an inline image without changing its display behavior.

One important detail: apply text-align: center to the parent element, not directly to the image. Also, this property centers other inline content inside the same container, including text.

3. How to Center an Image Horizontally and Vertically Using Flexbox

When an image needs to be centered in both directions, CSS Flexbox provides a convenient solution.

Unlike the margin method, Flexbox controls the alignment of child elements through their parent container.

HTML

<div class="image-container">
    <img src="image.jpg" alt="A beautiful landscape">
</div>

CSS

.image-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 300px;
}

.image-container img {
    max-width: 100%;
    height: auto;
}

How it works:

  • display: flex; makes the parent a Flexbox container.
  • justify-content: center; centers the image horizontally along the main axis.
  • align-items: center; centers the image vertically along the cross axis.
  • min-height: 300px; gives the container enough height to demonstrate vertical centering.

Flexbox is particularly useful for hero sections, image cards, product displays, and other layouts where an image needs to remain centered inside a defined area.

If the image is taller than the available container space, additional sizing constraints may be necessary.

4. How to Center an Image Using CSS Grid

CSS Grid is another option for centering an image horizontally and vertically.

Its place-items: center property combines horizontal and vertical item alignment in a single declaration.

HTML

<div class="image-container">
    <img src="image.jpg" alt="A beautiful landscape">
</div>

CSS

.image-container {
    display: grid;
    place-items: center;
    min-height: 300px;
}

.image-container img {
    max-width: 100%;
    height: auto;
}

How it works:

  • display: grid; creates a grid container.
  • place-items: center; centers the image within its grid area on both axes.
  • min-height: 300px; provides vertical space for the image to be centered.

CSS Grid is useful when building structured layouts, image galleries, content cards, and sections that require precise alignment.

5. How to Center Multiple Images in HTML

When you need to center multiple images in one row, use Flexbox on their parent container.

HTML

<div class="image-gallery">
    <img src="image1.jpg" alt="First image">
    <img src="image2.jpg" alt="Second image">
    <img src="image3.jpg" alt="Third image">
</div>

CSS

.image-gallery {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.image-gallery img {
    max-width: 100%;
    height: auto;
}

The justify-content: center property centers the image group horizontally, while gap: 20px adds space between images.

The flex-wrap: wrap property allows images to move onto additional rows when there is insufficient horizontal space.

This approach works well for image galleries, portfolios, product collections, and logo sections.

6. How to Center an Image in HTML Using Inline CSS

If you cannot edit an external stylesheet, you can apply CSS directly to the image using the style attribute.

HTML

<img
    src="image.jpg"
    alt="A beautiful landscape"
    style="display: block; max-width: 100%; height: auto; margin: 0 auto;"
>

This applies the same horizontal centering technique without requiring a separate CSS rule.

Inline CSS is useful for quick adjustments, but reusable CSS classes are generally easier to maintain when multiple images need the same styling.

7. Common Reasons an Image Is Not Centering

Sometimes an image does not appear centered even after applying CSS. This usually happens because the alignment property is being applied to the wrong element or the image does not have enough available space around it.

Here are some common problems and their solutions.

ProblemPossible reasonSolution
margin: auto does not workImage is still inlineAdd display: block;
Image remains left-alignedtext-align is applied to the wrong elementApply it to the parent container
Image is not vertically centeredParent has no available vertical spaceGive the parent a suitable height or minimum height
Image overflows on mobileImage is wider than its containerAdd max-width: 100%; height: auto;
Image fills the entire containerImage has width: 100%Use a smaller width or max-width when you want visible side spacing
Flexbox does not center the imageAlignment rules are applied to the child instead of the parentSet justify-content and align-items on the flex container

Why Does margin: auto Sometimes Fail?

The margin: auto technique works for centering a block-level image when there is available horizontal space.

If the image remains inline, automatic margins will not provide the expected centering behavior. Changing the image to display: block fixes this issue.

Additionally, an image that occupies the full width of its parent has no remaining horizontal space to distribute between its margins.

Why Is My Image Not Vertically Centered?

Horizontal and vertical centering are different layout operations.

Setting margin: 0 auto centers an image horizontally, but it does not automatically center it vertically.

For vertical alignment, use Flexbox or Grid and ensure the parent container has sufficient height.

Frequently Asked Questions

1. What is the easiest way to center an image in HTML?

The easiest method is to use display: block and margin: 0 auto in CSS.

img {
    display: block;
    margin: 0 auto;
}

2. How do I center an image vertically in HTML?

Use Flexbox or Grid on the parent container.

For Flexbox, apply display: flex and align-items: center. Make sure the container has sufficient height for vertical alignment to be visible.

3. Can I center an image without using a div?

Yes. You can apply CSS directly to the image element.

img {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

You do not need an additional div when the image itself is the element you want to center.

4. How do I center an image on mobile devices?

Use responsive CSS to prevent the image from exceeding its container width.

img {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
}

This allows the image to fit smaller screens while preserving its proportions.

5. Should I use Flexbox or CSS Grid to center an image?

Both methods can center an image horizontally and vertically. Flexbox is convenient for one-dimensional layouts, such as a row of images. Grid is useful when working with grid-based layouts or when you want to center an item with place-items: center.

Conclusion

Centering an image in HTML is straightforward when you choose the appropriate CSS technique.

For a single image, display: block with margin: 0 auto is usually the simplest solution. Use text-align: center when the image needs to remain inline, and choose Flexbox or CSS Grid when you need horizontal and vertical alignment.

For responsive layouts, remember to use max-width: 100% and height: auto so images fit their containers without distorting their proportions.

More Related Guide

How Do You Indent In Html
How Long Does It Take To Learn Html
How To Convert Html To Excel
How To Embed A Google Form In Html
How To Get Html Code From A Website
How To Hide Text In Html
What Are Tags In Websites
What Does Hr Stand For In Html
What Is The Difference Between Css And Html
How To Add An Image From A Folder In Html
How To Add Expires Headers In Html Catswhocode
How To Copy Html Code From A Website
How To Create A Banner In Html