Category:
Summary:
Explore CSS, Photoshop, and LaTeX methods to center an image horizontally and vertically, compare online tools, and choose the best approach for your project.

Centering methods at a glance — choose the one that fits your task.

Method Best For Horizontal Vertical Code? Difficulty Free?
margin: auto + display: block Static web images ✓ — Yes Easy ✓
CSS flexbox Responsive layouts ✓ ✓ Yes Medium ✓
CSS grid One-line centering ✓ ✓ Yes Medium ✓
text-align: center Inline images ✓ — Yes Easy ✓
OnlinePNGTools Quick PNG centering ✓ ✓ No Very easy ✓
SmallPNGTools Batch centering ✓ ✓ No Very easy 10/day
UWarp Design Simple web images ✓ ✓ No Very easy ✓
Photoshop (canvas) Graphic design ✓ ✓ No Medium trial
Photoshop (selection) Composition control ✓ ✓ No Medium trial
LaTeX \centering Academic / doc typesetting ✓ ✓ Yes Low ✓
Optical centering (javier.xyz) Visual balance ✓ ✓ No (tool) Advanced ✓

Colored pencils arranged in a circle with tips pointing inward, representing the concept of centering an image

Visual metaphor for centering — every element converges on a shared centre point.


All Methods to Center an Image at a Glance

The table above gives you a fast comparison. Here are a few trade-offs worth highlighting:

  • CSS flexbox or grid handle both horizontal and vertical centering in one container — they are the most flexible choice for modern web layouts.
  • margin: auto is the simplest CSS-only approach, but it only works horizontally and requires the image to be a block-level element.
  • text-align: center on a parent container centers inline images (like <img> by default) horizontally. Contrary to a misconception found in some forums, this property is valid CSS and is not deprecated by the W3C for images.
  • Online tools (OnlinePNGTools, SmallPNGTools, UWarp Design) require no coding — upload and download. Ideal for non-technical users or one-off tasks.
  • Photoshop gives you pixel-level control over placement and works with selections, not just the full canvas.
  • LaTeX \centering or the center environment is the standard for academic slides and papers.

The <center> tag is deprecated. It still renders in browsers, but HTML5 removed it from the specification. Modern best practice uses CSS — any of the methods above — instead.

If you only need one method for a website, start with CSS flexbox or grid — they handle both axes in a single container and adapt to responsive breakpoints naturally.

Infographic showing a Rubik's Cube with directional arrows and an HTML image tag, illustrating how to center an image with HTML and CSS

Centering an image with HTML and CSS can be visualised as aligning every face to a common centre.


Online Centering Tools for PNG Images

When you need to centre an image file itself — not its position on a web page — online tools offer a fast, code-free solution. Each tool works in your browser and outputs a centred PNG or JPG.

OnlinePNGTools
Upload a PNG, choose "Center Within Current Size" (keeps dimensions) or "Center Within New Size" (adds padding), then download the centred result. Supports transparency and offset adjustments.
Free

SmallPNGTools
Free for up to 10 uses per day for personal projects. Premium commercial licence available. Simple upload-and-centre workflow; ideal for batch processing when you need quick results.
Free (10/day) Premium

UWarp Design
A lightweight web interface with no account required. Upload an image, centre it with one click, and download. Best for casual or one-off centring tasks.
Free

When to use each tool

  • OnlinePNGTools — best for images with transparent backgrounds where you need to preserve alpha channels.
  • SmallPNGTools — good for batch work or when you need a premium licence for commercial use.
  • UWarp Design — quickest option for casual cropping and centring of JPG or PNG files.

These tools modify the image file itself. They are different from CSS centering, which controls placement inside a webpage layout.


CSS Centering Methods for Web Pages

CSS gives you several ways to centre an image inside a container. Each method suits a different layout situation.

Method 1: margin: auto + display: block

An <img> is inline by default, so margin: auto alone does nothing. Set it to display: block first, then apply auto margins.

img {
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: 100%;
}

This centres the image horizontally only. For vertical centering, you need a different approach.

Method 2: Flexbox

Wrap the image in a flex container and centre along both axes.

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    /* give the container a height if you want vertical centering */
    height: 100vh;
}

Flexbox works well for responsive layouts because the container can shrink or grow without breaking the centre alignment.

Method 3: Grid

CSS Grid can centre an image with a single property on the parent.

.container {
    display: grid;
    place-items: center;
}

place-items: center is shorthand that sets both align-items and justify-items to center, centring the image both horizontally and vertically. Support is modern but widely available.

Method 4: text-align: center

On the parent element, text-align: center centres inline-level content — including images that have not been set to display: block.

.parent {
    text-align: center;
}

This works horizontally only. For vertical centering, combine it with a line-height trick or use flexbox.

Clarification on text-align. Some forum posts claim the W3C "unrecommends" this property for images. That is a misunderstanding: text-align is a valid CSS property and works correctly on any inline element, including <img>.


Common Reasons Image Centering Fails and Solutions

Target with three arrows hitting the centre bullseye, representing successful image centering

Hitting the centre requires the right method for each layout context.

