I have a div
set to display:block
. (90px
height
and width
) and I have some text inside.
I need the text to be aligned in the center both vertically and horizontally.
I have tried text-align:center
, but it doesn't do the horizontal part so I tried vertical-align:middle
but it didn't work.
Any ideas?
If it is one line of text and/or image, then it is easy to do. Just use
that's it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/ Look for "vertical align".
Since they tend to be hacks or adding complicated divs... I usually use a table with a single cell to do it... to make it as simple as possible.
Update for 2016 / 2017:
It can be more commonly done with
transform
, and it works well even in older browsers such as IE10 and IE11. It can support multiple lines of text:example: https://jsfiddle.net/wb8u02kL/1/
To shrink wrap the width:
The solution above used a fixed width for the content area. To use a shrink wrap width, use
example: https://jsfiddle.net/wb8u02kL/2/
I tried flexbox, but it wasn't as widely supported, as it would break in some older version of IE such as IE10.
Adjusting line height to get the vertical alignment.
Common techniques as of 2014:
Approach 1 -
transform
translateX
/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.Approach 2 - Flexbox method:
Example Here / Full Screen Example
In supported browsers, set the
display
of the targeted element toflex
and usealign-items: center
for vertical centering andjustify-content: center
for horizontal centering. Just don't forget to add vendor prefixes for additional browser support (see example).Approach 3 -
table-cell
/vertical-align: middle
:Example Here / Full Screen Example
In some cases, you will need to ensure that the
html
/body
element's height is set to100%
.For vertical alignment, set the parent element's
width
/height
to100%
and adddisplay: table
. Then for the child element, change thedisplay
totable-cell
and addvertical-align: middle
.For horizontal centering, you could either add
text-align: center
to center the text and any otherinline
children elements. Alternatively, you could usemargin: 0 auto
assuming the element isblock
level.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-top
value that is half of the element's known height, in this case --9px
.Approach 5 - The
line-height
method (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-height
value 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.
Methods 4 and 5 aren't the most reliable. Go with one of the first 3.
Approach 1
Approach 2
Approach 3
Using flexbox/CSS:
The CSS:
Taken from Quick Tip: The Simplest Way To Center Elements Vertically And Horizontally