</> Code Corner

CSS · CSS Tutorial · Lesson 27

CSS Align

Center elements horizontally and vertically.

There are several ways to align elements.

Goal Technique
Center a block element margin: auto; with a width
Center text text-align: center;
Center an image display: block; margin: auto;
Center both ways display: flex; justify-content: center; align-items: center;

Example

<style>
.center { margin: auto; width: 60%; border: 3px solid #73AD21; padding: 10px; text-align: center; }
.flex { display: flex; justify-content: center; align-items: center; height: 120px; border: 3px solid green; margin-top: 10px; }
</style>
<div class="center">Centered block with centered text</div>
<div class="flex"><p>Perfectly centered with flexbox</p></div>

Keep reading “CSS Align”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