简体   繁体   English

如何垂直对齐导航栏的链接?

[英]How do I vertically align my links for a navbar?

I am trying to make a navigation bar but I'm not sure how to line up the li and a tags vertically. 我试图做一个导航栏,但我不知道怎么排队的lia垂直的标签。 How can I do this? 我怎样才能做到这一点?

 #navbar { font-weight: bold; display: block; background-color: #000; } li { float: left; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #000; } li a { color: white; vertical-align: middle; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #111; } 
 <div id="navbar"> <ul> <li><img src="img/logo.png" width="75" height="75"></li> <li><a href="#">HOME</a></li> <li><a href="#">MY WORK</a></li> <li><a href="#">HIRE ME</a></li> <li><a href="#">PROJECTS</a></li> </ul> </div> 

One of the common ways nowadays is to do it with the Flexbox : 如今,常见的方法之一就是使用Flexbox来做到这一点

 #navbar { font-weight: bold; display: block; background-color: #000; } ul { display: flex; /* displays flex-items (children) inline */ align-items: center; /* centers them vertically */ list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #000; } li { float: left; } li a { color: white; vertical-align: middle; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #111; } /* addition */ img {display: block} /* or: "vertical-align: middle" // removes bottom margin/whitespace */ 
 <div id="navbar"> <ul> <li><img src="https://placehold.it/100x100" alt="" width="75" height="75"></li> <li><a href="#">HOME</a></li> <li><a href="#">MY WORK</a></li> <li><a href="#">HIRE ME</a></li> <li><a href="#">PROJECTS</a></li> </ul> </div> 

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

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