By default, a browser puts every image on the left side of its container. Centering it takes one small piece of CSS. Which piece depends on how you want to do it, so we will go from the simplest idea to the most powerful one. In every example below, the dashed box is the “page” and the blue rectangle is the image.
1Start with a normal image
Here is an image in plain HTML. Nothing is centering it, so it sits against the left edge.
<img src="photo.jpg" alt="Describe the picture" width="400">
alt text. It is read aloud for people who cannot see the image and shown if the image fails to load.2Method A: text-align: center on the parent
An <img> is an inline element, which means it behaves like a letter in a line of text. So you can center it the same way you center text: put it inside a container and center the container's contents.
<div class="center-me">
<img src="photo.jpg" alt="Describe the picture">
</div>
.center-me {
text-align: center;
}
text-align goes on the parent (the <div>), not on the <img> itself. Putting it on the image does nothing.3Method B: display: block and margin: 0 auto
This is the most popular way, and it is the one you can put directly on the image with no wrapper. Two lines do the job. First, display: block turns the image into a block-level box. Second, margin: 0 auto says “zero space above and below, and split all the leftover space equally on the left and right.”
img {
display: block;
margin: 0 auto;
}
display: block. Auto margins do not center an inline element, so the image stays on the left. Here is that exact mistake, with margin: 0 auto but no display: block:style="display: block; margin: 0 auto;". A class in styles.css does the same thing without repeating it on every image (see Step 6).4Method C: Flexbox
Flexbox is the modern layout tool. Turn the parent into a flex container, then tell it to center its children along the horizontal line.
<div class="flex-center">
<img src="photo.jpg" alt="Describe the picture">
</div>
.flex-center {
display: flex;
justify-content: center;
}
justify-content controls the main (horizontal) direction. Its partner align-items controls the other (vertical) direction, which is what makes Step 6 possible.
5Method D: Grid
CSS Grid has the shortest centering code of all. One line centers the image both horizontally and vertically inside its container.
.grid-center {
display: grid;
place-items: center;
}
6Center it in the middle of the whole page
So far we centered left-to-right. To center an image vertically too, the container needs a height to be centered within. Give it the full height of the browser window with 100vh (“100% of the viewport height”), then center in both directions.
.page-center {
display: flex;
justify-content: center; /* left to right */
align-items: center; /* top to bottom */
min-height: 100vh; /* as tall as the window */
}
Which method should you use?
| Method | Best for | Centers |
|---|---|---|
A. text-align on parent | An image inside a paragraph or a simple wrapper | Horizontally |
B. display: block + margin: 0 auto | One image on its own, no wrapper needed | Horizontally |
| C. Flexbox | Several items, or when you also need vertical centering | Both |
D. Grid place-items | Shortest code for one centered item | Both |
Put it on your own site
Add one reusable rule to your styles.css, then give any image that class.
.center-img {
display: block;
margin: 0 auto;
}
<img src="images.webp" alt="Sora Matsushima" width="400" class="center-img">
Now run through this checklist if an image refuses to center:
- Did you use
display: blockwithmargin: 0 auto? - Did you put
text-align: centeron the parent, not the image? - Is the page linked to
styles.csswith<link rel="stylesheet" href="styles.css">? - Does the file name match exactly, including capital letters?
Home.pngandhome.pngare different files on GitHub Pages. - For vertical centering, did you give the container a height (like
min-height: 100vh)?