Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use CSS to vertically center the text in an anchor, within a LI?

Variations on this question have been asked many times. Vertical centering with CSS is a challenge.

I have a particular scenario, dealing with a list displayed horizontally. The markup is like this:

  <ul id='ul1' class='c'>
    <li><a href='javascript:void(0)'>Fribble Fromme</a></li>
    <li><a href='javascript:void(0)'>Fobble</a></li>
    <li><a href='javascript:void(0)'>Foo Fickle Pickle</a></li>
  </ul>

The style is like this:

  ul.c {
    height:52px;
    text-align:center;
  }
  ul li a {
    float:left;
    text-decoration:none;
    border: 1px solid Maroon;
    padding:2px 12px;
    background:#FFEF8A;
    line-height:1em;
    width:100px;
  }
  ul li a:hover {
    background: #CCC;
  }
  ul li {
    height:52px;
    display:inline-block;
  }

The resulting list looks like this:

enter image description here

But I want all the boxes to be the same height, and I want the text to be vertically centered in each box. I can set the box-height by adding a height style for the A elements. The result looks like this:

enter image description here

...which is close to what I want, but the vertical-centering isn't happening.

I can set line-height for the text, as suggested in this post, to do the vertical centering. I can even pick different values of line-height for different A elements, if I know which of the elements will get multiple lines of text. But I don't know which ones will require multiple lines.

How can I get it to center when some of the A elements have text that wraps?

like image 664
Cheeso Avatar asked Sep 30 '11 03:09

Cheeso


People also ask

How do I center text vertically in Li?

Inorder to make vertical-align: middle; work, you need to use display: table; for your ul element and display: table-cell; for li elements and than you can use vertical-align: middle; for li elements. You don't need to provide any explicit margins , paddings to make your text vertically middle.

How do I center text in anchor CSS?

Simply text-align: center; in your existing class.

How do I center text vertically in an element?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How do you vertically align the Li elements?

The proper way to align vertical-align: middle, work, is to use display: table, for your ul element and display: table-cell, for li elements and vertical-align: middle, for li elements. It is not necessary to provide any explicit margins or paddings to make your text vertically middle.


2 Answers

Old question, but the answer can now be updated with Flexbox.

a {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}
like image 128
andydavies Avatar answered Oct 17 '22 22:10

andydavies


You could use display:table, etc. along with vertical-align:middle

ul.c {
    text-align:center;
    display:table;
}

ul li {   
    float:left;    
}

ul li a {
    text-decoration:none;
    border: 1px solid Maroon;
    padding:2px 12px;
    background:#FFEF8A;
    width:100px;
    height:52px;
    display:table-cell;
    vertical-align:middle;
}

ul li a:hover {
    background: #CCC;
}

Example: http://jsfiddle.net/kf52n/2/

like image 17
Jason Gennaro Avatar answered Oct 17 '22 22:10

Jason Gennaro