Css 垂直对齐固定大小div中的中心图像

Css 垂直对齐固定大小div中的中心图像,css,image,position,vertical-alignment,Css,Image,Position,Vertical Alignment,我有一个div是145px 145px。我在潜水舱里有一个img。img可以是任何尺寸(最长侧为130px)。我希望图像在div中垂直居中。我尝试过的所有方法都适用于大多数浏览器,但不适用于IE7。我需要一些能在IE7中工作的东西。不确定到目前为止您尝试了什么,但是如果图像显示为内联元素,垂直对齐CSS属性应该可以工作 有关垂直对齐的一些信息: 如果必须考虑所有图像高度,那么这几乎是不使用JavaScript的唯一方法 如果图像不是内联元素,并且只需容纳高度一致的图像,则可以执行以下操作: im

我有一个div是145px 145px。我在潜水舱里有一个img。img可以是任何尺寸(最长侧为130px)。我希望图像在div中垂直居中。我尝试过的所有方法都适用于大多数浏览器,但不适用于IE7。我需要一些能在IE7中工作的东西。

不确定到目前为止您尝试了什么,但是如果图像显示为内联元素,垂直对齐CSS属性应该可以工作

有关垂直对齐的一些信息:

如果必须考虑所有图像高度,那么这几乎是不使用JavaScript的唯一方法

如果图像不是内联元素,并且只需容纳高度一致的图像,则可以执行以下操作:

img {
    margin-top: -50px; /* This number should be half the height of your image */
    position: absolute;
        top: 50%;
}

否则,我认为容纳不同高度的图像的唯一方法是对CSS执行类似的操作,但使用JS将负边距设置为图像高度的一半。

您可以用div上的背景替换图像,如下所示:

<div style="background:url(myimage.jpg) no-repeat center center"></div>

不太清楚IE7,但对于IE9和其他现代浏览器,以下是一些工作

    .picturecontainer{
        width:800px;
        height:800px;
        border:solid 1px;
        display:table-cell;
        vertical-align:middle;

    }
    .horizontalcenter{
        display:block;
        margin-left:auto;
        margin-right:auto;
        vertical-align:middle;
    }
使用它

<div class="picturecontainer"><img src="" class="horizontalcenter"/></div>


这会将图像置于死角。

我创建了一个小jQuery代码来实现这一点,而不必使用讨厌的表:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
imagepos = function () {
    $('img').each(function () {  
            imgheight = Math.round($(this).height() / 2);
            imgwidth = Math.round($(this).width() / 2);    
            $(this).attr("style", "margin-top: -" + imgheight + "px; margin-left: -" + imgwidth + "px; opacity:1;");
        });
    }   
$(window).load(imagepos);
</script>

以下是一个跨浏览器解决方案:

<div class="img-container"><img src="picture.png" class="cropped"/></div>


div.img-container {
     width: 390px;
     height: 211px;
     position: relative;
     margin-left: auto;
     margin-right: auto;
     overflow: hidden;
 }
img.cropped {
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

分区img-集装箱{
宽度:390px;
身高:211px;
位置:相对位置;
左边距:自动;
右边距:自动;
溢出:隐藏;
}
修剪{
位置:绝对位置;
保证金:自动;
排名:0;
左:0;
右:0;
底部:0;
}

使用
行高
属性为我解决了这个问题

参考:

HTML:

<div class="img_thumb">
    <a class="images_class" href="large.jpg" rel="images"><img src="http://www.minfo.pt/fotos/_cache/produtos/0/068.M.976010002__thumbnail_50_50_true_sharpen_1_1.JPG" title="img_title" alt="img_alt" /></a>
</div>

我也有类似的要求。找到了解决方案。但正如他所说,图像“img可以是任何大小”。所以-50的值只适用于高100像素的图像。因此,我不知道为什么这个答案有这么多的投票,但它不能解决OP的问题?是的,我的主要答案是不同大小的图像,但我也提供了一些其他选项的细节。你读了我的全部答案还是代码?不管怎样,这仍然是我首选的解决方案。这个解决方案太棒了。我搜索了整个网络,table cell和flexbox这两种常见的解决方案对我来说都不起作用,但这一个非常有效。除了chrome之外,我没有在任何浏览器中测试过它,但它在那里工作得很好……当图像高度大于容器时,这似乎不起作用。非常有效,谢谢!我可以添加img.crapped{max height:100%;max width:100%;}用于比容器大的图像这真是天才!真不敢相信这个应用还不广泛。
<div class="img_thumb">
    <a class="images_class" href="large.jpg" rel="images"><img src="http://www.minfo.pt/fotos/_cache/produtos/0/068.M.976010002__thumbnail_50_50_true_sharpen_1_1.JPG" title="img_title" alt="img_alt" /></a>
</div>
.img_thumb {
    float: left;
    height: 120px;
    margin-bottom: 5px;
    margin-left: 9px;
    position: relative;
    width: 147px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 3px;
    line-height:120px;
    text-align:center;
}

.img_thumb img {
    vertical-align: middle;
}