Html 需要将CSS中的图像居中对齐

Html 需要将CSS中的图像居中对齐,html,css,image,Html,Css,Image,我试图在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;"

我试图在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标签内使用垂直对齐:中间,但没有用

使用文本对齐:居中到水平对齐…垂直对齐中没有css标记。

使用文本对齐:居中到水平对齐…垂直对齐中没有css标记。

如果您只想在中间显示图像,请尝试此操作

 <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>
 <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>

如果你想在中间显示图像,那么试试这个

 <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>

我尝试了自己的解决方案,添加了display:table单元格和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>

需要一些指针。

我尝试了自己的解决方案,添加了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>
<div>&nbsp;<img src="placeholder.gif" width="70" height="120" />&nbsp;</div>
<div>&nbsp;<img src="placeholder.gif" width="90" height="80" />&nbsp;</div>
<div>&nbsp;<img src="placeholder.gif" width="70" height="120" />&nbsp;</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.
需要一些指针

<div>&nbsp;<img src="placeholder.gif" width="70" height="120" />&nbsp;</div>
<div>&nbsp;<img src="placeholder.gif" width="90" height="80" />&nbsp;</div>
<div>&nbsp;<img src="placeholder.gif" width="70" height="120" />&nbsp;</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.
工作起来很有魅力


就像一个符咒。

你应该考虑使用垂直对齐:中间和文本对齐:中心。这将解决这个问题,我猜。

你应该考虑使用垂直对齐:中间和文本对齐:中心。我想这会解决问题。

遇到了问题,它对我起了作用:

div{
    position: relative;
}

img {
    position: absolute;
    top: 0; bottom:0; left: 0; right:0;
    margin: auto;

}
垂直和水平对齐

对我来说很有效:

div{
    position: relative;
}

img {
    position: absolute;
    top: 0; bottom:0; left: 0; right:0;
    margin: auto;

}
垂直和水平对齐

使用显示:内联块、文本对齐:居中和垂直对齐:居中组合来居中两个尺寸:

/*使用html和正文的100%高度为垂直对齐提供上下文*/ html、正文、容器、占位符{高度:100%;} /*在CSS中设置图像的尺寸*/ img{宽度:16px;高度:16px;} /*必要的方块盒的垂直居中*/ .placeholder、.wrapper、.content{垂直对齐:中间;} /*使用内联块作为包装器和占位符*/ .placeholder、.wrapper{显示:内联块;} /*使用文本对齐所需的内联:居中*/ .content{display:inline;} /*水平居中文本对齐*/ .container{文本对齐:居中;} /*使用小于100%的宽度可避免Firefox 3+和Webkit包装问题*/ .wrapper{宽度:99%;} /*IE7及以下版本的媒体查询*/ @媒体,, { .wrapper{display:inline;} } 垂直/水平定心试验 将“显示:内联块”、“文本对齐:居中”和“垂直对齐:居中”组合用于两个尺寸标注的居中:

/*使用html和正文的100%高度为垂直对齐提供上下文*/ html、正文、容器、占位符{高度:100%;} /*在CSS中设置图像的尺寸*/ img{宽度:16px;高度:16px;} /*必要的方块盒的垂直居中*/ .placeholder、.wrapper、.content{垂直对齐:中间;} /*使用内联块作为包装器和占位符*/ .placeholder、.wrapper{显示:内联块;} /*使用文本对齐所需的内联:居中*/ .content{display:inline;} /*水平居中文本对齐*/ .container{文本对齐:居中;} /*使用小于100%的宽度可避免Firefox 3+和Webkit包装问题*/ .wrapper{宽度:99%;} /*IE7及以下版本的媒体查询*/ @媒体,, { .wrapper{display:inline;} } 垂直/水平定心试验
尝试在包含图像的div中使用此样式

<style="display: table-cell;
vertical-align: middle;">

尝试在包含图像的div中使用此样式

<style="display: table-cell;
vertical-align: middle;">

链接到网站plz:,然后我可以看到问题。链接到网站plz:,然后我可以看到问题。有垂直对齐属性。有垂直对齐属性。