Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For horizontal centering, make the image a block and give it automatic side margins. To center it horizontally and vertically, put the alignment rules on the parent with Flexbox or Grid. These positioning rules are separate from deciding whether the image should be contained or cropped inside a fixed-size box.

Center an image horizontally

Use this pattern when the image should sit in the middle from left to right, but does not need vertical centering in a tall container:

<div class="container">
  <img src="photo.jpg" alt="Description of the image">
</div>
.container img {
  display: block;
  margin-inline: auto;
  max-width: 100%;
}

Images are inline elements by default. display: block enables the automatic horizontal-margin pattern, and margin-inline: auto divides leftover inline space equally on both sides. max-width: 100% keeps the image from becoming wider than its containing block.

This centers the image only on the horizontal axis. It does not place the image halfway down a taller parent.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Center an image on both axes with Flexbox

Use the parent as a flex container when the image must be centered horizontally and vertically:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 16rem;
}

With the default row direction, justify-content centers items along the horizontal main axis and align-items centers them along the vertical cross axis.

The parent needs available block-axis space for vertical centering to be visible. A min-height, fixed height, or another layout context that gives the container height provides that space. If the container is only as tall as the image, there is no extra vertical room to distribute.

Center an image on both axes with Grid

Grid offers a compact alternative:

.container {
  display: grid;
  place-items: center;
  min-height: 16rem;
}

place-items: center sets both the block and inline alignment of the grid item. As with Flexbox, vertical centering requires the parent to be taller than the image.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right centering method

Method Best fit Limitation or decision
display: block; margin-inline: auto Horizontal centering of one image Does not vertically center an image in a tall parent
Parent Flexbox Centering on both axes, especially when the parent already uses Flexbox The parent needs available height for visible vertical centering
Parent Grid with place-items: center Compact two-axis centering The parent also needs available height for visible vertical centering
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Centering versus fitting or cropping the image

Centering positions the <img> element. It does not decide how the image pixels should fit inside that element when you use a fixed-size box.

For a thumbnail or media frame with a defined width and height, size the image element and then choose object-fit:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
.container {
  display: grid;
  place-items: center;
  width: 18rem;
  height: 12rem;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

object-fit: contain

contain preserves the complete image while fitting it inside the image element’s box. If the image and box have different aspect ratios, unused space can remain around the image.

object-fit: cover

cover fills the image element’s box while preserving the image’s aspect ratio. When the aspect ratios differ, part of the image overflows and is cropped.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Control the focal point of a crop

When cover cuts off important content, use object-position to move the visible focal point:

.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
}

object-fit has no useful sizing effect unless the image element itself has a constrained width and height (or equivalent dimensions from its layout).

Common fixes when centering does not work

  • The image is still left-aligned: check that the image is display: block when using automatic margins, and apply the margins to the image itself.
  • Vertical centering appears ineffective: give the parent a height or minimum height larger than the image.
  • The image overflows its parent: add max-width: 100% for a responsive image, or explicitly size the image for a fixed box.
  • The image is centered but looks cropped: change object-fit: cover to contain if showing the entire image matters more than filling the box.
  • The image is distorted: use object-fit: contain or cover on a sized image instead of forcing unequal dimensions without an object-fit rule.

Complete examples

Responsive image centered horizontally

<div class="container">
  <img src="photo.jpg" alt="Description of the image">
</div>
.container img {
  display: block;
  margin-inline: auto;
  max-width: 100%;
  height: auto;
}

Image centered in a tall panel

<div class="container">
  <img src="photo.jpg" alt="Description of the image">
</div>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 16rem;
}

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

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.