Skip to content
Justice Tech School

Centering Things

Text: text-align: center. A block with a width: margin: 0 auto. Anything, both directions: make the parent a flex container with justify-content: center and align-items: center, or a grid container with place-items: center.

Vertical centring needs the parent to have height, such as min-height: 100vh or an explicit value. Without it there is nothing to centre inside.

Example
<style>
  .stage { height: 160px; display: grid; place-items: center; background: #1e293b; }
  .dot { width: 70px; height: 70px; background: #facc15; border-radius: 50%; }
  .fixed { width: 200px; margin: 12px auto; padding: 8px; background: #bfdbfe; text-align: center; }
</style>
<div class="stage"><div class="dot"></div></div>
<div class="fixed">Centered by auto margins</div>

Exercise

Centre the child both ways in a grid.

.stage {
  display: grid;
  place-items: ;
}

Quiz Test

What does a parent need for vertical centring to be visible?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.