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


当前回答

这适用于现代浏览器(编辑时为2016年),如codepen上的演示所示

.frame {
    height: 25px;
    line-height: 25px;
    width: 160px;
    border: 1px solid #83A7D3;          
}
.frame img {
    background: #3A6F9A;
    display:inline-block;
    vertical-align: middle;
}

给图像一个类或使用继承来以需要居中的图像为目标是非常重要的。在本例中,我们使用了.frame img{},因此只有由类为.frame的div包装的图像才会成为目标。

其他回答

我所知道的唯一(也是最好的跨浏览器)方法是在两个元素上使用高度为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/>)垂直靠近它。

我为这个老问题添加了一个新的解决方案,因为我发现答案中没有包含我的首选方法。

在每个项目中,在一开始,我创建了2个CSS类

.flex-centered {
    display: flex; 
    flex-direction-column; 
    justify-content:center
}
.abs-centered {
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%)
}

根据情况,您可以将两种元素置于中心。

flex centre对于页面上的图像和内容更加通用,abs居中需要一个相对的父级,这对居中的div很好,就像弹出窗口一样。

所以你只要打电话

   <div class='flex-centered'>
        <img>
    </div>

并且图像垂直居中。

.flex居中{显示:柔性;弯曲方向:柱;对齐内容:中心;}/*这也是水平居中*/.m0a时{边距:0自动}/*让一个大家长变成灰色*/.大div{高度:200px;宽度:200px;背景:#ccc;边界半径:4px;}/*制作一个小div以居中*/.small div{高度:20px;宽度:20px;背景:红色;}<div class=“flex center big div”><div class=“small div m0a”></div></div>

三线解决方案:

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

这适用于任何事情。

从这里开始。

使用表格和表格单元格的解决方案

有时应该通过显示为表格/表格单元格来解决。例如,快速标题屏幕。这也是W3推荐的方法。我建议您检查W3C.org中名为“块或图像居中”的链接。

此处使用的提示如下:

绝对定位容器显示为表格与显示为表格单元格的中心内容垂直对齐

.容器{位置:绝对;显示:表格;宽度:100%;高度:100%;}.内容{显示:表格单元格;垂直对齐:中间;}<div class=“container”><div class=“content”>世界和平</h1></div></div>

就我个人而言,我实际上不同意为此目的使用助手。

http://jsfiddle.net/MBs64/

.frame {
    height: 35px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    text-align: center;
    margin: 1em 0;
    display: table-cell;
    vertical-align: middle;
}
img {
    background: #3A6F9A;
    display: block;
    max-height: 35px;
    max-width: 160px;
}

关键属性是显示:表单元格;for.frame.Div.frame显示为与此内联,因此需要将其包装在块元素中。

这适用于Firefox、Opera、Chrome、Safari和Internet Explorer 8(及更高版本)。

更新

对于Internet Explorer 7,我们需要添加一个CSS表达式:

*:first-child+html img {
    position: relative;
    top: expression((this.parentNode.clientHeight-this.clientHeight)/2+"px");
}