How to Center an Image on a Web Page

Six short steps, each with the code and a live example you can see working.

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.

The HTML
<img src="photo.jpg" alt="Describe the picture" width="400">
What you see
Sample image sitting on the left
Always write the 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.

The HTML
<div class="center-me">
  <img src="photo.jpg" alt="Describe the picture">
</div>
The CSS
.center-me {
  text-align: center;
}
What you see
Sample image centered with text-align
Common mistake: the 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.”

The CSS
img {
  display: block;
  margin: 0 auto;
}
What you see
Sample image centered with auto margins
Common mistake: leaving out 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:
Sample image that failed to center because display block is missing
This is the same approach used as an inline style on the ranking pages: 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.

The HTML
<div class="flex-center">
  <img src="photo.jpg" alt="Describe the picture">
</div>
The CSS
.flex-center {
  display: flex;
  justify-content: center;
}
What you see
Sample image centered with flexbox

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.

The CSS
.grid-center {
  display: grid;
  place-items: center;
}
What you see
Sample image centered with grid

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.

The CSS
.page-center {
  display: flex;
  justify-content: center;  /* left to right */
  align-items: center;      /* top to bottom */
  min-height: 100vh;        /* as tall as the window */
}
What you see (the box is 220px tall here instead of the full window)
Sample image centered both ways

Which method should you use?

MethodBest forCenters
A. text-align on parentAn image inside a paragraph or a simple wrapperHorizontally
B. display: block + margin: 0 autoOne image on its own, no wrapper neededHorizontally
C. FlexboxSeveral items, or when you also need vertical centeringBoth
D. Grid place-itemsShortest code for one centered itemBoth

Put it on your own site

Add one reusable rule to your styles.css, then give any image that class.

In styles.css
.center-img {
  display: block;
  margin: 0 auto;
}
In any page
<img src="images.webp" alt="Sora Matsushima" width="400" class="center-img">

Now run through this checklist if an image refuses to center:

  1. Did you use display: block with margin: 0 auto?
  2. Did you put text-align: center on the parent, not the image?
  3. Is the page linked to styles.css with <link rel="stylesheet" href="styles.css">?
  4. Does the file name match exactly, including capital letters? Home.png and home.png are different files on GitHub Pages.
  5. For vertical centering, did you give the container a height (like min-height: 100vh)?