CSS Image Centering
In this page:
img {
display: block;
margin: 0 auto;
}
Horizontal Centering with margin: auto
A block-level image (display: block, which images aren't by default) with a fixed width and margin: 0 auto centers itself horizontally within its containing block — this is the oldest and simplest centering technique and still the right tool for basic cases.
- Accepted values:
- margin: 0 auto — centers a block horizontally
- margin-inline: auto — logical equivalent
- display: block — required for images, which are inline by default
- width — must be less than the container's width
Example: Horizontal Centering with margin: auto
<style>
img {
display: block;
width: 150px;
margin: 0 auto;
}
</style>
<img src="https://placehold.co/150x100" alt="Centered">
Centering with text-align
If an image is left as its default inline/inline-block display, wrapping it in a block-level parent with text-align: center centers it horizontally the same way it would center a line of text, without needing to touch the image's own display or margin.
- Accepted values:
- text-align: center — centers inline content
- text-align: left | right — other alignments
- text-align: start | end — alignment that follows writing direction
- text-align: justify — stretches lines
- note: applies to the parent of an inline image, not the image
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Centering with text-align
<style>
.wrap {
text-align: center;
}
</style>
<div class="wrap">
<img src="https://placehold.co/150x100" alt="Centered like a line of text">
</div>
Flexbox Centering (Both Axes)
Setting display: flex on a container along with justify-content: center and align-items: center centers its image child on both axes at once, and works regardless of whether the image's or container's exact dimensions are known ahead of time.
- Accepted values:
- display: flex — makes a flex container
- justify-content: center — main axis centering
- align-items: center — cross axis centering
- height — the container needs a height to center vertically
Example: Flexbox Centering (Both Axes)
<style>
.wrap {
display: flex;
justify-content: center;
align-items: center;
height: 150px;
}
</style>
<div class="wrap">
<img src="https://placehold.co/100x80" alt="Centered on both axes">
</div>
Absolute Positioning with transform
Positioning an image absolutely at top: 50%; left: 50% moves its top-left corner to the container's center, and adding transform: translate(-50%, -50%) then shifts it back by exactly half its own size — perfectly centering it without flexbox or grid.
- Accepted values:
- position: absolute — takes it out of flow
- top: 50%; left: 50% — moves the corner to the center
- transform: translate(-50%, -50%) — pulls back by half its own size
- position: relative — on the parent as the reference
Example: Absolute Positioning with transform
<style>
.wrap {
position: relative;
height: 150px;
}
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="wrap">
<img src="https://placehold.co/100x80" alt="Centered without flexbox">
</div>
Grid Centering
A single-cell CSS Grid container with place-items: center centers its image child on both axes in one declaration, functionally equivalent to the flexbox approach but often considered more concise for this specific single-item case.
- Accepted values:
- display: grid — makes a grid container
- place-items: center — centers on both axes
- place-content: center — centers the whole grid
- min-height — needed to center vertically
Example: Grid Centering
<style>
.wrap {
display: grid;
place-items: center;
height: 150px;
}
</style>
<div class="wrap">
<img src="https://placehold.co/100x80" alt="Centered in one declaration">
</div>
- Using
margin: autoon an inline image withoutdisplay: block, so it does not center. - Using
text-align: centeron the image instead of on its parent. - Setting
margin: 0 autoon an image that is stilldisplay: inline, so it does not center.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties