How TO - Center Elements Vertically
Learn how to center an element vertically with CSS.
I am vertically centered.
How To Center Anything Vertically
Example
<style>
.container {
height: 200px;
position:
relative;
border: 3px solid green;
}
.vertical-center {
margin: 0;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
</style>
<div
class="container">
<div class="vertical-center">
<p>I am vertically centered.</p>
</div>
</div>
Try it Yourself »
How To Center Vertically AND Horizontally
Example
<style>
.container {
height: 200px;
position:
relative;
border: 3px solid green;
}
.center {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform:
translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
<div
class="container">
<div class="center">
<p>I am vertically and horizontally centered.</p>
</div>
</div>
Try it Yourself »
Tip: Go to our CSS Align Tutorial to learn more about aligning elements.
Tip: Go to our CSS Transform Tutorial to learn more about how to scale elements.