How can I vertically center text in an element?

For example, from this:

an example

to this:

second example

I know I can specify the height property on the text and then use

position: absolute;
top: 0;
bottom: 0;
margin: 0;

but I don't want this because the height of the text is variable.


1 Answer 1


Check this out: https://stackoverflow.com/questions/8865458/how-to-align-text-vertically-center-in-div-with-css


  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>


div {
  width: 250px;
  height: 100px;
  line-height: 100px;
  text-align: center;

span {
  display: inline-block;
  vertical-align: middle;
  line-height: normal;      


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.