bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Vertical Align

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind CSS Vertical Align?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___: flex;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Center Align with Grid
Center Align with Flexbox
Vertical Centering

Vertical Centering

Vertical centering in CSS can be achieved using modern layout techniques like Flexbox and Grid, or using positioning with transforms.

Center Align with Flexbox

With CSS flexbox you can center elements, both horizontally and vertically, within a flex container.

A flex container with both the justify-content and the align-items properties set to center will align the item(s) in the center (in both axis):

Example

Formatted code
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid
  green;
}

Live preview

Center Align with Grid

With CSS grid you can center elements, both horizontally and vertically, within a grid container.

A grid container with the place-items property set to center, will align the item(s) in the center (in both axis).

Example

Formatted code
.center {
  display: grid;
  place-items: center;
  height: 200px;
  border: 3px solid
  green;
}

Live preview

Center Align with position and transform

If you deal with elements of unknown or dynamic dimensions, it is a common technique to use position: absolute; combined with transform: translate(); to center an element:

I am vertically and horizontally centered.

Example

Formatted code
.container p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Live preview

Tip

You will learn more about the transform property in our 2D Transforms Chapter .

Previous

CSS Horizontal Align

Next

CSS Combinators