Recommended Free Tools
To center a standalone image horizontally in normal block layout, make it a block and give it automatic inline margins:
img {
display: block;
margin-inline: auto;
}
To center an image both horizontally and vertically inside a container, use Flexbox or Grid on the parent. The container needs extra space on the axis where you want centering to be visible.
As an Amazon Associate I earn from qualifying purchases.
How do I center an image horizontally?
For an image in normal block flow, set the image to display: block and use automatic inline margins:
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 →.centered-image {
display: block;
margin-inline: auto;
}
Automatic margins distribute the available inline space on either side of a block. The image must be narrower than its containing block for the centering to show. If it already fills the available width, there is no extra space to distribute. This pattern centers horizontally; it does not center the image vertically. MDN explains how auto margins absorb available space in block layout.
#1 Best Overall
How do I center an image horizontally and vertically?
Apply alignment to the parent container, not just to the image. Choose the layout already used by the parent, or use Grid for a simple two-axis layout.
Use Flexbox
.container {
min-height: 16rem;
display: flex;
justify-content: center;
align-items: center;
}
justify-content aligns items on Flexbox’s main axis, while align-items aligns them on its cross axis. With the default row direction, the main axis is horizontal and the cross axis is vertical. If you change flex-direction or writing mode, the physical directions can change. MDN describes Flexbox’s main and cross axes.
Rank #2
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Use Grid
.container {
min-height: 16rem;
display: grid;
place-items: center;
}
place-items centers the grid item’s alignment and justification within its grid area. The container needs room for the image to move; the example’s min-height supplies vertical space. MDN documents alignment in Grid.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsHow do I center an image in a div?
Make the div the layout container, then center its child. For two-axis centering, either of these works:
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
.image-frame {
min-height: 16rem;
display: flex;
justify-content: center;
align-items: center;
}
/* Or use Grid instead: */
.image-frame-grid {
min-height: 16rem;
display: grid;
place-items: center;
}
Use the class that matches the container’s layout; do not apply both examples to the same element unless you specifically intend to switch its display mode. If the div has no extra height, a short image may appear unchanged by vertical centering. For an existing Grid layout where only one item should be centered, place-self: center can align that item without changing the other grid items.
Why is margin: auto not centering my image?
- The image is still inline. In normal block layout, use
display: blockwithmargin-inline: auto. If its parent is Flexbox or Grid, center it through that parent instead. - The image fills the available width. Automatic margins can only distribute leftover space. Check the image’s rendered width and its containing block.
- You are trying to center vertically. Auto inline margins address the inline direction, not vertical placement. Use a parent Flexbox or Grid layout for two-axis centering.
- The alignment properties are on the wrong element.
justify-contentandalign-itemsbelong on a flex container;place-itemsbelongs on a grid container. - The container has no room on the axis you want to center. Give it a suitable size or minimum size, such as
min-height, when vertical centering is needed. - You used
justify-itemswith Flexbox. It is not implemented for Flexbox; usejustify-contentandalign-itemson the flex container. See MDN’s note onjustify-items.
Or skip the browser setup
If you need a screenshot of the centered page to check or share the result, ScreenshotNeo can return one from a single request. For example, this cURL command captures a page as WebP:
Quick Recap
Best Value
Rank #4
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 request options. It accepts cookie and consent banners and removes known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and the response identifies the page verdict and billing status. An MCP server offers screenshot tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for free.
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.




