html - 需要在 CSS 中居中对齐图像

标签 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;" 
        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 但它没有用。

最佳答案

遇到了this post它对我有用:

div{
    position: relative;
}

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

}

(垂直和水平对齐)

关于html - 需要在 CSS 中居中对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2370175/

相关文章:

php - simplexml_load_file 无法识别 <dc :title> tags

javascript - 导航栏下方的搜索文本框

html - 滚动时我的 div 与标题重叠

c# - 如何通过 WPF 动画使图像变大(产生选择的错觉)?

html - 左对齐的嵌套列表 "bullets"

HTML 如何获得图像的完整底部边框,我可以在其中放置文本

html - CSS 将鼠标悬停在某物上并让完全不同的 div 更改其属性

jquery - 我可以使用 CSS 创建保持内容纵横比的全 Angular slider 吗?

javascript - 如何获取网站的所有src和href属性

c# - 如何检查 Logo /图像是否在图像中?