Centering in CSS: A Complete Guide ![rw-book-cover](https://css-tricks.com/wp-json/social-image-generator/v1/image/181375) ## Metadata - Author: [[Chris Coyier]] - Full Title: Centering in CSS: A Complete Guide - Category: #articles - Summary: Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn't that it's difficult to do, - URL: https://css-tricks.com/centering-css-complete-guide/ Centering things in CSS is the poster child of CSS complaining. *Why does it have to be so hard?* They jeer. I think the issue isn’t that it’s difficult to do, but in that there so many different ways of doing it, depending on the situation, it’s hard to know which to reach for. So let’s make it a decision tree and hopefully make it easier. I need to center… ##### Horizontally Is it inline or inline-\* elements (like text or links)? You can center inline elements horizontally, within a block-level parent element, with just: ``` .center { text-align: center; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/HulzB?height=250&theme-id=1&slug-hash=HulzB&default-tab=result) This will work for inline, inline-block, inline-table, inline-flex, etc. Is it a block level element? You can center a block-level element by giving it `margin-left` and `margin-right` of `auto` (and it has a set `width`, otherwise it would be full width and wouldn’t need centering). That’s often done with shorthand like this: ``` .center-me { margin: 0 auto; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/eszon?height=250&theme-id=1&slug-hash=eszon&default-tab=result) This will work no matter what the width of the block level element you’re centering, or the parent. Note that you can’t `float` an element to the center. [There is a trick though.](https://css-tricks.com/float-center/) Is there more than one block level element? If you have two or more block-level elements that need to be centered horizontally *in a row*, chances are you’d be better served making them a different `display` type. Here’s an example of making them `inline-block` and an example of flexbox: Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/ebing?height=500&theme-id=1&slug-hash=ebing&default-tab=result) Unless you mean you have multiple block level elements stacked on top of each other, in which case the auto margin technique is still fine: Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/haCGt?height=450&theme-id=1&slug-hash=haCGt&default-tab=result) ##### Vertically Vertical centering is a bit trickier in CSS. Is it inline or inline-\* elements (like text or links)? Is it a single line? Sometimes inline / text elements can appear vertically centered, just because there is equal padding above and below them. ``` .link { padding-top: 30px; padding-bottom: 30px; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/ldcwq?height=250&theme-id=1&slug-hash=ldcwq&default-tab=result) If padding isn’t an option for some reason, and you’re trying to center some text that you know will not wrap, there is a trick were making the `line-height` equal to the height will `center` the text. ``` .center-text-trick { height: 100px; line-height: 100px; white-space: nowrap; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/LxHmK?height=350&theme-id=1&slug-hash=LxHmK&default-tab=result) Is it multiple lines? Equal padding on top and bottom can give the centered effect for multiple lines of text too, but if that isn’t going to work, perhaps the element the text is in can be a table cell, either literally or made to behave like one with CSS. The [`vertical-align`](https://css-tricks.com/almanac/properties/v/vertical-align/) property handles this, in this case, unlike what it normally does which is handle the alignment of elements aligned on a row. ([More on that.](https://css-tricks.com/what-is-vertical-align/)) Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/ekoFx?height=450&theme-id=1&slug-hash=ekoFx&default-tab=result) If something table-like is out, perhaps you could use flexbox? A single flex-child can be made to center in a flex-parent pretty easily. ``` .flex-center-vertically { display: flex; justify-content: center; flex-direction: column; height: 400px; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/uHygv?height=350&theme-id=1&slug-hash=uHygv&default-tab=result) Remember that it’s only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height. If both of these techniques are out, you could employ the “ghost element” technique, in which a full-height pseudo-element is placed inside the container and the text is vertically aligned with that. ``` .ghost-center { position: relative; } .ghost-center::before { content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle; } .ghost-center p { display: inline-block; vertical-align: middle; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/ofwgD?height=450&theme-id=1&slug-hash=ofwgD&default-tab=result) Is it a block-level element? Do you know the height of the element? It’s fairly common to *not* know the height in web page layout, for lots of reasons: if the width changes, text reflow can change the height. Variance in the styling of text can change the height. Variance in the amount of text can change the height. Elements with a fixed aspect ratio, like images, can change height when resized. Etc. But if you do know the height, you can center vertically like: ``` .parent { position: relative; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */ } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/HiydJ?height=450&theme-id=1&slug-hash=HiydJ&default-tab=result) Is the element of unknown height? It’s still possible to center it by nudging it up half of it’s height after bumping it down halfway: ``` .parent { position: relative; } .child { position: absolute; top: 50%; transform: translateY(-50%); } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/lpema?height=450&theme-id=1&slug-hash=lpema&default-tab=result) Do you care if the element stretches the height of the container? If you don’t, you just need the content inside vertically centered, using tables or CSS display to make elements into tables can do the trick. Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/RmeWvQ?height=450&theme-id=1&slug-hash=RmeWvQ&default-tab=result) Can you use flexbox? No big surprise, this is a lot easier in [flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/). ``` .parent { display: flex; flex-direction: column; justify-content: center; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/FqDyi?height=450&theme-id=1&slug-hash=FqDyi&default-tab=result) You can also get centering in flexbox using `margin: auto;` on the child element. ##### Both Horizontally & Vertically You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps: Is the element of fixed width and height? Using negative margins equal to half of that width and height, after you’ve absolutely positioned it at 50% / 50% will center it with great cross-browser support: ``` .parent { position: relative; } .child { width: 300px; height: 100px; padding: 20px; position: absolute; top: 50%; left: 50%; margin: -70px 0 0 -170px; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/JGofm?height=450&theme-id=1&slug-hash=JGofm&default-tab=result) Is the element of unknown width and height? If you don’t know the width or height, you can use the transform property and a negative translate of 50% in both directions (it is based on the current width/height of the element) to center: ``` .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/lgFiq?height=450&theme-id=1&slug-hash=lgFiq&default-tab=result) Can you use flexbox? To center in both directions with flexbox, you need to use two centering properties: ``` .parent { display: flex; justify-content: center; align-items: center; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/msItD?height=450&theme-id=1&slug-hash=msItD&default-tab=result) Can you use grid? This is just a little trick (sent in by Lance Janssen) that will pretty much work for one element: ``` body, html { height: 100%; display: grid; } span { /* thing to center */ margin: auto; } ``` Some content could not be imported from the original document. [View content ↗](https://codepen.io/anon/embed/NvwpyK?height=450&theme-id=1&slug-hash=NvwpyK&default-tab=css,result) ##### Conclusion You can totally center things in CSS., Centering in CSS: A Complete Guide, Chris Coyier, 01gnmd09yf9k2tvtjbeam7m5bw.