[英]Css having trouble aligning image with text horizontally
I'm having touble aligning my text horizontally with my image... It's actually aligned horizontally, but the image creates the illusion that it's not. 我在将文本与图像水平对齐方面遇到麻烦...实际上是在水平方向对齐,但是图像产生了一种错觉,即并非如此。
Code(CSS): 代码(CSS):
/* Copyright © 2016 Dynavio Cooperative */
.navbar {
width: 100%;
border-bottom: 1px solid #C8C8C8;
box-shadow: 0 1px 2px #000000;
}
.nav-logo {
width: 130px;
height: 58px;
display: inline-block;
vertical-align: middle;
}
.nav-items {
display: inline-block;
}
.nav-item {
display: inline-block;
font-family: SinkinSansRegular;
font-size: 20px;
}
Code(HTML): 代码(HTML):
<div class="navbar">
<img src="../images/logo.png" class="nav-logo">
<ul class="nav-items">
<li class="nav-item">Homepage</li>
</ul>
</div>
JsFiddle: https://jsfiddle.net/ha91bzsu/ JsFiddle: https ://jsfiddle.net/ha91bzsu/
Just add vertical-align: middle; padding: 0;
只需添加vertical-align: middle; padding: 0;
vertical-align: middle; padding: 0;
to your .nav-items
rule and you'll get an equivalent result across browsers 您的.nav-items
规则,您将在浏览器中获得同等的结果
Stack snippet 堆栈片段
/* Copyright © 2016 Dynavio Cooperative */ .navbar { width: 100%; border-bottom: 1px solid #C8C8C8; box-shadow: 0 1px 2px #000000; position: fixed; top: 0; } .nav-logo { width: 130px; height: 58px; display: inline-block; vertical-align: middle; } .nav-items { display: inline-block; vertical-align: middle; padding: 0; } .nav-item { display: inline-block; font-size: 20px; }
<div class="navbar"> <img src="http://87.92.41.2/logo.png" class="nav-logo"> <ul class="nav-items"> <li class="nav-item">Homepage</li> </ul> </div>
Approach 1: 方法1:
modify vertical-align: middle;
修改vertical-align: middle;
property of .nav-logo
to vertical-align: top;
.nav-logo
属性为vertical-align: top;
add a property margin-top: 17px;
添加一个属性margin-top: 17px;
to the .nav-items
. 到.nav-items
。 You can adjust this margin and text height property upto your satisfaction. 您可以调整此边距和文本高度属性,直至达到您的满意程度。
jsfiddle: https://jsfiddle.net/sajibche/twpy8eq8/ jsfiddle: https ://jsfiddle.net/sajibche/twpy8eq8/
Approach 2 方法2
Another dynamic solution: Just use vertical-align: bottom;
另一个动态解决方案:只需使用vertical-align: bottom;
for both .nav-logo
and .nav-items
elements. .nav-logo
和.nav-items
元素。
jsfiddle: https://jsfiddle.net/sajibche/cd52ytch/ jsfiddle: https ://jsfiddle.net/sajibche/cd52ytch/
Try using the line height property of CSS on the text 尝试在文本上使用CSS的line height属性
http://www.w3schools.com/cssref/pr_dim_line-height.asp http://www.w3schools.com/cssref/pr_dim_line-height.asp
If that doesn't work, try small adjustments using margin or padding. 如果这样不起作用,请尝试使用边距或填充进行小的调整。
You would have to put display: block;
您将必须放置display: block;
in order to make it work. 为了使它工作。 Then you can controll the padding and margin. 然后,您可以控制填充和边距。 Note: add a clearfix class to your navbar also. 注意:也将clearfix类添加到导航栏。
updated jsfiddle here 在这里更新了jsfiddle
.navbar {
width: 100%;
border-bottom: 1px solid #C8C8C8;
box-shadow: 0 1px 2px #000000;
position: fixed;
top: 0;
}
.nav-logo {
width: 130px;
height: 58px;
line-height: 58px;
display: block;
float: left;
}
.nav-items {
display: block;
float: left;
height: 58px;
line-height: 58px;
padding: 4px 0 0 0;
box-sizing: border-box;
background-color: aqua;
}
.nav-item {
display: inline-block;
font-size: 20px;
background: aqua;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.