如何垂直对齐div内的图像?

Arn*_*anc 1371 css image vertical-alignment

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

在我的例子,我需要垂直居中的<img><div>class ="frame":

<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>
Run Code Online (Sandbox Code Playgroud)

.frame高度固定,图像高度未知..frame如果这是唯一的解决方案,我可以添加新元素.我试图在IE≥7,Webkit,Gecko上做到这一点.

这里查看jsfiddle

.frame {
    height: 25px;      /* Equals maximum image height */
    line-height: 25px;
    width: 160px;
    border: 1px solid red;

    text-align: center;
    margin: 1em 0;
}
img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}
Run Code Online (Sandbox Code Playgroud)
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=15 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=13 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=11 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=9 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=7 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=5 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=3 />
 </div>
Run Code Online (Sandbox Code Playgroud)

kiz*_*izu 2059

唯一的(和最好的跨浏览器)的方式,因为我知道是使用inline-block佣工height: 100%,并vertical-align: middle在这两个元素.

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

.frame {
    height: 25px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    white-space: nowrap; /* This is required unless you put the helper span closely near the img */

    text-align: center;
    margin: 1em 0;
}

.helper {
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="frame">
    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=250px />
</div>
<div class="frame">
    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25px />
</div>
<div class="frame">
    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23px />
</div>
<div class="frame">
    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21px />
</div>
<div class="frame">
    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=17px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=15px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=13px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=11px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=9px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=7px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=5px />
</div>
<div class="frame">
    <span class="helper"></span>
    <img src="http://jsfiddle.net/img/logo.png" height=3px />
</div>
Run Code Online (Sandbox Code Playgroud)

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

:beforeexpression()IE 的解决方案:http://jsfiddle.net/kizu/4RPFa/4571/

.frame {
    height: 25px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    white-space: nowrap;

    text-align: center;
    margin: 1em 0;
}

.frame:before,
.frame_before {
    content: "";
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}

/* Move this to conditional comments */
.frame {
    list-style:none;
    behavior: expression(
        function(t){
            t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');
            t.runtimeStyle.behavior = 'none';
        }(this)
    );
}
Run Code Online (Sandbox Code Playgroud)
<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" height=5px /></div>
<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=3px /></div>
Run Code Online (Sandbox Code Playgroud)


这个怎么运作:

  1. 当你有两个inline-block元素彼此靠近时,你可以将每个元素对齐到另一边,所以vertical-align: middle你会得到这样的东西:

    两个对齐的块

  2. 当你有固定的高度(在一个块中px,em或其他绝对单位),可以设置在内部块的高度%.

  3. 所以,加入一个inline-block具有height: 100%与固定高度块将对准另一inline-block它元素(<img/>你的情况),垂直接近它.

  • 请注意:`<img rel="nofollow noreferrer" src =""/>`不在添加的`<span> </ span>`助手中.它在外面.我只是因为没有意识到这一点而拉扯我的每一根头发. (103认同)
  • 看起来你还需要添加"white-space:nowrap;" 如果您的图像和辅助跨度之间存在换行符,则会出现问题.我花了一个小时为什么这个解决方案不适合我. (48认同)
  • 您可以自己看看:http://jsfiddle.net/kizu/Pw9NL/ - 只有内联框不会生成一个框,但所有其他情况都可以正常工作. (2认同)
  • 很好的答案。我在内联块元素(即.frame和.frame:before之间)之间的空间中挣扎。在此[http://css-tricks.com/fighting-the-space-between-inline-block-elements/)提出的解决方案是在'.frame'上添加'margin-left:-4px'。希望这可以帮助。 (2认同)

Tah*_*sin 497

这可能很有用:

div {
    position: relative;
    width: 200px;
    height: 200px;
}
img {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto;
}
.image {
    min-height: 50px
}
Run Code Online (Sandbox Code Playgroud)

参考:http://www.student.oulu.fi/~laurirai/www/css/middle/

  • 如果您还希望水平对齐图像,请添加左:0; 右:0; img (72认同)
  • 这很好,但在IE 10中不起作用:( (4认同)

jom*_*omo 458

matejkramny的解决方案是一个良好的开端,但超大的图像有一个错误的比例.
这是我的分叉:

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

预习


HTML:

<div class="frame">
  <img src="foo"/>
</div>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • @Justin完全有可能:http://jsbin.com/sejejemudi/2/edit(悬停) (3认同)

Jor*_*nel 246

3线解决方案:

position: relative;
top: 50%;
transform: translateY(-50%);
Run Code Online (Sandbox Code Playgroud)

这适用于任何事情.

这里开始.

  • 如果这个解决方案是跨浏览器的,那么它绝对应该位于顶部! (5认同)
  • IE9的前缀,但在IE8及更低版本中根本不起作用:http://caniuse.com/#search=transform但我的意见:我不关心IE8 (3认同)
  • 好的,当你不能使用`position:absolute;`因为你需要一个流动的宽度. (2认同)
  • 迄今为止最好的解决方案,甚至比flexbox更好,它支持大多数现代和旧浏览器。请支持这个 (2认同)

cod*_*nja 135

一个纯粹的CSS解决方案:

http://jsfiddle.net/3MVPM/

CSS:

.frame {
  margin: 1em 0;
  height: 35px;
  width: 160px;
  border: 1px solid red;
  position: relative;
}

img {
  max-height: 25px;
  max-width: 160px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  background: #3A6F9A;
}
Run Code Online (Sandbox Code Playgroud)

关键的东西

<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=15 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=13 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=11 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=9 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=7 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=5 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>
Run Code Online (Sandbox Code Playgroud)


Ric*_*Zea 116

对于那些登陆这篇文章并且对更现代的解决方案感兴趣并且不需要支持旧版浏览器的人,您可以这样做:

.frame {
    display: flex;
    /**
    Uncomment 'justify-content' below to center horizontally.
    ? Read below for a better way to center vertically and horizontally.
    **/

    /* justify-content: center; */
    align-items: center;
}

img {
    height: auto;

    /**
    ? To center this image both vertically and horizontally,
    in the .frame rule above comment the 'justify-content'
    and 'align-items' declarations,
    then  uncomment 'margin: auto;' below.
    **/

    /* margin: auto; */
}

/* Styling stuff not needed for demo */
.frame {
    max-width: 900px;
    height: 200px;
    margin: auto;
    background: #222;
}
p {
    max-width: 900px;
    margin: 20px auto 0;
}
img {
    width: 150px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="frame">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9988/hand-pointing.png">
</div>
Run Code Online (Sandbox Code Playgroud)

这是一支笔:http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e

  • 如果未设置容器的高度,这尤其有用 (3认同)

小智 101

这样您就可以垂直居中(演示):

div{
  height: 150px; // Internet Explorer 7 fix
  line-height: 150px;
}
img{
  vertical-align: middle;
  margin-bottom: 0.25em;
}
Run Code Online (Sandbox Code Playgroud)

  • 奇怪的是,在我的情况下,我无法得到可接受的答案(我还用溢出来剪切图像:隐藏).但这完全奏效了. (4认同)
  • 我只是在Chrome中对此进行了测试,但线高似乎是关键.我把vertical-align属性放在div而不是img上,因为我的img在一个锚点内(<a/>). (3认同)

tou*_*uet 36

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

.parent {
  align-items: center; /* For vertical align */
  background: red;
  display: flex;
  height: 250px;
  /* justify-content: center; <- for horizontal align */
  width: 250px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <img class="child" src="https://cdn2.iconfinder.com/data/icons/social-icons-circular-black/512/stackoverflow-128.png" />
</div>
Run Code Online (Sandbox Code Playgroud)


BBl*_*kwo 26

使用flexbox有一个超级简单的解决方案!

.frame {
    display: flex;
    align-items: center;
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,但你很难在所有的答案中看到它.我试图简单明了地突出它,因为它比所有其他答案更容易解决. (5认同)

Yeo*_*ave 22

您可以尝试将PI的CSS设置为 display: table-cell; vertical-align: middle;

  • 显示:如果要宽度为100%,则表格单元格的大小并不总是正确 (3认同)

San*_*lse 17

你可以尝试下面的代码

.frame{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>
Run Code Online (Sandbox Code Playgroud)


Man*_*mar 15

CSS 网格

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

.img-container {
  display: grid;
}

img { 
  align-self: center;
}
Run Code Online (Sandbox Code Playgroud)

.img-container {
  display: grid;
}

img { 
  align-self: center;
}
Run Code Online (Sandbox Code Playgroud)
.img-container {
  display: grid;
  grid-auto-flow: column; 
  background: #BADA55;
  width: 1200px;
  height: 500px;
}

img.vertical-align {
  align-self: center;
}
Run Code Online (Sandbox Code Playgroud)

如果你想在一个图像容器内对齐多个图像,你可以使用这个:

.img-container {
  display: grid;
  align-items: center;
}
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)
.img-container {
  display: grid;
  align-items: center;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我grid-auto-flow: column在这两种情况下都使用过,因为否则元素会通过指定显式网格项换行。在问题代码中,我也看到该项目水平居中。在这种情况下,只需使用place-items: center代替align-items: center


Ben*_*ate 14

背景图像解决方案 我一起删除了图像元素,并将其设置为div的背景.frame

http://jsfiddle.net/URVKa/2/

这至少适用于IE8,FF6和Chrome13

我检查过,这个解决方案无法缩小大于25px高度的图像.有一个属性叫做background-size设置元素的大小,但它是CSS3,它会与IE7要求冲突.

我建议您重做浏览器优先级并设计最佳浏览器,或者如果您想使用此解决方案,请获取一些服务器端代码来调整图像大小


Jos*_*kle 11

你可以这样做:

演示

http://jsfiddle.net/DZ8vW/1

CSS

.frame {
    height: 25px;      /* Equals maximum image height */
    line-height: 25px;
    width: 160px;
    border: 1px solid red;

    text-align: center; 
    margin: 1em 0;
    position: relative; /* Changes here... */
}
img {
    background: #3A6F9A;
    max-height: 25px;
    max-width: 160px;
    top: 50%;           /* Here.. */
    left: 50%;          /* Here... */
    position: absolute; /* And here */
}    
Run Code Online (Sandbox Code Playgroud)


JavaScript的

$("img").each(function(){
    this.style.marginTop = $(this).height() / -2 + "px";
})
Run Code Online (Sandbox Code Playgroud)


Web*_*ars 11

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;
}
Run Code Online (Sandbox Code Playgroud)

关键属性是display:table-cell; 对于.frame.Div.frame显示为与此内联,因此您需要将其包装在块元素中.

这适用于FF,Opera,Chrome,Safari和IE8 +.

UPDATE

对于IE7,我们需要添加一个css表达式:

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


san*_*eep 7

尝试使用纯css的这个解决方案http://jsfiddle.net/sandeep/4RPFa/72/ 可能是你的HTML的主要问题.在html中定义c lass&时,不要使用引号image height.

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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

当我使用img标签时,它会留下top空间line-height.现在我减少了line-height它的工作.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 */
    }
Run Code Online (Sandbox Code Playgroud)

line-height属性line-height在不同的浏览器中有所不同.所以; 我们必须定义不同的lass属性浏览器

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

请查看此示例,了解Firefox和Chromeimage height中不同浏览器输入高度差异的不同

  • 不起作用(至少在Firefox中).对于缺少的引号,这在HTML5中是允许的(不知道在jsfiddle中使用了哪种doctype) (2认同)

sla*_*ava 7

不确定IE,但在Firefox和Chrome下,如果你有img一个div容器,下面的CSS应该工作.至少对我来说效果很好:

div.img-container {
    display: table-cell;
    vertical-align: middle;
    height: 450px;
    width: 490px;
}

div.img-container img {
    max-height: 450px;
    max-width: 490px;
}
Run Code Online (Sandbox Code Playgroud)


ang*_*asS 6

这适用于现代浏览器(2016年编辑时),如此代码中的演示所示

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

为图像提供类或使用继承来定位需要居中的图像非常重要.在这个例子中,我们使用的.frame img {}是只有一个div包含的图像.frame才会被定位.

  • 即使在 Firefox 和 chrome 上,它似乎也没有正确对齐(http://jsfiddle.net/4RPFa/19/) (2认同)

Ily*_*nko 6

我的解决方案:http://jsfiddle.net/XNAj6/2/

<div class="container">
    <div class="frame">
        <img src="http://jsfiddle.net/img/logo.png" class="img" alt="" />
    </div>
</div>

.container {
    display: table;
    float: left;
    border: solid black 1px;
    margin: 2px;
    padding: 0;
    background-color: black;
    width: 150px;
    height: 150px;
}
.frame {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    border-width: 0;
}
.img {
    max-width: 150px;
    max-height: 150px;
    vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*Sam 6

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

有时它应该通过显示为table/table-cell来解决.例如快速标题屏幕.这也是W3的推荐方式.我建议您查看这个名为W3C.org 中心的链接

这里使用的提示是:

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

.container {
    position: absolute;
    display: table;
    width: 100%;
    height: 100%;
}
.content {
    display: table-cell;
    vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="content">
    <h1 style="text-align:center">Peace in the world</h1>
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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


DOC*_*TML 5

对我有用的简单方法:

img {
    vertical-align: middle;
    display: inline-block;
    position: relative;
}
Run Code Online (Sandbox Code Playgroud)

非常适合谷歌浏览器.在不同的浏览器上尝试这个.


jul*_*lez 5

为了使图像居中放置在容器(可能是徽标)中,除了像这样的一些文字:

在此处输入图片说明

基本上,您包装图像

.outer-frame {
  border: 1px solid red;
  min-height: 200px;
  text-align: center; /* Only to align horizontally */
}

.wrapper{
  line-height: 200px;
  border: 2px dashed blue;
  border-radius: 20px;
  margin: 50px
}

img {
  /* height: auto; */
  vertical-align: middle;   /* Only to align vertically */
}
Run Code Online (Sandbox Code Playgroud)
<div class="outer-frame">
  <div class="wrapper">
    some text
    <img src="http://via.placeholder.com/150x150">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


fdr*_*drv 5

想像你有

<div class="wrap">
    <img src="#">
</div>
Run Code Online (Sandbox Code Playgroud)

和CSS:

.wrap {
    display: flex;
}
.wrap img {
    object-fit: contain;
}
Run Code Online (Sandbox Code Playgroud)