[英]Text Align Middle Vertically CSS
I building a header and I have HTML code below 我建立了一个标头,下面有HTML代码
#header{ height:50px; border-bottom: 1px solid #000; } .left{ display: inline-block; float: left; } .right{ display: inline-block; float: right; } .logo{ display: inline-block; font-size: 40px; } .slogan{ display: inline-block; vertical-align: middle; } .menu{ margin: 0px; } .menu li { display: inline; }
<div id="header"> <div class="left"> <div class="logo">LOGO</div> <div class="slogan">SLOGAN HERE</div> </div> <div class="right"> <nav> <ul class="menu"> <li><a href="" title="">Menu 1</a></li> <li><a href="" title="">Menu 2</a></li> <li><a href="" title="">Menu 3</a></li> <li><a href="" title="">Menu 4</a></li> </ul> </nav> </div> </div>
I want Slogan and Menu is middle of header div so I use CSS vertical-align: middle;
我希望标语和菜单位于标题div的中间,因此我使用CSS vertical-align: middle;
but it's not working. 但它不起作用。 It only working with vertical-align: top;
它仅适用于vertical-align: top;
How to fix it ? 如何解决?
Thanks you so much. 非常感谢。
You could use line-height to achieve the effect you want. 您可以使用line-height实现所需的效果。 The headers height is 50px, so you should give the list items a line-height of 50px. 标头的高度为50px,因此应为列表项的行高设置为50px。 I think this will solve your problem in this case but it wouldn't work if your text has multiple lines. 我认为这可以解决您的问题,但如果您的文字有多行,则无法使用。
Change display: inline-block
to display: table
on element .left
, then on .slogan
change display: table-cell;
更改display: inline-block
display: table
元素.left
上的display: table
,然后在.slogan
更改display: table-cell;
to display: inline-block;
display: inline-block;
. 。
#header{ height:50px; border-bottom: 1px solid #000; } .left{ /*display: inline-block;*/ display: table; float: left; } .right{ display: inline-block; float: right; } .logo{ display: inline-block; font-size: 40px; } .slogan{ vertical-align: middle; display: table-cell; /*display: inline-block;*/ } .menu{ margin: 0px; } .menu li { display: inline; }
<div id="header"> <div class="left"> <div class="logo">LOGO</div> <div class="slogan">SLOGAN HERE</div> </div> <div class="right"> <nav> <ul class="menu"> <li><a href="" title="">Menu 1</a></li> <li><a href="" title="">Menu 2</a></li> <li><a href="" title="">Menu 3</a></li> <li><a href="" title="">Menu 4</a></li> </ul> </nav> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.