CSS - 浮动 div 中的图像垂直居中
我决定放弃这个问题并需要一些帮助:)。根据标题,尝试将包含在浮动固定高度 div 中心的锚元素中的图像垂直对齐。
做了很多谷歌搜索来寻找解决方案,当 div 没有浮动时(但它需要浮动),我能得到的衣柜在下面。任何想法将不胜感激!
.class_name {
/*float: left*/
width:153px;
height:153px;
margin:3px;
padding:4px;
border:1px solid #dedede;
text-align: center;
vertical-align: middle;
background-color: #000;
display: table-cell;
}
<div class="class_name">
<a href=""><img src="image.jpg" alt="" /></a>
</div>
I've decided to throw in the towel on this problem and need some help :). As per title trying to vertically align an image wrapped in an anchor element in the center of a floated fixed height div.
Done a lot of googling for solutions and the closet I can get is below when the div is not floated (however it needs to be). Any ideas would be greatfully appreciated!
.class_name {
/*float: left*/
width:153px;
height:153px;
margin:3px;
padding:4px;
border:1px solid #dedede;
text-align: center;
vertical-align: middle;
background-color: #000;
display: table-cell;
}
<div class="class_name">
<a href=""><img src="image.jpg" alt="" /></a>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我昨晚遇到了同样的问题(对于类似画廊的事情),并在偶然发现 此页面。我很高兴地报告这似乎也适用于浮动元素!
技巧基本上是给外部元素“display:table;”,和内部元素(包含img)“display:table-cell;”。
对于 IE8,您确实需要处于标准模式。需要一些额外的定位才能使其在 IE7 中工作:
Well, I bumped into the same issue last night (for a gallery-like type of thing), and managed to find a solution after stumbling onto this page. I'm happy to report this also seems to work for floated elements!
The trick is basically to give the outer element "display: table;", and the inner element (containing the img) "display: table-cell;".
For IE8, you do need to be in standards mode. Some additional positioning is needed to get it to work in IE7:
如果高度固定并且您知道图像的大小,则只需手动定位图像即可。在图像上使用
position:absolute;top:25px;
或类似的内容,或者为图像添加边距:margin:25px 0;
。If the height is fixed and you know the size of the image, just position the image manually. Use
position:absolute;top:25px;
on the image or something like that, or add a margin to the image:margin:25px 0;
.这里有一个跨浏览器 CSS 解决方案: http:// /www.vdotmedia.com/blog/vertically-center-content-with-css/
There is a cross browser css solution for this available here: http://www.vdotmedia.com/blog/vertically-center-content-with-css/