On this page12 sections
Lecture 018: Alignment & Centering
| Category | 4: Layout Basics |
| Lecture | 018 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
"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
<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
<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
<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
<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
<div style="display: flex; justify-content: center; align-items: center; height: 140px; background: #fff3bf;">
<p>Perfectly centred</p>
</div>With Grid (the shortest)
<div style="display: grid; place-items: center; height: 140px; background: #e6fcf5;">
<p>Also perfectly centred</p>
</div>With positioning and transform
<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
<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
| Goal | Use |
|---|---|
| Centre text | text-align: center |
| Centre a block across | margin: 0 auto + a width |
| Centre in both directions | Grid place-items: center, or Flex justify-content + align-items |
| Push an item right | Flex 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
- How do you centre the text inside a div?
- A div is 400px wide. How do you centre it horizontally?
- Write the shortest CSS to centre something in both directions inside a 200px-tall box.
- How do you centre an image on its own line?
- In a flex row, how do you push the last item to the right?
Practice Answers
text-align: center;margin: 0 auto;.box { display: grid; place-items: center; height: 200px; }img { display: block; margin: 0 auto; }- Give it
margin-left: auto;
Key Takeaways
- Text:
text-align: center. Blocks:margin: 0 autowith a width. - Both directions: Grid
place-items: centeror Flexbox. margin-left: autoin a flex row pushes items right.- Positioning +
translate(-50%, -50%)centres overlays.