CSS Layout Centering Techniques

CSS Centering Techniques

Centering elements with CSS sometimes can be tricky. There are plenty of techniques that you could use but what technique you should use depends on the element and the content. Some questions that you might ask yourself when trying to center an element could be:

  • Is it an inline or a block element?
  • Is it just one line of text or multiple lines?
  • Does it have a fixed width and height or its size is unknown?

These are likely the most common questions to ask yourself when you want to center an element.

I’m gonna divide the techniques by horizontal, vertical and both.
Let’s get to the code.

Horizontal Centering

Text-align

That’s the simplest horizontal centering technique that you could use for text and inline-* elements.

p { text-align: center; }

In case you want to center an inline element you should apply it on its parent and not directly on that element:

.parent { text-align: center; }
<div class="parent">
 <strong>I'm Centered</strong>
</div>

In case we have an inline-block element we could make it full width and apply text-align: center; on it directly instead of it’s parent.

strong {
 display: inline-block; 
 width: 100%;
 text-align: center;
}

Pen:

See the Pen Centering with Text -Align. by Alan Fall (@AlanFall) on CodePen.0

Margin Auto

This is the most common technique for centering horizontally block elements, it’s widely used by many frameworks like Bootstrap. You just set the left and rightmargin to auto, make sure you have also set a width:

.block-box { 
 width: 600px;
 margin: auto;
}

Pen:

See the Pen Horizontal Centering Using Margin Auto by Alan Fall (@AlanFall) on CodePen.0

Absolute Position

A common technique for both horizontal and vertical centering is using an absolute positioned element as child of a relative parent.

What we do is basically position our child element left by 50% and we shift it back by half of its size using a negative 50% translateX in order to get it centered.

.parent { position: relative; }
.centered-element {
 position: absolute;
 left: 50%;
 transform: translateX(-50%);
}
<div class="parent">
 <div class="centered-element">I'm Centered!</div>
</div>

That’s also a nice way to center elements of unknown width (and height).

An alternative to that, -if you have elements of known width- would be to use a negative margin-left instead of translateX:

.centered-element { 
 position: absolute;
 width: 600px;
 left: 50%;
 margin-left: -300px; // Shift it back by half of it's size.
}

Pen:

See the Pen Horizontal Centering with Absolute Position by Alan Fall (@AlanFall) on CodePen.0

Flex

If you don’t care about support of older browsers, a nice way to horizontally(and vertically) center an element or a bunch of elements is by using flex.

.parent { 
 display: flex; 
 justify-content: center;
 height: 100vh;
}

Note: Make sure you haven’t set the flex-direction property to column otherwise you’ll have the opposite effect (vertical center).

Pen:

See the Pen Horizontal Centering using Flex by Alan Fall (@AlanFall) on CodePen.0

Vertical Centering

Absolute Position

Again, we could use the same absolute positioning technique we used previously but instead of setting the left position to 50% we set the top position to 50% and we shift it up using a negative 50% translateY:

.parent { position: relative; }
.centered-element {
 position: absolute;
 top: 50%;
 transform: translateY(-50%);
}

Pen:

See the Pen Vertical Centering with Absolute Position by Alan Fall (@AlanFall) on CodePen.0

Table Cell

A nice way for vertical centering would be to use vertical-align: middle. Of course this is tricky because you have to apply it on elements of a parent that is set to display: table and children to display: table-cell.

.parent { 
 display: table;
 height: 100vh; 
}

.centered-element {
 display: table-cell;
 vertical-align: middle;
}

Note: Make sure that the parent element has height otherwise this won’t work.

Pen:

See the Pen Vertical Centering with Table-Cell by Alan Fall (@AlanFall) on CodePen.0

Line-height

This technique is okay if you want to center single words and not whole paragraphs or lines of text. If you have a single word inside an element you could vertically center it by setting a fixed height on that element and a line-height equal the height of the element:

.box { 
 width: 600px;
 height: 600px;
 line-height: 600px;
}
<div class="box">
 <strong>I'm Centered</strong>
</div>

Pen:

See the Pen Vertical Centering with Line-Height by Alan Fall (@AlanFall) on CodePen.0

The Ghost Element

In this technique, we have two inline-block elements: a container with a “ghost” pseudo element set to full height and a child that is set to vertical-align: middle; This way the child element aligns with the ghost element -which has also vertical-align: middle

.container { font-size: 0; }
.container::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.container strong {
 display: inline-block;
 vertical-align: middle;
 font-size: 1rem;
}

Note: The ghost element creates an empty space usually on the left side so make sure to add a negative margin (margin-right: -0.25em) or a font-size: 0 on the parent container and a font-size: 1rem on the child.

Pen:

See the Pen Vertical Centering using a Ghost Element by Alan Fall (@AlanFall) on CodePen.0

Flex

We’ve seen how to align horizontally with flex. We can do the same for vertical centering by using the align-items property.

.parent { 
 display: flex; 
 align-items: center;
 height: 100vh;
}

Note: again, make sure you haven’t set the flex-direction to row, otherwise you’ll have the opposite effect. (horizontal centering).

Pen:

See the Pen Vertical Centering using Flex by Alan Fall (@AlanFall) on CodePen.0

Horizontal & Vertical Centering

We can combine all the techniques we used previously to center in both axis.

From all the techniques we used, the absolute positioning and the flex technique will give us both Horizontal and Vertical centering.

Absolute Position

We position the element to 50% in both top and left and we shift back in both positions by using a negative translate of 50%:

.centered-element {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
}

Pen:

See the Pen Horizontal & Vertical Centering with Absolute Position by Alan Fall (@AlanFall) on CodePen.0

Flex

Again, using the same flex technique we use both align-items and justify-content set to center.

.parent { 
 display: flex; 
 align-items: center;
 justify-content: center;
 height: 100vh;
}

Pen:

See the Pen Horizontal & Vertical Centering using Flex by Alan Fall (@AlanFall) on CodePen.0

Have something to add? Feel free to throw it on the comments section.

Thanks to PVGR for the useful info and corrections.

One Comment

Good article mate.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow me on instagram