如何在包含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>


当前回答

除了像这样的文本外,还可以将图像居中放置在容器内(可以是徽标):

基本上,您可以包装图像

.外框{边框:1px纯红色;最小高度:200px;文本对齐:居中;/*仅水平对齐*/}.包装器{线条高度:200px;边框:2px蓝色虚线;边框半径:20px;边距:50px}国际货币基金组织{/*高度:自动*/垂直对齐:中间;/*仅垂直对齐*/}<div class=“外框”><div class=“wrapper”>一些文本<img src=“http://via.placeholder.com/150x150"></div></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");
  });
}

使用纯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中的输入高度差异

我所知道的唯一(也是最好的跨浏览器)方法是在两个元素上使用高度为100%、垂直对齐为中间的内联块帮助器。

因此有一个解决方案:http://jsfiddle.net/kizu/4RPFa/4570/

.框架{高度:25px;/*等于最大图像高度*/宽度:160px;边框:1px纯红色;空白:nowrap;/*这是必需的,除非您将helper span放在img附近*/文本对齐:居中;边距:1em 0;}.helper{显示:内联块;高度:100%;垂直对齐:中间;}国际货币基金组织{背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px;}<div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=250px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=25px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=23px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=21px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=19px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=17px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=15px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=13px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=11px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=9px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=7px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=5px/></div><div class=“frame”><span class=“helper”></span><img src=“http://jsfiddle.net/img/logo.png“高度=3px/></div>

或者,如果您不想在现代浏览器中使用额外的元素,也不介意使用Internet Explorer表达式,您可以使用伪元素,并使用方便的表达式将其添加到Internet Explorer中,每个元素只运行一次,这样就不会有任何性能问题:

Internet Explorer的解决方案包括:before和expression():http://jsfiddle.net/kizu/4RPFa/4571/

.框架{高度:25px;/*等于最大图像高度*/宽度:160px;边框:1px纯红色;空白:nowrap;文本对齐:居中;边距:1em 0;}.frame:之前,.frame_前面{内容:“”;显示:内联块;高度:100%;垂直对齐:中间;}国际货币基金组织{背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px;}/*将此移至条件注释*/.框架{列表样式:无;行为:表达式(函数(t){t.insertAdjacentHTML('afterBegin','<span class=“frame_before”></span>');t.runtimeStyle.behavior='none';}(本));}<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=250px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=25px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=23px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=21px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=19px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=17px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=15px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=13px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=11px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=9px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“height=7px/></div>”<div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“高度=5px/></div><div class=“frame”><img src=“http://jsfiddle.net/img/logo.png“高度=3px/></div>


工作原理:

当两个内联块元素彼此靠近时,可以将每个元素对齐到另一侧,因此使用垂直对齐:中间,您将得到如下结果:当您有一个固定高度的块(以像素、em或其他绝对单位表示)时,可以将内部块的高度设置为%。因此,在具有固定高度的块中添加一个高度为100%的内联块将使其中的另一个内联块元素(在您的情况下是<img/>)垂直靠近它。

这可能很有用:

div {
    position: relative;
    width: 200px;
    height: 200px;
}
img {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto;
}
.image {
    min-height: 50px
}

除了像这样的文本外,还可以将图像居中放置在容器内(可以是徽标):

基本上,您可以包装图像

.外框{边框:1px纯红色;最小高度:200px;文本对齐:居中;/*仅水平对齐*/}.包装器{线条高度:200px;边框:2px蓝色虚线;边框半径:20px;边距:50px}国际货币基金组织{/*高度:自动*/垂直对齐:中间;/*仅垂直对齐*/}<div class=“外框”><div class=“wrapper”>一些文本<img src=“http://via.placeholder.com/150x150"></div></div>