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.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11#1 Best Overall
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
- 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.
Rank #3
- 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:
Rank #4
<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:
Best Value
.image-container img {
place-self: center;
}
MDN’s Grid alignment guide covers item alignment, including place-self.
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: blockif 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-contentandalign-itemson a flex container, orplace-itemson 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-contentfollows the main axis andalign-itemsthe cross axis; check the container’sflex-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:
Quick Recap
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.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →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.

