# How to Center a Div in CSS (Horizontally and Vertically)

DevFeed: [How to Center a Div in CSS (Horizontally and Vertically)](<https://devfeed.tech/articles/how-to-center-a-div-in-css-horizontally-and-vertically-143994.md>)

Original publisher: [Read original article](<https://markodenic.com/center-a-div/>)

Author: Marko

Published: 2026-11-19T09:00:00Z

Content type: article

Language: en

Sources: [Marko Denic Blog](<https://devfeed.tech/sources/marko-denic-blog.md>)

Topics: [CSS](<https://devfeed.tech/topics/css.md>)

Tags: [absolute](<https://devfeed.tech/tags/absolute.md>), [align](<https://devfeed.tech/tags/align.md>), [alignment](<https://devfeed.tech/tags/alignment.md>), [axis](<https://devfeed.tech/tags/axis.md>), [block](<https://devfeed.tech/tags/block.md>), [browsers](<https://devfeed.tech/tags/browsers.md>), [button](<https://devfeed.tech/tags/button.md>), [centering](<https://devfeed.tech/tags/centering.md>), [chrome](<https://devfeed.tech/tags/chrome.md>), [css](<https://devfeed.tech/tags/css.md>), [css-flexbox](<https://devfeed.tech/tags/css-flexbox.md>), [fail](<https://devfeed.tech/tags/fail.md>), [flexbox](<https://devfeed.tech/tags/flexbox.md>), [grid](<https://devfeed.tech/tags/grid.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [images](<https://devfeed.tech/tags/images.md>), [margin](<https://devfeed.tech/tags/margin.md>), [positioning](<https://devfeed.tech/tags/positioning.md>), [text](<https://devfeed.tech/tags/text.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>), [vertical](<https://devfeed.tech/tags/vertical.md>)

## AI overview

To center a div horizontally and vertically, set its parent to `display: grid; place-items: center` and give it height; flexbox with `justify-content` and `align-items` also works. For horizontal centering in normal flow, give a block a width or max-width and use `margin-inline: auto`. In flex or grid, auto margins center in both directions and keep oversized children scrollable, unlike `align-items: center`. Absolute positioning suits overlays: translate from the midpoint for unknown dimensions, or use `inset: 0` and auto margins when dimensions are known. `align-content: center` vertically centers regular block content in the browser versions listed in the text. `text-align` centers inline content, not block boxes; `vertical-align` does not affect a regular div. The supplied text also shows two ways to center images: parent text alignment or making the image a block with auto side margins.

## Source excerpt

Every way to center a div in CSS: flexbox, grid, margin auto, absolute positioning, text and images, plus why vertical-align and text-align often fail.