[英]Vertically aligning elements with CSS
I have an element that's 60px tall that has other elements like an image and a couple of spans inside of it and I'm having trouble getting them to align vertically inside the 60px high element. 我有一个高60像素的元素,其中包含其他元素,如图像和几个跨度,我无法让它们在60px高元素内垂直对齐。 Here's the mockup and CSS:
这是模型和CSS:
<div class="member">
<img src="images/pic.png" alt="John Smiths's Profile Picture" class="pic">
<span class="name"><a href="">John Smith</a></span>
<span class="skills">PHP, MySQL, Javascript, C#, Java</span>
</div>
#sidebar .member {
height: 60px;
margin-bottom: 10px;
vertical-align: center;
}
#sidebar .member .name {
font-size: 15px;
font-weight: bold;
}
#sidebar .member .pic {
width: 50px;
height: 50px;
border-radius: 50%;
float: left;
margin-right: 10px;
}
#sidebar .member .skills {
display: block;
font-size: 12px;
overflow: hidden;
}
I've put it up on jsfiddle: http://jsfiddle.net/CYFyx/2/ 我把它放在了jsfiddle: http : //jsfiddle.net/CYFyx/2/
As you can see, the elements within the .member
element push to the top. 如您所见,
.member
元素中的元素.member
推。 I need them vertically aligned like so: 我需要它们垂直对齐,如下所示:
Already tried vertical-align: middle;
已经尝试过
vertical-align: middle;
but with no luck. 但没有运气。
You can use vertical-align: middle
in td table layout only. 您只能在td表布局
vertical-align: middle
使用vertical-align: middle
。 So you have to add a div around the spans 所以你必须在跨度上添加一个div
<div class="cell">
<span class="name"><a href="">John Smith</a></span>
<span class="skills">PHP, MySQL, Javascript, C#, Java</span>
</div>
with this properties 有这个属性
#sidebar .member .cell {
display: table-cell;
vertical-align: middle;
height: 50px;
}
You can test it here: http://jsfiddle.net/Tn2RU/ 你可以在这里测试一下: http : //jsfiddle.net/Tn2RU/
Try putting them all in a div that you can vertically align using 尝试将它们全部放在可以使用垂直对齐的div中
position:relative;
margin-top: auto;
margin-bottom: auto;
height: XXpx;
You need to set the width of a parent element and width of your children so they must go into next line. 您需要设置父元素的宽度和子元素的宽度,以便它们必须进入下一行。
The other posibility would be to set your parent element to position:relative
and then use position:absolute
on all children and simply, precisely position them with top:20px;
另一个可能性是将你的父元素设置为
position:relative
,然后在所有子元素上使用position:absolute
,并简单地将它们精确定位在top:20px;
, then next one top:40px;
,然后下一个
top:40px;
etc etc. 等等
With this second solution you can get exact pixel positioning of all children elements. 使用第二种解决方案,您可以获得所有子元素的精确像素定位。
That shall positively give you best results. 这将积极地给你最好的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.