Emmanuel Akinfulubi

HTML and CSSPractical I

Center a div horizontally and vertically in CSS

Beginner · 3 min read

"How do I center a div?" is probably the most famous question in CSS. For years the answer involved strange tricks with margins and transforms. Today it takes three lines, and once you understand them you can center anything: a login box, a loading spinner, an icon inside a button.

In this tutorial you will center a box inside its parent, both left to right and top to bottom, using flexbox. You need to know basic HTML and how to write a CSS rule. Nothing else.

The setup

You need two elements: a parent (we will call it the frame) and a child (the box) inside it.

index.html

<div class="frame">
  <div class="box">Centred</div>
</div>

The goal is for the box to sit exactly in the middle of the frame.

Here is the exercise. Try it yourself first if you like, or follow the steps below.

1 Centre a box inside its parent

easy

Put a child element exactly in the middle of its parent, both across and down. Use the modern approach rather than the old margin tricks.

Stuck? Three hints, smallest first
  1. 1. Three declarations on the parent do it: display: flex, align-items: center, justify-content: center.
  2. 2. align-items works down the cross axis, justify-content along the main axis. With the default row direction that means vertical and horizontal respectively.
  3. 3. The parent needs a height, or there is no vertical space to centre within.

Step 1: make the parent a flex container

Centering is a job for the parent, not the child. Add display: flex to the frame:

Step 1

.frame {
  display: flex;
}

You won't see much change yet. The box shrinks to the width of its text and sits on the left. That's fine: display: flex unlocks two properties that decide where the children go, and the next two steps use them.

Step 2: center from left to right

justify-content places the children along the direction the flex container runs in. By default that direction is a row, from left to right.

Step 2

.frame {
  display: flex;
  justify-content: center;
}

The box now sits in the middle, left to right.

Step 3: center from top to bottom

align-items places the children in the other direction. For a row, that is top to bottom.

Step 3

.frame {
  display: flex;
  justify-content: center;
  align-items: center;
}

If you try this and the box doesn't move down, you haven't done anything wrong. There is one piece missing.

Step 4: give the parent a height

A div is only as tall as what is inside it. If the frame is exactly as tall as the box, there is no space above or below to center in. Give the frame a height:

Step 4

.frame {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 140px;
}

Now the box is centered both ways. For a layout that fills the screen, use height: 100vh instead: it makes the frame as tall as the browser window.

The full solution

Here is the complete HTML and CSS. The frame has a dashed border and the box has a background color, so you can see both. The result is shown next to the code, and the checks underneath confirm each part of the answer.

index.html

<div class="frame">
  <div class="box">Centred</div>
</div>

style.css

.frame {
  display: flex;
  align-items: center;      /* down the cross axis */
  justify-content: center;  /* along the main axis */
  height: 140px;
  border: 2px dashed #3055d8;
  border-radius: 8px;
}

.box {
  padding: 12px 20px;
  background: #3055d8;
  color: #fff;
  border-radius: 6px;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
7 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 1

test('the parent is a flex container', declares(css, 'display', 'flex'), true);
test('centred on the cross axis', declares(css, 'align-items', 'center'), true);
test('centred on the main axis', declares(css, 'justify-content', 'center'), true);
test('the parent has a height to centre within', declares(css, 'height', '140px'), true);
test('there is a child to centre', hasTag(html, 'div'), true);
test('exactly two elements', countTag(html, 'div'), 2);
test('no float or absolute positioning was needed', declares(css, 'position', 'absolute'), false);

Output


      

Two declarations plus a height is the whole answer, and it replaces a decade of margin: 0 auto plus transform: translateY(-50%) workarounds. The detail people miss is the height: with none, the parent collapses to fit the child and there is no vertical space for align-items to work in, so it looks like the property did nothing.

Common mistakes

  • Putting the properties on the child. justify-content and align-items go on the parent. On the box they do nothing.
  • No height on the parent. This is the most common reason "align-items: center doesn't work".
  • Mixing up the two. In a row, justify-content is left to right and align-items is top to bottom. Switch to flex-direction: column and they swap.

Try it yourself

  1. Center the box in the whole browser window: give the frame height: 100vh and remove the border.
  2. Put three boxes inside the frame. What happens? Add gap: 12px to the frame to space them out.
  3. Do it with grid instead: display: grid; place-items: center; on the frame, plus the height. How many lines did that take?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.