DEV Community

Cover image for CSS Layout Centering Techniques
Alexander Antoniades
Alexander Antoniades

Posted on • Edited on

CSS Layout 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
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

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

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

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

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

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

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

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

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

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

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

Thanks to PVGR for the useful info and corrections.

Top comments (7)

Collapse
 
tigerwood2006 profile image
JW

nice article. very good summary on the topic.

Collapse
 
alexantoniades profile image
Alexander Antoniades

thanks JW! Hope it helps!

Collapse
 
ibrahimwehbi profile image
Ibrahim Wehbi

Such an amazing article. Very well crafted!

Collapse
 
alexantoniades profile image
Alexander Antoniades

thank you ImNah!

Collapse
 
amjadmh73 profile image
Amjad Abujamous

Great article. Will be using this as a reference.

Collapse
 
k33fus profile image
Keith Greer

Great article thank you very much, a real help.

Collapse
 
alexantoniades profile image
Alexander Antoniades

thanks k33fus! I'm glad for that!