horizontal nav bar… vertically-align element

2019-02-23 08:46发布

I currently have the following code. I am trying to get the second, third, and fourth "li" elements to vertically-align in the middle of the nav bar. The first "li" is an image, and the second-fourth are all text.

Here is a screenshot of it currently.

http://i49.tinypic.com/bfesl3.png

I have tried using "vertical-align:middle" and padding. Note that the second, third and fourth "li" elements appear vertically-aligned in the middle if viewed in Firefox but not in other browsers.

Here is the code I have.

<ul class = "nav">

<li><a href="index.html" style="border-right:1px #FFFFFF solid; padding-top:4.6px; padding-bottom:17.3px;"><img src="img/randomtitle.png" style="padding-left:8px;padding-top:8px;"/></a></li>
<li><a href="aboutme.html" style="vertical-align:middle;padding-top:32px;margin-left:-15px;padding-bottom:14px;padding-right:20px;border-right:1px #ffffff solid;">about me</a></li>
<li><a href="films.html" style="vertical-align:middle;padding-top:32px;margin-left:1px;padding-bottom:14px;padding-right:30.5px;border-right:1px #ffffff solid;">films</a></li>
<li><a href="contactme.html" style="vertical-align:middle;padding-top:32px;margin-left:-20px;padding-bottom:14px;padding-right:11px;border-right:1px #ffffff solid;">contact me</a></li>

<span class="navlinkimages">
<li><a href=  target="_blank"><img src="social/social_vimeo.png" height = "30px" style = "margin-right:-14px;"/></a></li>
<li><a href= target="_blank"><img src="social/social_youtube.png" height = "30px" style = "margin-right:-14px;"/></a></li>
<li><a href= target="_blank"><img src="social/social_facebook.png" height="30px" style = "margin-right:-14px;"/></a></li>
<li><a href=  target="_blank"><img src="social/social_twitter.png" height = "30px" style = "margin-right:-14px;" /></a></li>
</span>
</ul>`

CSS Code:

.nav {
    list-style-type:none;
    padding-left:0;
    margin-left:0;
    font-family:DinC;
    padding-bottom:5px;
    background-color: #000000;
    border-radius:5px;
    height:35px;
}

.navlinkimages {
  float:right;
  padding-top:5px;
}

.nav li {
  display:inline;
  vertical-align:middle;
}

ul.nav a:hover {
  color:#FA4B2A;
}

.nav li img {
  vertical-align:middle;
}

ul.nav a{
  text-decoration:none;
  margin-right:27px;
  color:#FFFFFF;
}

Is there a way to make it vertically-aligned on all browsers?

Thanks!

2条回答
不美不萌又怎样
2楼-- · 2019-02-23 09:14

Give each of those li a's a class middle:

<li>
    <a class="middle" href="aboutme.html" style="REDACTED">about me</a>
</li>

Define middle like:

.middle {
    line-height: 35px; /** or same as ul height */
}

Inline elements are always vertically centered within their line-height.

DEMO

查看更多
走好不送
3楼-- · 2019-02-23 09:30

Try this:

.nav li {
  display:inline;
  vertical-align:middle;
  line-height:35px;
}
查看更多
登录 后发表回答