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


当前回答

对于更现代的解决方案,如果不需要支持旧版浏览器,您可以这样做:

.框架{显示:柔性;/**取消注释下面的“对齐内容”,使其水平居中。✪ 阅读以下内容,了解垂直和水平居中的更好方法。**//*对齐内容:中心*/对齐项目:居中;}国际货币基金组织{高度:自动;/**✪ 为了使该图像垂直和水平居中,在上面的.frame规则中,注释“justify content”和“对齐项目”声明,然后取消注释“margin:auto;”在下面**//*边距:自动*/}/*演示不需要的造型材料*/.框架{最大宽度:900px;高度:200px;边距:自动;背景:#222;}p型{最大宽度:900px;边距:20px自动0;}国际货币基金组织{宽度:150px;}<div class=“frame”><img src=“https://s3-us-west-2.amazonaws.com/s.cdpn.io/9988/hand-pointing.png"></div>

这是一支使用Flexbox的钢笔:http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e

编辑1/13/22

有一种更好的方法可以使用CSS网格和地方内容速记来实现这一点:

.frame文本网格{显示:栅格;放置内容:中心;/**✪ “放置内容”是“对齐内容”和“调整内容”的缩写。✪ “放置内容”速记需要两个值,第一个用于“对齐内容”,第二个用于“调整内容”。如果仅存在一个值(如本演示中所示),则该值将应用于两个方向。✪ 注释上面的“place content:center;”声明,以查看元素如何沿着容器的高度分布。**/}<div class=“ctnr框架文本网格”><h2>使用Grid和<code>放置内容</code></h2><p>垂直和水平居中只需要两条线</p></div>

这是一支使用CSS网格的钢笔:https://codepen.io/ricardozea/pen/c4e27f1e74542618d73e21f7c2276272?editors=0100

其他回答

matejkramny的解决方案是一个良好的开端,但超大图像的比例错误。

这是我的叉子:

演示:https://jsbin.com/lidebapomi/edit?html,css,输出


HTML格式:

<div class="frame">
  <img src="foo"/>
</div>

CSS:

.frame {
    height: 160px; /* Can be anything */
    width: 160px; /* Can be anything */
    position: relative;
}
img {
    max-height: 100%;
    max-width: 100%;
    width: auto;
    height: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

这样,您可以将图像垂直居中(演示):

div{
  height: 150px; // Internet Explorer 7 fix
  line-height: 150px;
}
img{
  vertical-align: middle;
  margin-bottom: 0.25em;
}

此外,您可以使用Flexbox获得正确的结果:

.父级{对齐项目:居中;/*用于垂直对齐*/背景:红色;显示:柔性;高度:250px;/*对齐内容:中心;<-用于水平对齐*/宽度:250px;}<div class=“parent”><img class=“child”src=“https://cdn2.iconfinder.com/data/icons/social-icons-circular-black/512/stackoverflow-128.png" /></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");
}

我在寻找一个类似的答案,一些建议将图像向左对齐,或者垂直比例挤压了图像,所以我想出了这个解决方案。。。它将内容垂直和水平居中。

 .div{
    position: relative;
    overflow: hidden;
 }
 .bantop img {
    position: absolute;
    margin: auto;
    width: 103%;
    height: auto;
    top: -50%;
    bottom: -50%;
    left: -50%;
    right: -50%;
  }

我在几个项目中使用它,它就像一个魅力。