Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Center an Image in CSS

Use block display and auto inline margins to center an image horizontally; use Flexbox or Grid on its container to center it on both axes.
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 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:

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

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
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

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.

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

How 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
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
.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: block with margin-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-content and align-items belong on a flex container; place-items belongs 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-items with Flexbox. It is not implemented for Flexbox; use justify-content and align-items on the flex container. See MDN’s note on justify-items.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.