Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:34:34+00:00 2026-05-25T09:34:34+00:00

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

  • 0

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 the 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 Internet  Explorer 7 and later, WebKit, Gecko.

See the jsfiddle here.

.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;
}
<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:34:35+00:00Added an answer on May 25, 2026 at 9:34 am

    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 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;
    }
    <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 Internet Explorer expressions, you can use a pseudo-element and add it to Internet Explorer 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 Internet Explorer: 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)
        );
    }
    <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 another 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to center html content inside a div class=partners (top div with 2
How can I vertically align a Label and TextBlock at Top so that their
I can't seem to get this text to align vertically. Can someone please tell
I have three values I need to align in a dropdown box. How can
I am using Eclipse & JDE 4.5.0 plug-in. How to align fields vertically. Can
I'm trying to BOTTOM align an image in a fixed-height block: div { float:
I have a container div with another div centered inside with a background image.
I have the following html (inside ASP.NET Repeater): <div class=team_member> <a class=teamMemberLink href='<%# Profile.aspx?uID=
I need to know how to align an image in Matlab for further work.
How can I center an image horizontally and aligned to the bottom of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.