必承其重 | 欲带皇冠 2011-09-01 16:25 采纳率: 0%
浏览 575
已采纳

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

Question

How can you align an image inside of a containing div?

Example

In my example, I need to vertically center the <img> in the <div> with class ="frame" :

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

.frame's height is fixed and image's height is unknown. I can add new elements in .frame if that's the only solution. I'm trying to do this on IE≥7, Webkit, Gecko.

See the jsfiddle here

.frame {
    height: 25px;      /* equals max 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;
}
        
<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>

</div>

转载于:https://stackoverflow.com/questions/7273338/how-to-vertically-align-an-image-inside-a-div

  • 写回答

30条回答 默认 最新

  • 程序go 2011-09-05 16:04
    关注

    The only (and the best cross-browser) way as I know is to use an inline-block helper with height: 100% and vertical-align: middle on both elements.

    So there is a solution: http://jsfiddle.net/kizu/4RPFa/4570/

    .frame {
        height: 25px;      /* equals max 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;
    }
    <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>

    Or, if you don't want to have an extra element in modern browsers and don't mind using IE expressions, you can use a pseudo-element and add it to IE using a convenient Expression, that runs only once per element, so there won't be any performance issues:

    The solution with :before and expression() for IE: http://jsfiddle.net/kizu/4RPFa/4571/

    .frame {
        height: 25px;      /* equals max 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)
        );
    }
    <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>

    How it works:

    1. When you have two inline-block elements near each other, you can align each to other's side, so with vertical-align: middle you'll get something like this:

      Two aligned blocks

    2. When you have a block with fixed height (in px, em or other absolute unit), you can set the height of inner blocks in %.

    3. So, adding one inline-block with height: 100% in a block with fixed height would align another inline-block element in it (<img/> in your case) vertically near it.
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作