You will center an image horizontally and vertically inside a container using CSS Grid, while keeping the image responsive and preventing overflow.
What it is
Image centering is the practice of placing an image in the middle of a parent element along both axes. The most compact modern method is to make the parent a grid container and use place-items: center. This tells the browser to center each grid item inside its grid area. Related terms include horizontal centering, vertical centering, flexbox centering, and absolute positioning centering.
Why it matters
- It creates balanced hero sections, cards, galleries, and empty states.
- It keeps logos and icons visually stable when text or layout changes.
- It avoids fragile hacks such as fixed margins or table-cell tricks.
- It works well with responsive images when combined with
max-widthandmax-height. - It reduces layout surprises when images have different aspect ratios.
Syntax or steps
Use a wrapper element around the image. Give the wrapper display: grid and place-items: center. Give the wrapper a height or min-height so vertical centering has space to work. Then constrain the image so it does not overflow the wrapper.
Example
<div class="wrap">
<img src="photo.jpg" alt="A centered photo">
</div>
.wrap {
display: grid;
place-items: center;
min-height: 100vh;
padding: 1rem;
box-sizing: border-box;
}
.wrap img {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
The HTML places the image inside a wrapper. The CSS makes the wrapper a one-cell grid. place-items: center centers the image horizontally and vertically. min-height: 100vh gives the wrapper enough vertical space. max-width: 100% keeps the image from becoming wider than the wrapper. max-height: 100% keeps it from becoming taller than the wrapper. object-fit: contain preserves the image aspect ratio when both constraints apply.
Common mistakes
- Forgetting container height. Vertical centering needs space. Add
height,min-height, or a parent with a defined height. - Letting the image overflow. A large image can push the layout. Add
max-width: 100%andmax-height: 100%. - Using
margin: autoalone. Auto margins can center a block horizontally, but they do not reliably center vertically without extra layout rules. - Applying centering to the wrong element.
place-itemsbelongs on the grid container, not the image.
When to use it
Grid centering is best when you want the shortest, clearest rule for a single item. Flexbox is also excellent, especially when you need to center multiple items in a row or column. Absolute positioning is useful when the image must be centered inside a specific positioned ancestor without affecting document flow.
| Method | Best for | Watch out for |
|---|---|---|
display: grid; place-items: center; | One image or one item in a container | Container needs height |
display: flex; justify-content: center; align-items: center; | Multiple items or simple row/column centering | More properties than grid shorthand |
position: absolute; inset: 0; margin: auto; | Overlaying an image inside a positioned parent | Parent needs position: relative |
Practice
Guided exercise: Center a logo inside a header that is 80px tall. Use display: grid and place-items: center on the header, and give the logo max-height: 100%.
Challenge: Center a photo inside a card that is 300px tall and 200px wide, without cropping the photo. Expected result: the photo stays fully visible and centered. Hint: use object-fit: contain and constrain both dimensions.
Quick check
Why does place-items: center center the image?
Because it sets the alignment of grid items inside their grid area, centering them on both the block and inline axes.
Summary
Centering an image horizontally and vertically is easiest when the parent becomes a grid container and the image is constrained to fit. The transferable idea is to separate the layout job from the image job: the wrapper controls placement, while the image controls size and aspect ratio.