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

To center a standalone image horizontally in normal block layout, make it a block and set its inline margins to auto:

img {
  display: block;
  margin-inline: auto;
}

To center an image horizontally and vertically inside a container, use Flexbox or Grid on the container. The container needs available space on the axis you want to center.

How do I center an image horizontally?

For an image in normal block layout, use display: block with automatic inline margins:

.centered-image {
  display: block;
  margin-inline: auto;
}

Apply the class to the image. The automatic margins divide any available inline space on either side of the image, centering it when the image is narrower than its containing block. If the image is as wide as its container, there is no extra space to distribute and no visible centering effect.

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

margin-inline sets the margins at the start and end of the inline axis, making the rule suitable for different writing directions. This pattern centers horizontally in block flow; it does not center the image vertically. MDN explains how automatic margins absorb available space in block layout.

How do I center an image vertically or on both axes?

Use a parent container when you need to control the image’s position on both axes. Flexbox and Grid provide straightforward options.

Rank #2
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

Flexbox

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

Place the image inside an element with the image-container class. justify-content aligns items along the flex container’s main axis; align-items aligns them along its cross axis. With the default flex-direction: row, the main axis is horizontal, so these rules center the image horizontally and vertically. If you change the flex direction, the axes change too. MDN’s Flexbox guide describes the main and cross axes.

Grid

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

place-items: center centers grid items on both axes. MDN documents place-items as shorthand for align-items and justify-items.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
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

The example’s min-height supplies room for vertical alignment; choose a height appropriate to your design. If the container is only as tall as the image, there may be no visible vertical movement.

How do I center an image in a div?

Make the div the layout container, then center its image using Flexbox or Grid. For example:

<div class="image-container">
  <img src="photo.jpg" alt="A description of the photo">
</div>

Use either the Flexbox or Grid rule above on .image-container. Centering properties belong on that parent container, not on the image itself. If you only need horizontal centering and the image is in normal block layout, you can instead apply display: block; margin-inline: auto; directly to the image.

Center one item in an existing grid

If the image is already a grid item and you want to align only that item, use place-self: center on the image:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.image-container img {
  place-self: center;
}

MDN’s Grid alignment guide covers item alignment, including place-self.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why is margin: auto not centering my image?

  • The image is inline. The block-margin pattern relies on the image being a block-level element. Add display: block if it is in normal block layout.
  • The image fills the available width. Automatic margins have no remaining space to split. Check the image’s rendered width and the width of its containing block.
  • You are trying to center vertically. Automatic inline margins center along the inline axis, not both axes. Use a parent Flexbox or Grid container for two-axis alignment.
  • The alignment rules are on the wrong element. Put justify-content and align-items on a flex container, or place-items on a grid container.
  • The container has no extra room on the desired axis. Give it suitable dimensions or a minimum height so the centered position can be visible.
  • The flex direction changes which axis is which. In Flexbox, justify-content follows the main axis and align-items the cross axis; check the container’s flex-direction.

Do not use justify-items as a Flexbox centering property; MDN notes that it is not implemented for Flexbox. Use justify-content and align-items on the flex container instead. See MDN’s justify-items reference.

Or skip the browser setup

If you need a screenshot to check a page’s layout, ScreenshotNeo can capture a URL with one request. For example, using cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for setup and options. It accepts cookie banners and removes more than 60 known consent platforms, newsletter popups and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads and cache hits cost nothing, and response headers identify the page verdict and whether a request was billed. ScreenshotNeo also provides an MCP server with tools for AI agents, including Claude and Cursor. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Visit ScreenshotNeo to learn more, or sign up for 1,000 free screenshots a month with no card.

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

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.