How to center text (horizontally and vertically) inside a div block?
P粉969253139
2023-08-24 16:58:26
<p>I have a <code>div</code> set to <code>display:block</code> (<code>90px</code> <code>height</code> and < ;code>width</code>), and I have some text inside.</p>
<p>I need the text to be centered vertically and horizontally aligned. </p>
<p>I have tried <code>text-align:center</code>, but it doesn't do the vertical centering part, so I tried <code>vertical-align:middle</code>, but it didn't work.</p>
<p>Any ideas? </p>
Commonly used technologies 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.Method 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.Although this solution works in some cases, it's worth noting that it doesn't work when there are multiple lines of text - like this.
Methods 4 and 5 are not the most reliable. Choose one of the top 3.
If it's a line of text and/or an image, it's easy to do. Just use:
That's it. If it can be multiple lines, it's a little more complicated. But there is a solution at http://pmob.co.uk/. Look for "vertical alignment".
Since they tend to be hacks or adding complex divs... I usually do it using a table with a single cell... to keep it as simple as possible.
2020 Update:
Unless you need to make it work on earlier browsers such as Internet Explorer 10, you can use Flexbox. It is widely supported by all current major browsers. Basically, the container needs to be designated as a flex container and centered along its main and horizontal axes:
Specify a fixed width for a child item, called a "flex item":
Example:http://jsfiddle.net/2woqsef1/1/
To shrink-wrap the content, it is even simpler: just remove the
flex: ...
line from the flex item, and it is automatically shrink-wrapped.Example:http://jsfiddle.net/2woqsef1/2/
The above examples have been tested on major browsers including MS Edge and Internet Explorer 11.
One technical note if you need to customize it: inside of the flex item, since this flex item is not a flex container itself, the old non-flexbox way of CSS works as expected. However, if you add an additional flex item to the current flex container, the two flex items will be horizontally placed. To make them vertically placed, add the
flex-direction: column;
to the flex container. This is how it works between a flex container and its immediate child elements.There is an alternative method of doing the centering: by not specifying
center
for the distribution on the main and cross axis for the flex container, but instead specifymargin: auto
on the flex item to take up all extra space in all four directions, and the evenly distributed margins will make the flex item centered in all directions. This works except when there are multiple flex items. Also, this technique works on MS Edge but not on Internet Explorer 11.2016/2017 Update:
It can be more commonly done with
transform
, and it works well even in older browsers such as Internet Explorer 10 and Internet Explorer 11. It can support multiple lines of text:Example:https://jsfiddle.net/wb8u02kL/1/
Shrink wrap width:
The above solution uses a fixed width for the content area. To use shrinkwrap width, use
Example:https://jsfiddle.net/wb8u02kL/2/
If the support for Internet Explorer 10 is needed, then flexbox won't work and the method above and the
line-height
method would work. Otherwise, flexbox would do the job.