如何在包含div内对齐图像?

实例

在我的示例中,我需要使用class=“frame”将<img>垂直居中放置在<div>中:

<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>

.frame的高度是固定的,图像的高度是未知的。如果这是唯一的解决方案,我可以在.frame中添加新元素。我正在尝试在Internet Explorer 7和更高版本WebKit、Gecko上执行此操作。

看到这里的jsfiddle。

.框架{高度:25px;/*等于最大图像高度*/线条高度:25px;宽度:160px;边框:1px纯红色;文本对齐:居中;边距:1em 0;}国际货币基金组织{背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px;}<div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=250/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=25/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=23/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=21/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=19/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=17/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=15/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=13/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=11/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=9/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=7/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=5/></div><div class=frame><img src=“http://jsfiddle.net/img/logo.png“高度=3/></div>


当前回答

最好的解决方案是

.block{
    /* Decor */
    padding:0 20px;
    background: #666;
    border: 2px solid #fff;
    text-align: center;
    /* Important */
    min-height: 220px;
    width: 260px;
    white-space: nowrap;
}
.block:after{
    content: '';
    display: inline-block;
    height: 220px; /* The same as min-height */
    width: 1px;
    overflow: hidden;
    margin: 0 0 0 -5px;
    vertical-align: middle;
}
.block span{
    vertical-align: middle;
    display: inline-block;
    white-space: normal;
}

其他回答

想要对齐文本/标题之后且两者都在div中的图像吗?

请参阅JSfiddle或运行代码段。

只需确保所有元素(div、img、title等)都有ID或类。

对于我来说,这个解决方案适用于所有浏览器(对于移动设备,您必须使用@media调整代码)。

h2.h2红色{颜色:红色;字体大小:14px;}.mydiv类{页边空白:30px;显示:块;}img.mydesired类{右边距:10px;显示:块;float:左;/*如果要将文本与同一行上的图像对齐*/宽度:100px;高度:100px;margin-top:-40px/*更改此值以适应页面*/;}<div class=“mydivclass”><br/><img class=“mydesiredclass”src=“https://upload.wikimedia.org/wikipedia/commons/thumb/b/b3/Wikipedia-logo-v2-en.svg/2000px-Wikipedia-logo-v2-en.svg.png"><h2 class=“h2red”>通过负操作img位置在div中的图像后对齐文本</h2></div>

只是为了这是一个有效的答案,我仍然希望再次发布jQuery解决方案。这适用于应用了v_align类的每个元素。它将在父元素中垂直居中,在调整窗口大小时将重新计算。

链接到JSFiddle

$(document).ready(function() {
  // Define the class that vertically aligns stuff
  var objects = '.v_align';
  // initial setup
  verticalAlign(objects);

  // Register resize event listener
  $(window).resize(function() {
    verticalAlign(objects);
  });
});

function verticalAlign(selector) {
  $(selector).each(function(val) {
    var parent_height = $(this).parent().height();
    var dif = parent_height - $(this).height()
    // Should only work if the parent is larger than the element
    var margin_top = dif > 0 ? dif/2 : 0;
    $(this).css("margin-top", margin_top + "px");
  });
}

三线解决方案:

position: relative;
top: 50%;
transform: translateY(-50%);

这适用于任何事情。

从这里开始。

使用纯CSS尝试此解决方案http://jsfiddle.net/sandeep/4RPFa/72/

也许这是HTML的主要问题。在HTML中定义类和图像高度时,不使用引号。

CSS:

.frame {
    height: 25px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    position: relative;
    margin: 1em 0;
    top: 50%;
    text-align: center;
    line-height: 24px;
    margin-bottom: 20px;
}

img {
    background: #3A6F9A;
    vertical-align: middle;
    line-height: 0;
    margin: 0 auto;
    max-height: 25px;
}

当我处理img标记时,它会从顶部留出3像素到2像素的空间。现在我降低了线的高度,它开始工作了。

CSS:

    .frame {
        height: 25px;      /* Equals maximum image height */
        width: 160px;
        border: 1px solid red;
        margin: 1em 0;
        text-align: center;
        line-height: 22px;
        *:first-child+html line-height:24px; /* For Internet Explorer 7 */
    }

    img {
        background: #3A6F9A;
        vertical-align: middle;
        line-height: 0;    
        max-height: 25px;
        max-width: 160px;
    }
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .frame {
        line-height:20px; /* WebKit browsers */
    }

线条高度属性在不同的浏览器中呈现不同。因此,我们必须定义不同的线宽属性浏览器。

检查此示例:http://jsfiddle.net/sandeep/4be8t/11/

查看以下关于不同浏览器中线条高度差异的示例:Firefox和Chrome中的输入高度差异

CSS网格

如果要在图像容器内垂直对齐单个图像,可以使用以下方法:

.img-container {
  display: grid;
}

img { 
  align-self: center;
}

.img容器{显示:栅格;栅格自动流动:列;背景:#BADA55;宽度:1200px;高度:500px;}img.垂直对齐{对齐自身:中心;}<div class=“img container”><img src=“https://picsum.photos/300/300" /><img class=“vertical align”src=“https://picsum.photos/300/300" /><img src=“https://picsum.photos/300/300" /></div>

如果要在图像容器中对齐多个图像,可以使用以下方法:

.img-container {
  display: grid;
  align-items: center;
}

.img容器{显示:栅格;栅格自动流动:列;对齐项目:居中;背景:#BADA55;宽度:1200px;高度:500px;}<div class=“img container”><img src=“https://picsum.photos/300/300" /><img src=“https://picsum.photos/300/300" /><img src=“https://picsum.photos/300/300" /></div>

请注意,我在这两种情况下都使用了grid auto-flow:column,因为否则元素会换行到指定显式网格项的行。在问题代码中,我也看到项目水平居中。在这种情况下,只需使用placeitems:center而不是alignitems:center。