我试图在html和css的div框中居中对齐(水平和垂直)图像。我无法对齐它。这是我的下面的代码。
<div style="float:left;margin: 10px" >
<div style="border:1px solid gray;width: 60px;height: 60px;text-align:center;">
<img style="max-height: 60px;max-width: 60px;"
src="http://t1.gstatic.com/images?q=tbn:UnPJn535Xfha7M:http://gizmodo.com/assets/resources/2007/07/ipod_6gen_1.jpg"/>
</div>
</div>
图像与顶部对齐。我尝试在img标签内使用vertical-align:middle但它没用。
答案 0 :(得分:8)
遇到this post,它对我有用:
div{
position: relative;
}
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(垂直和水平对齐)
答案 1 :(得分:6)
<div> <img src="placeholder.gif" width="70" height="120" /> </div>
<div> <img src="placeholder.gif" width="90" height="80" /> </div>
<div> <img src="placeholder.gif" width="70" height="120" /> </div>
div {
float: left;
text-align: center;
width: 150px;
height: 150px;
margin: 5px;
border: 1px solid #ccc;
font-size: 1em;
line-height: 148px;
}
div img {
margin-top: expression(( 150 - this.height ) / 2);
}
html>body div img { /*hidden from IE 5-6 */
margin-top: 0; /* to clean up, just in case IE later supports valign! */
vertical-align: middle;
}
Note: some <script> tag, either inline or external (can be a dummy one), is needed to get IE 5.0 on track.
像魅力一样。
答案 2 :(得分:2)
尝试在包含图片的div中使用此样式。
<style="display: table-cell;
vertical-align: middle;">
答案 3 :(得分:1)
使用text-align:center to horizontal align ... no css tag in vertical align。
答案 4 :(得分:1)
<div style="float:left;margin: 10px; height: 199px; width: 242px;text-align:center; vertical-align:middle;" >
<div style="border:1px solid gray;width: 60px;height: 60px;">
<img style="max-height: 60px;max-width: 60px; height: 58px; width: 47px;"
src="http://t1.gstatic.com/images?q=tbn:UnPJn535Xfha7M:http://gizmodo.com/assets/resources/2007/07/ipod_6gen_1.jpg"/>
</div>
</div>
答案 5 :(得分:1)
如果你只想在中心显示图像,那就试试吧
<div style="background-position: center center; margin: 10px; text-align:center; background-image: url('http://t1.gstatic.com/images?q=tbn:UnPJn535Xfha7M:http://gizmodo.com/assets/resources/2007/07/ipod_6gen_1.jpg'); background-repeat: no-repeat;"
class="style1" >
</div>
答案 6 :(得分:1)
我通过添加display:table-cell
和vertical-align:middle
来尝试自己的解决方案。它在FireFox中运行良好。但在IE中惨遭失败:(
<div style="border:1px solid gray;width: 60px;height: 60px;display:table-cell; vertical-align:middle;text-align:center;">
<img style="max-height: 60px;max-width: 60px; " src="http://www.google.com/intl/en_ALL/images/logo.gif"/>
</div>
需要一些指示。
答案 7 :(得分:1)
您应该考虑使用vertical-align:middle和text-align:center。这将解决我猜的问题。
答案 8 :(得分:0)
使用display: inline-block
,text-align: center
和vertical-align: middle
的组合来固定两个维度:
<!doctype html>
<html lang="en">
<head>
<style type="text/css">
/* Use 100% height for html and body to provide context for vertical-align */
html, body, .container, .placeholder { height: 100%; }
/*Set dimensions of image in CSS */
img { width:16px; height:16px; }
/*Vertical centering for the necessary block boxes */
.placeholder, .wrapper, .content { vertical-align: middle; }
/* Use inline-block for wrapper and placeholder */
.placeholder, .wrapper { display: inline-block; }
/* Inline necessary to use text-align:center */
.content { display:inline; }
/* Text align for horizontal centering */
.container { text-align:center; }
/* Use width of less than 100% to avoid Firefox 3+ and Webkit wrapping issues */
.wrapper { width: 99%; }
/* Media query for IE7 and under */
@media,
{
.wrapper { display:inline; }
}
</style>
<title>Vertical/Horizontal Centering Test</title>
</head>
<body>
<div class="container">
<div class="content">
<div class="wrapper">
<img src="http://mozilla.com/favicon.ico" alt="test image">
</div>
</div>
<span class="placeholder"></span>
</div>
</body>
</html>