I have a div
element which contains text, and I want to align the contents of this div
vertically center.
Here is my div style:
#box {
height: 170px;
width: 270px;
background: #000;
font-size: 48px;
color: #FFF;
text-align: center;
}
<div id="box">
Lorem ipsum dolor sit
</div>
What is the best way to do this?
You can also use below properties.
Even better idea for this. You can do like this too
For a single line of text (or a single character) you can use this technique:
It can be used when
#box
has non-fixed, relative height in %.<div id="box"></div>
See live demo at JsBin (easier to edit CSS) or JsFiddle (easier to change height of result frame).
If you want to place inner text in HTML, not in CSS, then you need to wrap text content in additional inline element and edit
#box::after
to match it. (And, of course,content:
property should be removed.)For example,
<div id="box"><span>TextContent</span></div>
In this case
#box::after
should be replaced to#box span
.For IE8 support you must replace
::
with:
.A very simple & most powerful solution to vertically align center:
Set it within
button
instead ofdiv
if you don't care about its little visual 3D effect.You can use the positioning method in CSS:
Check the result here....
HTML:
CSS:
Hope you use this method too.