I am displaying number of boxes in a row with fix height and width, generated from <li> tags. now I need to align the text in the vertical center. The CSS vertical-align has no impact, maybe I am missing something???
I am not looking for tricks using (margin, padding, line-height), these will not work because some text are long and will break into two lines.
Please find the actual code:
CSS code
ul.catBlock{
width:960px;
height: 270px;
border:1px solid #ccc;
}
ul.catBlock li{
list-style: none;
float:left;
display:block;
text-align: center;
width:160px;
height: 100px;
}
ul.catBlock li a{
display: block;
padding: 30px 10px 5px 10px;
height:60px;
}
HTML code
<ul class="catBlock">
<li><a href="#">IP Phone</a></li>
<li><a href="#">Dual SIM Switch Server</a></li>
<li><a href="#">IP PBX</a></li>
</ul>
Simple solution for vertical align middle... for me it works like a charm
However many years late this response may be, anyone coming across this might just want to try
Browser support for flexbox is far better than it was when @scottjoudry posted his response above, but you may still want to consider prefixing or other options if you're trying to support much older browsers. caniuse: flex
Surprisingly (or not), the
vertical-align
tool actually works best for this job. Best of all, no Javascript is required.In the following example, I am positioning the
outer
class in the middle of the body, and theinner
class in the middle of theouter
class.Preview: http://jsfiddle.net/tLkSV/513/
HTML:
CSS:
Vertical align works by aligning the centers of elements that are next to each other. Applying vertical-align to a single element does absolutely nothing. If you add a second element that has no width but is the height of the container, your single element will move to vertically center with this no-width element, thus vertically centering it. The only requirements are that you set both elements to inline (or inline-block), and set their vertical-align attribute to
vertical-align: middle
.Note: You may notice in my code below that my
<span>
tag and<div>
tag are touching. Because they are both inline elements, a space will actually add a space between the no-width element and your div, so be sure to leave it out.There are no perfect answers provided here except Asaf's answer which doesn't provide any code nor any example, so I would like to contribute mine...
Inorder to make
vertical-align: middle;
work, you need to usedisplay: table;
for yourul
element anddisplay: table-cell;
forli
elements and than you can usevertical-align: middle;
forli
elements.You don't need to provide any explicit
margins
,paddings
to make your text vertically middle.Demo
In the future, this problem will be solved by flexbox. Right now the browser support is dismal, but it is supported in one form or another in all current browsers.
Browser support: http://caniuse.com/flexbox
Background on Flexbox: http://css-tricks.com/snippets/css/a-guide-to-flexbox/
As explained in here: https://css-tricks.com/centering-in-the-unknown/.
Screenshot: