To learn how to center an image in HTML, begin with CSS. Make the image a block and give it automatic margins on both sides. Use display: block; margin-inline: auto; to center it horizontally. To center it horizontally and vertically, place it inside a Flexbox or Grid container.
Choose the method that fits the space around your image. For example, a picture in an article has different needs from a logo inside a tall card. Compare the options below, copy the complete example, and resize your browser to see how the image behaves.

How to center an image in HTML: the quick answer
Create a CSS class for the image, then use that class name in your stylesheet. The following rules center the image inside its parent. They also let it shrink when there is less horizontal space.
<img class="centered-image" src="mountains.jpg"
alt="Snow-covered mountains beside a lake"
width="640" height="400">
.centered-image {
display: block;
max-width: 100%;
height: auto;
margin-inline: auto;
}
Replace mountains.jpg with the actual path to your picture. Also, make sure the width and height attributes match the file’s pixel dimensions. In this example, the image begins at 640 pixels wide. As a result, it gets smaller when its container has less room.
By default, an image participates in inline layout. Therefore, setting display: block lets automatic side margins absorb the spare horizontal space. The W3C explanation of centering images demonstrates the same block-and-auto-margin approach.
In a normal horizontal writing mode, margin-inline sets the left and right margins. It follows the writing direction rather than hard-coding physical sides, as MDN’s margin-inline reference explains. Alternatively, write margin-left: auto; margin-right: auto;.
Choose the right image-centering method
| Your layout | Apply CSS to | Use | What it centers |
|---|---|---|---|
| A standalone article image | The image | display: block; margin-inline: auto |
Horizontally |
| An inline image beside text | The parent | text-align: center |
Inline content horizontally |
| An image in a card | The parent | Flexbox with both alignment properties | Both axes |
| A single image in a panel | The parent | display: grid; place-items: center |
Both axes |
Choose one method at a time until you have worked out your layout. Floats, automatic margins, and absolute positioning can interfere with each other. Consequently, adding several approaches at once makes the cause harder to find.
A complete HTML example you can copy
Save the code below as index.html and add a 640-by-400 image named mountains.jpg next to it. Next, open the file in your browser and resize the window. As a result, the image stays horizontally centered without stretching past its original width.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Centered image example</title>
<style>
body {
margin: 0;
padding: 1rem;
font-family: sans-serif;
}
main {
max-width: 60rem;
margin-inline: auto;
}
.centered-image {
display: block;
max-width: 100%;
height: auto;
margin-inline: auto;
}
</style>
</head>
<body>
<main>
<h1>A centered landscape photo</h1>
<img class="centered-image" src="mountains.jpg"
alt="Snow-covered mountains beside a lake"
width="640" height="400">
</main>
</body>
</html>
For convenience, the style block keeps this example in one file. On a larger site, move those CSS rules into an external stylesheet. Our guide to linking CSS to HTML shows where to place the stylesheet link and how to check its path.
Can you center the image with an inline style?
Yes. An inline style gives the same result when you can edit the image tag but cannot change a stylesheet:
<img src="mountains.jpg"
alt="Snow-covered mountains beside a lake"
width="640" height="400"
style="display: block; max-width: 100%; height: auto; margin-inline: auto;">
This still uses CSS. In other words, the rules are embedded in the HTML. However, a reusable class makes more sense when you want the same layout on several images.
Center an inline image with text-align
To center an image that stays inline, put text-align: center on its parent container. As a result, this centers all the container’s inline content, including text and inline images.
<div class="image-line">
<img src="logo.png" alt="Acme" width="160" height="80">
</div>
<style>
.image-line {
text-align: center;
}
.image-line img {
max-width: 100%;
height: auto;
}
</style>
In contrast, setting text-align: center on the image itself will not move its box. In addition, this method stops centering the image if another CSS rule sets it to display: block. Instead, use automatic side margins in that case.
How to center an image in HTML horizontally and vertically
Use Flexbox or Grid when an image needs to sit in the middle of a larger element. For this to work, give the parent a height or minimum height. Otherwise, there may be no spare vertical space for centering.
Use Flexbox for a centered image inside a card
<div class="image-card">
<img src="mountains.jpg" alt="Mountains beside a lake"
width="640" height="400">
</div>
<style>
.image-card {
display: flex;
justify-content: center;
align-items: center;
min-height: 24rem;
padding: 1rem;
}
.image-card img {
max-width: 100%;
height: auto;
}
</style>
With the default flex-direction: row in horizontal writing, justify-content centers horizontally and align-items centers vertically. If you switch to flex-direction: column, those axis roles swap. The web.dev guide to centering in CSS explores how Flexbox and other approaches behave as content and available space change.
Use Grid for a simple centered panel
Keep the same image markup, change the parent’s class to image-panel, and apply these rules:
.image-panel {
display: grid;
place-items: center;
min-height: 24rem;
padding: 1rem;
}
.image-panel img {
max-width: 100%;
height: auto;
}
In this Grid example, place-items: center centers the single image on both axes within its grid area. However, that shorthand alone does not do the same job in Flexbox. Instead, use justify-content and align-items for the Flexbox version above.
Watch a CSS centering walkthrough
Watch Adam Argyle’s “Thinking on ways to solve CENTERING” video from Chrome for Developers to compare several approaches. Then resize the examples above and look at what changes when the available space gets smaller.
Center an image and its caption together
When an image has a caption, center the entire figure first. Next, allow the image to fill that figure. As a result, the picture and caption will stay together across different screen sizes.
<figure class="photo">
<img src="mountains.jpg" alt="Snow-covered peaks above a lake"
width="640" height="400">
<figcaption>The mountain range seen from the lakeshore.</figcaption>
</figure>
<style>
.photo {
width: min(100%, 40rem);
margin: 1.5rem auto;
}
.photo img {
display: block;
width: 100%;
height: auto;
}
.photo figcaption {
margin-top: 0.5rem;
text-align: center;
}
</style>
Our explanation of the figure and figcaption elements covers the markup. A caption supplies context. In contrast, alt text describes the image’s purpose for people who cannot see it. Write each for that job instead of repeating the same words.
Why is your image still not centered?
First, open your browser’s developer tools and inspect the image. Look at its computed display, width, margins, float, and position. Then check the parent element. An image centers relative to its container, which may be narrower than the page.
| Problem | Likely cause | Fix |
|---|---|---|
| Auto margins do nothing | The image is still inline | Add display: block |
| No visible space on either side | The image fills the container | Use a smaller width or a wider parent |
| The image stays on the left | A float or another CSS rule overrides yours | Remove the float and check the cascade |
| Vertical centering seems broken | The parent is only as tall as the image | Give the parent a suitable min-height |
| The box is centered but the artwork looks off | The file contains uneven blank or transparent space | Crop the source image or adjust its export |
Add a temporary outline to the parent and a different outline to the image. You can then see whether the issue is the container, the image box, or blank space inside the file. Finally, remove both outlines after finding the cause.
Common questions about centering images
How do you center an image without CSS?
Modern HTML does not provide a recommended presentation-only centering tag. Therefore, avoid the obsolete <center> element and old align attributes. Apply the centering through a stylesheet, a style block, or an inline CSS style.
Does margin: auto center an image vertically?
Automatic margins can center a block image horizontally in ordinary block layout. However, vertical automatic margins resolve to zero there. For vertical centering, put the image inside a Flexbox or Grid parent that has enough height.
How do you keep a centered image responsive?
Add max-width: 100%; height: auto; to your centering rules. As a result, the image can shrink and keep its aspect ratio. With <picture>, add the centering class to the nested <img>, which is the rendered image box.
How do you center an image in HTML without stretching it?
Begin with display: block; margin-inline: auto; max-width: 100%; height: auto;. Avoid width: 100% if you want to prevent a small image from filling a larger parent. Finally, test at a narrow screen width and check the space on both sides.
Join the discussion.