⚠ Problem 1: margin: auto does not centre the image
Root cause: The image is still inline by default. margin: auto only works on block-level elements.
✓ Fix: Add display: block to the image. Also check for typos — one user reported writing display: block inline, which is invalid and caused the rule to fail.

⚠ Problem 2: Images not centred in WordPress posts after exporting a theme
Root cause: The exported theme code may lack centering classes on .wp-image-* elements.
✓ Fix: Add a custom CSS class with display: block; margin: 0 auto; and assign it to the image block. Alternatively, wrap the image in a <figure> element with text-align: center on the parent.

⚠ Problem 3: text-align: center does not centre vertically
Root cause: text-align only affects horizontal alignment. It has no effect on the vertical axis.
✓ Fix: Use flexbox (display: flex; align-items: center; justify-content: center) or grid (place-items: center) when you need both axes centred.

⚠ Problem 4: Inline images on the same row need centering
Root cause: When images use display: inline (the default), margin: auto does not apply, and centering inside a row is tricky.
✓ Fix: Set the parent to display: flex; justify-content: center; or give each image display: inline-block and use text-align: center on the parent.


Photoshop Alignment Techniques

Photoshop interface with CENTER ANYTHING graphic, crosshairs, and directional arrows showing alignment controls

Photoshop's Move Tool provides one-click horizontal and vertical alignment.

Full canvas centering

  1. Select the entire canvas: Ctrl/Cmd + A.
  2. Switch to the Move Tool (shortcut V).
  3. In the options bar, click "Align Horizontal Centers" then "Align Vertical Centers".

The image is now centred exactly on the canvas. This method takes about two seconds once you know the icons.

Selection-based centering

When you want to centre an image within a specific area — not the full canvas — use the marquee tool to define that area first:

  1. Select the Rectangular Marquee Tool and draw a selection (for example, a one-third grid zone).
  2. Select the layer you want to centre.
  3. Switch to the Move Tool and click "Align Horizontal Centers" and "Align Vertical Centers" in the options bar.

This is useful when you need to centre a logo inside a phone mockup frame or align a subject within a compositional guide.


LaTeX Centering Commands

In LaTeX, centering an image inside a figure environment or a beamer slide is straightforward.

Using \centering

\begin{figure}[h]
    \centering
    \includegraphics[width=0.6\textwidth]{filename.png}
\end{figure}

\centering affects everything inside the current group. It does not add extra vertical space.

Using the center environment

\begin{center}
    \includegraphics{filename.png}
\end{center}

The center environment adds vertical spacing above and below, which may be desirable for slides or standalone figures.

For more control (e.g., centering within a specific width), the adjustbox package offers additional options.


From Design to Web: A Complete Centering Workflow

When you need pixel-level control over an image subject and reliable centering on a live website, combine three tools in sequence.

  1. Design your image in Photoshop
    Place your subject freely on the canvas. Use selection-based centering (marquee tool + Move Tool) to align the subject within a transparent canvas. This gives you fine control over the subject's position relative to the frame.

  2. Export as PNG with transparency
    Use Export > Export As > PNG with transparency enabled. The image retains its exact pixel dimensions.

  3. Use an online tool to add equal padding
    Upload your PNG to OnlinePNGTools and choose "Center Within New Size." This adds balanced padding around the subject so the file itself is geometrically centred. The visual centre will align better when the image is placed on a web page.

  4. Embed in HTML/CSS
    Use display: block; margin: 0 auto; max-width: 100% (or flexbox) to centre the image within its container. The file is already centred internally, and the CSS handles layout centering.

This cross-tool workflow is especially useful for product photography, icons, and logos where the subject carries visual weight on one side.


Optical Centering vs Geometric Centering

Circle divided into four quadrants by a horizontal and vertical line, representing geometric centering

Geometric centering measures equal distance from each edge; optical centering accounts for perceived weight.

Geometric centering is the mathematical midpoint — equal pixels on the left and right, equal pixels on the top and bottom. This is what margin: auto, flexbox, and Photoshop's alignment buttons give you.

Optical centering is where the image looks centred to the human eye. Because different colours and densities carry different visual weight, a geometrically centred subject can appear off-balance. As explained by the javier.xyz visual center algorithm, the weighted centre of an image is where the sum of squared pixel distances multiplied by colour difference from the background is equal on all sides.

How to apply optical centering manually in CSS

After centering an image with flexbox or grid, use transform: translate(x, y) to nudge it by small percentages until it looks balanced. For example:

.img-balanced {
    transform: translate(4%, -2%);
}

Alternatively, add asymmetrical padding or negative margins to shift the apparent centre. This approach is most useful for asymmetrical icons, product shots with heavy detail on one side, or logos with uneven visual mass.

When centre composition works in photography

In photography, placing the subject in the centre is a deliberate compositional choice. According to resources on centre composition, it works best when supported by leading lines, symmetry, or simplified frames that draw the eye inward. It is not a "default" position — it is a stylistic decision that, when used intentionally, creates strong visual impact.

Try it yourself. Upload an image to javier.xyz/visual-center to see the difference between the geometric centre and the optical centre of your file.