- Web Style Sheets CSS tips & tricks
- Centering a block or image
- Centering vertically
- Centering vertically in CSS level 3
- Centering vertically and horizontally in CSS level 3
- Centering in the viewport in CSS level 3
- Nicely centered
- Site navigation
- How to Center Anything with CSS — Align a Div, Text, and More
- Here’s an Interactive Scrim Showing How to Center Anything with CSS
- How to Center Horizontally
- How to Center Text with the CSS Text-Align Center Property
- How to Center a Div with CSS Margin Auto
- How to Center a Div Horizontally with Flexbox
- How to Center Vertically
- How to Center a Div Vertically with CSS Absolute Positioning and Negative Margins
- How to Center a Div Vertically with Transform and Translate
- How to Center a Div Vertically with Flexbox
- How to Center Both Vertically and Horizontally
- How to Center a Div Vertically and Horizontally with CSS Absolute Positioning and Negative Margins
- How to Center a Div Vertically and Horizontally with Transform and Translate
- How to Center a Div Vertically and Horizontally with Flexbox
Web Style Sheets CSS tips & tricks
The most common and (therefore) easiest type of centering is that of lines of text in a paragraph or in a heading. CSS has the property ‘text-align’ for that:
renders each line in a P or in a H2 centered between its margins, like this:
The lines in this paragraph are all centered between the paragraph’s margins, thanks to the value ‘center’ of the CSS property ‘text-align’.
Centering a block or image
Sometimes it is not the text that needs to be centered, but the block as a whole. Or, phrased differently: we want the left and right margin to be equal. The way to do that is to set the margins to ‘auto’. This is normally used with a block of fixed width, because if the block itself is flexible, it will simply take up all the available width. Here is an example:
This rather narrow block of text is centered. Note that the lines inside the block are not centered (they are left-aligned), unlike in the earlier example.
This is also the way to center an image: make it into block of its own and apply the margin properties to it. For example:
The following image is centered:
Centering vertically
CSS level 2 doesn’t have a property for centering things vertically. There will probably be one in CSS level 3 (see below ). But even in CSS2 you can center blocks vertically, by combining a few properties. The trick is to specify that the outer block is to be formatted as a table cell, because the contents of a table cell can be centered vertically.
DIV.container < min-height: 10em; display: table-cell; vertical-align: middle >.This small paragraph.
This small paragraph is vertically centered.
Centering vertically in CSS level 3
CSS level 3 offers other possibilities. At this time (2014), a good way to center blocks vertically without using absolute positioning (which may cause overlapping text) is still under discussion. But if you know that overlapping text will not be a problem in your document, you can use the ‘transform’ property to center an absolutely positioned element. For example:
This paragraph is vertically centered.
For a document that looks like this:
the style sheet looks like this:
div.container3 < height: 10em; position: relative > /* 1 */ div.container3 p < margin: 0; position: absolute; /* 2 */ top: 50%; /* 3 */ transform: translate(0, -50%) > /* 4 */
- Make the container relatively positioned, which declares it to be a container for absolutely positioned elements.
- Make the element itself absolutely positioned.
- Place it halfway down the container with ‘top: 50%’. (Note that 50%’ here means 50% of the height of the container.)
- Use a translation to move the element up by half its own height. (The ‘50%’ in ‘translate(0, -50%)’ refers to the height of the element itself.)
Recently (since about 2015), another technique has also become available in several CSS implementations. It is based on the new ‘flex’ keyword for the ‘display’ property. This keyword is meant for use in graphical user interfaces (GUIs), but nothing stops you from using it in a document, if the document happens to have the right structure.
This paragraph is vertically centered.
the style sheet looks like this:
div.container5 < height: 10em; display: flex; align-items: center > div.container5 p
Centering vertically and horizontally in CSS level 3
We can extend both methods to center horizontally and vertically at the same time.
A side-effect of making the paragraph absolutely positioned is that it is then only as wide as it needs to be (unless we give it an explicit width, of course). In the example below, that’s precisely what we want: We center a paragraph with just one word (“Centered!”), so the width of the paragraph should be exactly the width of that word.
The yellow background is there to show that the paragraph is indeed only as wide as its contents. We assume the same mark-up as before:
The style sheet is similar to the previous example with respect to the vertical centering. But we now move the element halfway across the container as well, with ‘left: 50%’, and at the same time move it leftwards by half its own width in the ‘translate’ transformation:
div.container4 < height: 10em; position: relative >div.container4 p < margin: 0; background: yellow; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%) >
The next example below explains why the ‘margin-right: -50%’ is needed.
When the CSS formatter supports ‘flex’, it’s even easier:
div.container6 < height: 10em; display: flex; align-items: center; justify-content: center > div.container6 p
i.e., the only addition is the ‘justify-content: center’. Just like ‘align-items’ determines the vertical alignment of the container’s contents, ‘justify-content’ determines the horizontal alignment. (It’s actually a bit more complex, as their names suggest, but in a simple case that’s how it works.) A side-effect of ‘flex’ is that the child element, the P in this case, is automatically made as small as possible.
Centering in the viewport in CSS level 3
The default container for absolutely positioned elements is the viewport. (In case of a browser, that means the browser window). So centering an element in the viewport is very simple. Here is a complete example. (This example uses HTML5 syntax.)
Nicely centered
This text block is vertically centered.
Horizontally, too, if the window is wide enough.
You can see the result in a separate document.
The ‘margin-right: -50%’ is needed to compensate the ‘left: 50%’. The ‘left’ rule reduces the available width for the element by 50%. The renderer will thus try to make lines that are no longer than half the width of the container. By saying that the right margin of the element is further to the right by that same amount, the maximum line length is again the same as the container’s width.
Try resizing the window: You’ll see that each sentence is on one line when the window is wide enough. Only when the window is too narrow for the whole sentence will the sentence be broken over several lines. When you remove the ‘margin-right: -50%’ and resize the window again, you’ll see that the sentences will be broken already when the window is still twice as wide as the text lines.
Site navigation
Bert Bos, style activity lead
Copyright © 1994–2021 W3C ® Privacy policy
Created 5 May 2001;
Last updated Wed 06 Jan 2021 05:40:49 AM UTC
How to Center Anything with CSS — Align a Div, Text, and More
Kris Koishigawa
Centering things is one of the most difficult aspects of CSS.
The methods themselves usually aren’t difficult to understand. Instead, it’s more due to the fact that there are so many ways to center things.
The method you use can vary depending on the HTML element you’re trying to center, or whether you’re centering it horizontally or vertically.
In this tutorial, we’ll go over how to center different elements horizontally, vertically, and both vertically and horizontally.
Here’s an Interactive Scrim Showing How to Center Anything with CSS
How to Center Horizontally
Centering elements horizontally is generally easier than centering them vertically. Here are some common elements you may want to center horizontally and different ways to do it.
How to Center Text with the CSS Text-Align Center Property
To center text or links horizontally, just use the text-align property with the value center :
How to Center a Div with CSS Margin Auto
Use the shorthand margin property with the value 0 auto to center block-level elements like a div horizontally:
How to Center a Div Horizontally with Flexbox
Flexbox is the most modern way to center things on the page, and makes designing responsive layouts much easier than it used to be. However, it’s not fully supported in some legacy browsers like Internet Explorer.
To center an element horizontally with Flexbox, just apply display: flex and justify-content: center to the parent element:
How to Center Vertically
Centering elements vertically without modern methods like Flexbox can be a real chore. Here we’ll go over some of the older methods to center things vertically first, then show you how to do it with Flexbox.
How to Center a Div Vertically with CSS Absolute Positioning and Negative Margins
For a long time this was the go-to way to center things vertically. For this method you must know the height of the element you want to center.
First, set the position property of the parent element to relative .
Then for the child element, set the position property to absolute and top to 50% :
But that really just vertically centers the top edge of the child element.
To truly center the child element, set the margin-top property to -(half the child element’s height) :
How to Center a Div Vertically with Transform and Translate
If you don’t know the height of the element you want to center (or even if you do), this method is a nifty trick.
This method is very similar to the negative margins method above. Set the position property of the parent element to relative .
For the child element, set the position property to absolute and set top to 50% . Now instead of using a negative margin to truly center the child element, just use transform: translate(0, -50%) :
Note that translate(0, -50%) is shorthand for translateX(0) and translateY(-50%) . You could also write transform: translateY(-50%) to center the child element vertically.
How to Center a Div Vertically with Flexbox
Like centering things horizontally, Flexbox makes it super easy to center things vertically.
To center an element vertically, apply display: flex and align-items: center to the parent element:
How to Center Both Vertically and Horizontally
How to Center a Div Vertically and Horizontally with CSS Absolute Positioning and Negative Margins
This is very similar to the method above to center an element vertically. Like last time, you must know the width and height of the element you want to center.
Set the position property of the parent element to relative .
Then set the child’s position property to absolute , top to 50% , and left to 50% . This just centers the top left corner of the child element vertically and horizontally.
To truly center the child element, apply a negative top margin set to half the child element’s height, and a negative left margin set to half the child element’s width:
How to Center a Div Vertically and Horizontally with Transform and Translate
Use this method to center an element vertically and horizontally if you don’t know its exact dimensions and can’t use Flexbox.
First, set the position property of the parent element to relative .
Next, set the child element’s position property to absolute , top to 50% , and left to 50% .
Finally, use transform: translate(-50%, -50%) to truly center the child element:
How to Center a Div Vertically and Horizontally with Flexbox
Flexbox is the easiest way to center an element both vertically and horizontally.
This is really just a combination of the two previous Flexbox methods. To center the child element(s) horizontally and vertically, apply justify-content: center and align-items: center to the parent element:
That’s everything you need to know to center with the best of ’em. Now go forth and center all the things.