Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css trick for centering horizontal and vertical

Tags:

css

My .cent class is for <li> elements. I want to center the text of the <li> both horizontally and vertically. text-align:center; takes care of horizontal centering, but the vertical centering isn't working. What's the CSS trick for this?

.cent{
  height:20px;
  width:20px;
  text-align:center;
  vertical-align: middle;
}
like image 465
silow Avatar asked Dec 16 '10 12:12

silow


1 Answers

Use line-height property.

Set it equal to height of element, and text will be vertically centered.

.cent{
  height: 20px;
  width: 20px;
  text-align: center;
  /*vertical-align: middle;*/
  line-height: 20px;
}
like image 116
tomsseisums Avatar answered Sep 23 '22 01:09

tomsseisums