On this page12 sections
  1. Learning Objectives
  2. 1. Centre Text
  3. 2. Centre a Block Horizontally
  4. 3. Centre an Image
  5. 4. Left and Right Alignment
  6. 5. Centre Vertically and Horizontally
  7. 6. Centre One Line of Text Vertically
  8. Quick Reference
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 018: Alignment & Centering

Category4: Layout Basics
Lecture018 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

"How do I centre a div?" may be the most-asked CSS question ever. The answer depends on what you're centring (text, a block, an image) and which way (across, down, or both). This lecture gathers every reliable technique in one place.


Learning Objectives

  • Centre text with text-align
  • Centre a block horizontally with margin: auto
  • Centre an image
  • Centre vertically and in both directions with Flexbox, Grid or positioning
  • Align things left and right

1. Centre Text

Example

html
<div style="text-align: center; border: 1px solid #ced4da; padding: 10px;">
  <h3>Centred heading</h3>
  <p>And a centred paragraph.</p>
</div>

text-align centres inline content (text, links, images, inline-blocks) inside a block.


2. Centre a Block Horizontally

Example

html
<div style="width: 60%; margin: 0 auto; background: #d0ebff; padding: 10px;">
  I have a width, and margin: 0 auto centres me.
</div>

The block needs a width (or max-width) smaller than its parent.


3. Centre an Image

An image is inline, so either give the parent text-align: center, or make the image a block:

Example

html
<img src="https://picsum.photos/id/1025/200/130" alt="A dog" width="200" height="130"
     style="display: block; margin: 0 auto;">

4. Left and Right Alignment

Example

html
<style>
  .bar { display: flex; justify-content: space-between; align-items: center; background: #f1f3f5; padding: 10px; }
  .right { margin-left: auto; }
</style>

<div class="bar"><strong>Logo</strong><span>Log in</span></div>
<br>
<div style="display: flex; background: #f1f3f5; padding: 10px;">
  <span>Left</span><span class="right">Pushed right with margin-left: auto</span>
</div>

Older code used float: right or position: absolute; right: 0. Flexbox is simpler.


5. Centre Vertically and Horizontally

With Flexbox

html
<div style="display: flex; justify-content: center; align-items: center; height: 140px; background: #fff3bf;">
  <p>Perfectly centred</p>
</div>

With Grid (the shortest)

html
<div style="display: grid; place-items: center; height: 140px; background: #e6fcf5;">
  <p>Also perfectly centred</p>
</div>

With positioning and transform

html
<div style="position: relative; height: 140px; background: #f3f0ff;">
  <p style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); margin: 0;">Centred with transform</p>
</div>

top: 50%; left: 50% puts the element's corner in the middle; translate(-50%, -50%) pulls it back by half its own size.


6. Centre One Line of Text Vertically

html
<div style="height: 60px; line-height: 60px; text-align: center; background: #ffe3e3;">One line, vertically centred</div>

This only works for a single line. For anything else, use Flexbox or Grid.


Quick Reference

GoalUse
Centre texttext-align: center
Centre a block acrossmargin: 0 auto + a width
Centre in both directionsGrid place-items: center, or Flex justify-content + align-items
Push an item rightFlex parent + margin-left: auto

Common Beginner Mistakes

1. text-align: center on a block to centre the block itself. It centres what's inside. Use margin: auto.

2. margin: auto without a width. A full-width block has nothing to centre.

3. Vertical centring without height. The container must be taller than its content.


Practice Questions

  1. How do you centre the text inside a div?
  2. A div is 400px wide. How do you centre it horizontally?
  3. Write the shortest CSS to centre something in both directions inside a 200px-tall box.
  4. How do you centre an image on its own line?
  5. In a flex row, how do you push the last item to the right?

Practice Answers

  1. text-align: center;
  2. margin: 0 auto;
  3. .box { display: grid; place-items: center; height: 200px; }
  4. img { display: block; margin: 0 auto; }
  5. Give it margin-left: auto;

Key Takeaways

  • Text: text-align: center. Blocks: margin: 0 auto with a width.
  • Both directions: Grid place-items: center or Flexbox.
  • margin-left: auto in a flex row pushes items right.
  • Positioning + translate(-50%, -50%) centres overlays.