How to center an image with CSS

To center an image horizontally, use text-align: center on the parent of an inline image, or margin-inline: auto on a block image. For both horizontal and vertical centering, make the parent a Grid container and set place-items: center.

Center an inline image from its parent

.photo-area {
  text-align: center;
}
.photo-area img {
  max-width: 100%;
  height: auto;
}

text-align aligns inline content within the parent’s lines, including text on the same line. Use it when img is still inline. If the image already uses display: block, use the block method below. vertical-align positions an inline image relative to its line. Grid can center the image within the full height of its container. See CSS Text · text-align.

Center a block image with automatic margins

.photo {
  display: block;
  width: 240px;
  max-width: 100%;
  height: auto;
  margin-inline: auto;
}

Automatic side margins split the leftover width around a block image. In horizontal writing, margin-inline: auto is equivalent to setting both margin-left: auto and margin-right: auto. The sample width is 240px, capped by max-width: 100%. If width: 100% makes the image fill the parent, there is no leftover space to distribute. See CSS 2.2 · Block-level replaced element width and margins.

Compare three methods in a complete example

First save the SVG as sample.svg in UTF-8. Save the HTML as image-center.html in the same folder and open it. If the image is missing, check the file names and extensions. No external resources or JavaScript are required.

<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360" viewBox="0 0 640 360">
  <rect width="640" height="360" fill="#dff2f3"/>
  <circle cx="480" cy="90" r="42" fill="#f3ba51"/>
  <path d="M0 360 210 100 410 360Z" fill="#298e96"/>
  <path d="M220 360 440 170 640 360Z" fill="#17618a"/>
</svg>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare image centering methods</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; }
    h2 { margin: 16px 0 8px; font-size: 18px; }
    .frame { max-width: 500px; border: 1px solid #7896ac;
      padding: 12px; background: #f3f6fa; }
    .frame img { width: 160px; max-width: 100%; height: auto; }
    .text-center { text-align: center; }
    .text-center img { vertical-align: middle; }
    .margin-center img { display: block; margin-inline: auto; }
    .both-center { display: grid; place-items: center; min-height: 160px; }
    .both-center img { display: block; }
  </style>
</head>
<body>
<h1>Compare image centering methods</h1>
<h2>Using the parent’s text-align</h2>
<div class="frame text-center">
  <img src="sample.svg" width="640" height="360" alt="Sample picture of the sun and two mountains">
</div>
<h2>Automatic margins on a block image</h2>
<div class="frame margin-center">
  <img src="sample.svg" width="640" height="360" alt="Sample picture of the sun and two mountains">
</div>
<h2>Center horizontally and vertically with Grid</h2>
<div class="frame both-center">
  <img src="sample.svg" width="640" height="360" alt="Sample picture of the sun and two mountains">
</div>
</body>
</html>

The first two images are horizontally centered. The last is centered on both axes in a grid cell. At a width of 160px, the image is 90px tall. With a 160px-tall final frame, 35px remains above and below. Change only its min-height to 220px and each space becomes 65px. Restore 160px after comparing.

For Grid, place-items: center sets both align-items and justify-items. If the parent uses Flexbox, set align-items: center and justify-content: center separately. Vertical centering needs spare space, so make the parent taller than the image to see the difference. See CSS Box Alignment · place-items.

Check why an image does not move

If centering has no visible effect, inspect the image’s computed display and width, and the parent’s content width. Floating or absolutely positioned images do not follow the ordinary block example’s margin behavior. If a theme overrides the margins, inspect which selector wins.

To crop a photo to fill a frame, size the image box and apply an appropriate object-fit: cover rule separately. To overlay a caption instead, see how to center text over an image.