简体   繁体   English

为什么文本垂直居中对齐

[英]Why is text vertically aligning to middle

I'm trying to create a header in which there would be a logo to the far left, and a nav menu to the far right. 我正在尝试创建一个标题,其中最左边有一个徽标,最右边有一个导航菜单。 The problem i'm having is the vertical alignment of the logo image to the nav menu. 我遇到的问题是徽标图像与导航菜单的垂直对齐。 I would like the bottom of the logo to vertically align with the bottom of the li menu elements (bottom, not text-bottom), but I am having trouble accomplishing that. 我希望徽标的底部与li菜单元素的底部(底部,而不是文本底部)垂直对齐,但是我很难做到这一点。

I think part of the issue is the floats - logo is floated to the left, ul is floated to the right. 我认为问题的一部分是浮动内容-徽标浮动在左侧,ul浮动在右侧。

HTML / CSS HTML / CSS

 html { height: 100%; font-size: 16px; } body { background: #fff url('images/background.jpg') no-repeat bottom fixed; background-size: cover; -moz-background-size: cover; -o-background-size: cover; -webkit-background-size: cover; padding-top: 20px; padding-bottom: 20px; font-size: 62.5%; } header { position: fixed; width: 95%; margin: 0 auto; top: 26px; display: inline-block; padding-bottom: 16px; border-bottom: 1px solid #959595; } .nav ul { float: right; } .nav li { float: left; padding-right: 40px; font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 2.2em; } .nav li a { text-decoration: none; color: #a1a1a1; } .container { width: 95%; margin: 0 auto; } .logo { float: left; } 
 <div class="container"> <header> <div class="nav"> <img class="logo" src="http://www.placehold.it/80"> <ul> <li><a href="bio.html">bio</a> </li> <li><a href="calendar.html">calendar</a> </li> <li><a href="media.html">media</a> </li> <li><a href="contact.html">contact</a> </li> </ul> </div> </header> </div> 

set margin-top:10px for ul li then give padding: 10px 0 for header ul li设置margin-top:10px ,然后为header padding: 10px 0

 html { height: 100%; font-size: 16px; } body { background: #fff url('images/background.jpg') no-repeat bottom fixed; background-size: cover; -moz-background-size: cover; -o-background-size: cover; -webkit-background-size: cover; padding-top: 20px; padding-bottom: 20px; font-size: 62.5%; } header { border-bottom: 1px solid #959595; display: inline-block; margin: 0; padding: 20px 0; position: fixed; top: 26px; width: 95% } .nav ul { float: right; margin-top: 15px; } .nav li { float: left; padding-right: 40px; font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 2.2em; } .nav li a { text-decoration: none; color: #a1a1a1; } .container { width: 95%; margin: 0 auto; } .logo { float: left; } 
 <div class="container"> <header> <div class="nav"> <img class="logo" src="http://lorempixel.com/50/50"> <ul> <li><a href="bio.html">bio</a> </li> <li><a href="calendar.html">calendar</a> </li> <li><a href="media.html">media</a> </li> <li><a href="contact.html">contact</a> </li> </ul> </div> </header> </div> 

instead float - use display: inline-block; 而不是float使用display: inline-block;

because vertical alignment doesn't work with floated elements 因为垂直对齐方式不适用于浮动元素

 html { height: 100%; font-size: 16px; } body { background: #fff url('images/background.jpg') no-repeat bottom fixed; background-size: cover; -moz-background-size: cover; -o-background-size: cover; -webkit-background-size: cover; padding-top: 20px; padding-bottom: 20px; font-size: 62.5%; } header { position: fixed; width: 95%; margin: 0 auto; top: 26px; display: inline-block; padding-bottom: 16px; border-bottom: 1px solid #959595; } .nav .logo, .nav ul { display: inline-block; vertical-align: middle; padding: 0; } .nav ul{ text-align: right; width: calc(100% - 120px); } .nav li { display: inline-block; vertical-align: middle; padding: 0 20px; font-family:'Open Sans', sans-serif; font-weight: 300; font-size: 2.2em; } .nav li a { text-decoration: none; color: #a1a1a1; } .container { width: 95%; margin: 0 auto; } 
 <div class="container"> <header> <div class="nav"> <img class="logo" src="http://www.placehold.it/80" /> <ul> <li><a href="bio.html">bio</a></li> <li><a href="calendar.html">calendar</a></li> <li><a href="media.html">media</a></li> <li><a href="contact.html">contact</a></li> </ul> </div> </header> </div> 

Fiddle 小提琴

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

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