简体   繁体   English

卧式 <ul> 导航-如何垂直对齐内容 <li>

[英]Horizontal <ul> navigation - how to vertical align contents of <li>

I have a horizontal <ul> styled with CSS. 我有一个用CSS样式的水平<ul> Works nice, rolloevers everything, however, the list has a height of 50px and I need to position the in the links in the vertical middle. 效果很好,rolloevers一切,但是,列表的高度为50px,我需要将其放置在垂直中间的链接中。 For some reason, I can't figure out how to do that... 由于某种原因,我不知道该怎么做...

The relevant code is here: http://jsfiddle.net/XPFMB/ 相关代码在这里: http : //jsfiddle.net/XPFMB/

How to make the "ssss" links go to the middle of the grey bar? 如何使“ ssss”链接转到灰色条的中间?

Add line-height: 50px; 添加line-height: 50px; to your CSS for .nav ul li a 到您的CSS以获取.nav ul li a

Add line-height to the li 's CSS: li的CSS上添加line-height:

.nav li {
    margin: 0px;
    float: left;
    line-height: 50px;
}

JS Fiddle demo . JS小提琴演示

This will cause problems if your text is long enough to run to a second line, though; 但是,如果您的文本足够长以至第二行, 则会导致问题。 however to account for that you could use min-width (instead of width ) on the a elements, and then add white-space: nowrap to both the ul and the li : 但是要考虑到这一点,您可以a元素上使用min-width (而不是width ),然后在ul li添加white-space: nowrap

.nav ul {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
    vertical-align: middle;
    white-space: nowrap;
}
.nav li {
    margin: 0px;
    float: left;
    line-height: 50px;
    white-space: nowrap;
}

JS Fiddle demo . JS小提琴演示

It's still not perfect; 它仍然不完美; but it does reduce the likelihood of the layout breaking by using link-text a few characters too long. 但是通过使用一些太长的字符的链接文本,确实降低了布局中断的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM