In this recipe you will see how to center one box inside another. Centering both horizontally and vertically was difficult before flexbox, with the Box Alignment properties it is now straightforward.
[[File:../../../../../../media.prod.mdn.mozit.cloud/attachments/2018/09/23/16182/5e004ea5b85bdc0a0aa6b612809154ee/cookbook-center.png|an element centered inside a larger box]]
Requirements
To place an item into the center of another box horizontally and vertically.
Recipe
Choices made
To center one box inside another we make the containing box a flex container. Then set align-items
to center to perform centering on the block axis, and justify-content
to center to perform centering on the inline axis.
In the future we may be able to center elements without needing to turn the parent into a flex container, as the Box Alignment properties used here are specified to apply to block layout too. However, support is currently limited for box alignment properties on block layout, so currently centering using Flexbox is the most robust way to achieve this.
Browser compatibility
The various layout methods have different browser support. See the charts below for details on basic support for the properties used.
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
align-items
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Supported in Flex Layout |
Chrome Full support 52 Full support 52 Partial support 29 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support 21 Prefixed' Implemented with the vendor prefix: -webkit- |
Edge Full support 12 Full support 12 Full support 12 Prefixed' Implemented with the vendor prefix: -webkit- |
Firefox Full support 20 Full support 20 Notes' Multi-line flexbox has been supported since Firefox 28. Full support 49 Prefixed' Implemented with the vendor prefix: -webkit- No support 18 — 20 Disabled' From version 18 until version 20 (exclusive): this feature is behind the Prefixed' Implemented with the vendor prefix: -webkit-
Disabled' From version 48: this feature is behind the |
IE Full support 11 Full support 11 Notes' In Internet Explorer 10 and 11, if column flex items have |
Opera
Full support 12.1 |
Safari Full support 9 Full support 9 Full support 7 Prefixed' Implemented with the vendor prefix: -webkit- |
WebView Android Full support 52 Full support 52 Partial support 4.4 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support ≤37 Prefixed' Implemented with the vendor prefix: -webkit- |
Chrome Android Full support 52 Full support 52 Partial support 29 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support 25 Prefixed' Implemented with the vendor prefix: -webkit- |
Firefox Android Full support 20 Full support 20 Notes' Multi-line flexbox has been supported since Firefox 28. Full support 49 Prefixed' Implemented with the vendor prefix: -webkit- No support 18 — 20 Disabled' From version 18 until version 20 (exclusive): this feature is behind the Prefixed' Implemented with the vendor prefix: -webkit-
Disabled' From version 48: this feature is behind the |
Opera Android
Full support 12.1 |
Safari iOS Full support 9 Full support 9 Full support 7 Prefixed' Implemented with the vendor prefix: -webkit- |
Samsung Internet Android Full support 6.0 Full support 6.0 Partial support 2.0 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Samsung Internet 6.0. Full support 1.5 Prefixed' Implemented with the vendor prefix: -webkit- |
Supported in Grid Layout | Chrome
Full support 57 |
Edge
Full support 16 |
Firefox
Full support 52 |
IE
No support No |
Opera
Full support 44 |
Safari
Full support 10.1 |
WebView Android
Full support 57 |
Chrome Android
Full support 52 |
Firefox Android
Full support 52 |
Opera Android
Full support 43 |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 6.2 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.'
- See implementation notes.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
- Requires a vendor prefix or different name for use.'
- Requires a vendor prefix or different name for use.
justify-content
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Supported in Flex Layout |
Chrome Full support 52 Full support 52 Partial support 29 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support 21 Prefixed' Implemented with the vendor prefix: -webkit- |
Edge Full support 12 Full support 12 Full support 12 Prefixed' Implemented with the vendor prefix: -webkit- |
Firefox Full support 20 Full support 20 Notes' Before Firefox 27, Firefox supported only single-line flexbox. No support 18 — 20 Disabled' From version 18 until version 20 (exclusive): this feature is behind the Prefixed' Implemented with the vendor prefix: -webkit- Full support 48 Prefixed' Implemented with the vendor prefix: -webkit-
Disabled' From version 48: this feature is behind the |
IE
Full support 11 |
Opera
Full support 12.1 |
Safari Full support 9 Full support 9 Full support 6.1 Prefixed' Implemented with the vendor prefix: -webkit- |
WebView Android Full support 52 Full support 52 Partial support 4.4 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support ≤37 Prefixed' Implemented with the vendor prefix: -webkit- |
Chrome Android Full support 52 Full support 52 Partial support 29 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support 25 Prefixed' Implemented with the vendor prefix: -webkit- |
Firefox Android Full support 20 Full support 20 Notes' Before Firefox 27, Firefox supported only single-line flexbox. No support 18 — 20 Disabled' From version 18 until version 20 (exclusive): this feature is behind the Prefixed' Implemented with the vendor prefix: -webkit- Full support 48 Prefixed' Implemented with the vendor prefix: -webkit-
Disabled' From version 48: this feature is behind the |
Opera Android
Full support 12.1 |
Safari iOS Full support 9 Full support 9 Full support 6.1 Prefixed' Implemented with the vendor prefix: -webkit- |
Samsung Internet Android Full support 6.0 Full support 6.0 Partial support 2.0 Notes' Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52. Full support 1.5 Prefixed' Implemented with the vendor prefix: -webkit- |
Supported in Grid Layout | Chrome
Full support 57 |
Edge
Full support 16 |
Firefox
Full support 52 |
IE
No support No |
Opera
Full support 44 |
Safari
Full support 10.1 |
WebView Android
Full support 57 |
Chrome Android
Full support 52 |
Firefox Android
Full support 52 |
Opera Android
Full support 43 |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 6.2 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.'
- See implementation notes.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
- Requires a vendor prefix or different name for use.'
- Requires a vendor prefix or different name for use.
Resources on MDN
Center an element by Mozilla Contributors is licensed under CC-BY-SA 2.5.