The Complete Guide to Centering a Div

Every developer inevitably finds that centering a div isn't as obvious as you'd expect. Centering what's inside a div horizontally is easy but then things tend to get a bit sticky. When you get to centering a div vertically, you can end up in a world of CSS hurt.

The aim of this article is to show how, with a few CSS tricks, any div can be centered, horizontally and/or vertically. And within the page, a div or any element.

Centering a div in a page, basic

This method works with just about every browser, ever.
CSS
HTML
Example

See the Pen Centering a div in a page, basic by Steve Pear () on CodePen.

The value auto in the margin property sets the left and right margins to the available space within the page. The thing to remember is your centered div must have a width property.

centering a div within a div, old-school

This method works with just about every browser.
CSS
HTML
Example

See the Pen Centering a div within a div, old-school by Steve Pear () on CodePen.

The margin auto trick strikes again. The outer div can be disorderly, but the inner div must have a width property.

Centering a div within a div with inline-block

With this method the inner div doesn't require a set width. It works with all browsers.
CSS
HTML
Example

See the Pen Centering a div within a div with inline-block by Steve Pear () on CodePen.

The text-align property only works on inline elements. The inline-block value displays the inner div as an inline element as well as a block, so the text-align property in the outer div centers the inner div.

Centering a div within a div, horizontally and vertically

This uses the margin auto trick to center a div both horizontally and vertically. It works with every browser.
CSS
HTML
Example

See the Pen Centering a div within a div, horizontally and vertically by Steve Pear () on CodePen.

The inner div must have a width and height property. This doesn't work if the outer div has a fixed height.

Vertical center in a div with transform

This uses transform to center any content in a div vertically. This method uses an HTML empty element as a container, in this case p. You can wrap a lot in an empty element.

Transform is supported by Chrome 63+, IE11, Microsoft Edge 16+, Firefox 57+, Safari 11+, iOS Safari 11.2+ and Chrome for Android 62+.

CSS
HTML
Example

See the Pen Vertical center in a div with transform by Tipue () on CodePen.

The perspective value reduces blurring on high definition displays. You can support older browser releases using vendor prefixes for transform, such as -webkit-transform and -ms-transform. The later will go as low as IE9.

Centering a div at the bottom of a page

This uses margin auto and an absolute-positioned outer div. It works with all browsers.
CSS
HTML
Example

See the Pen Centering a div at the bottom of a page by Steve Pear () on CodePen.

The inner div must have a width property. The gap from the bottom of the page is set in the outer div's bottom property. You can also center a div at the top of a page, replacing the bottom property with top.

Centering a div in a page, horizontally and vertically

This uses margin auto again and an absolute-positioned div. It works with all browsers.
CSS
Example

See the Pen Centering a div in a page, horizontally and vertically by Steve Pear () on CodePen.

The div must have a width and height property.

Centering a div in a page, responsive

The width of the div is responsive, in that it responds to the size of the viewport. It works with just about every browser.
CSS
Example

See the Pen Centering a div in a page, responsive by Steve Pear () on CodePen.

Your centered div must have a max-width property.

centering a div within a div, inner div responsive

The inner div is responsive. Works with all browsers.
CSS
HTML
Example

See the Pen Centering a div within a div, inner div responsive by Steve Pear () on CodePen.

The inner div must have a max-width property.

Centering two responsive divs, side by side

Two divs side by side, and both responsive. Works with every browser.
CSS
HTML
Example

See the Pen Centering two responsive divs, side by side by Steve Pear () on CodePen.

Simply a couple of inline-block elements within a centered container. This also uses CSS media queries. When the screen size is less than 600 pixels, the max-width property of both left and right divs is set to 100%.

Flexbox, div centered

This centers a div both horizontally and vertically using Flexbox, the CSS3 Flexible Box Layout Module.

Flexbox is supported by Chrome 63+, IE11, Microsoft Edge 16+, Firefox 57+, Safari 11+, iOS Safari 11.2+ and Chrome for Android 62+.

CSS
HTML
Example

See the Pen Flexbox, div centered by Steve Pear () on CodePen.

The height property in the container can be anything, as long as it's larger than the centered div. In this example we're using vh, which is the height of the browser window, the viewport, rather than the height of the page. For more on viewport values and their uses see Using vw and vh.

Centering a dynamic div, horizontally and vertically

This method uses the display property with a table value, making it behave like a table element, centering a div both horizontally and vertically. The centered div can have dynamic content, that is, any height or width. It can be responsive.

Table display is supported by Chrome 63+, IE11, Microsoft Edge 16+, Firefox 57+, Safari 11+, iOS Safari 11.2+ and Chrome for Android 62+.

CSS
HTML
Example

See the Pen Centering a dynamic div, horizontally and vertically by Tipue () on CodePen.

The width and height properties in the centered div can be anything.