-
Approach 1 –
transformtranslateX/translateY:Example Here / Full Screen Example
In supported browsers (most of them), you can use
top: 50%/left: 50%in combination withtranslateX(-50%) translateY(-50%)to dynamically vertically/horizontally center the element.
.container {
position: absolute;
top: 50%;
left: 50%;
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
<div class="container">
<span>I'm vertically/horizontally centered!</span>
</div>
-
Approach 2 – Flexbox method:
Example Here / Full Screen Example
In supported browsers, set the
displayof the targeted element toflexand usealign-items: centerfor vertical centering andjustify-content: centerfor horizontal centering. Just don’t forget to add vendor prefixes for additional browser support (see example).
html, body, .container {
height: 100%;
}
.container {
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
justify-content: center;
}
<div class="container">
<span>I'm vertically/horizontally centered!</span>
</div>
-
Approach 3 –
table-cell/vertical-align: middle:Example Here / Full Screen Example
In some cases, you will need to ensure that the
html/bodyelement’s height is set to100%.For vertical alignment, set the parent element’s
width/heightto100%and adddisplay: table. Then for the child element, change thedisplaytotable-celland addvertical-align: middle.For horizontal centering, you could either add
text-align: centerto center the text and any otherinlinechildren elements. Alternatively, you could usemargin: 0 auto, assuming the element isblocklevel.
html, body {
height: 100%;
}
.parent {
width: 100%;
height: 100%;
display: table;
text-align: center;
}
.parent > .child {
display: table-cell;
vertical-align: middle;
}
<section class="parent">
<div class="child">I'm vertically/horizontally centered!</div>
</section>
-
Approach 4 – Absolutely positioned
50%from the top with displacement:Example Here / Full Screen Example
This approach assumes that the text has a known height – in this instance,
18px. Just absolutely position the element50%from the top, relative to the parent element. Use a negativemargin-topvalue that is half of the element’s known height, in this case –-9px.
html, body, .container {
height: 100%;
}
.container {
position: relative;
text-align: center;
}
.container > p {
position: absolute;
top: 50%;
left: 0;
right: 0;
margin-top: -9px;
}
<div class="container">
<p>I'm vertically/horizontally centered!</p>
</div>
-
Approach 5 – The
line-heightmethod (Least flexible – not suggested):Example Here
In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a
line-heightvalue on the child element equal to the fixed height of the parent element.Though this solution will work in some cases, it’s worth noting that it won’t work when there are multiple lines of text – like this.
.parent {
height: 200px;
width: 400px;
background: lightgray;
text-align: center;
}
.parent > .child {
line-height: 200px;
}
<div class="parent">
<span class="child">I'm vertically/horizontally centered!</span>
</div>