Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to vertically align an image inside a div

How to vertically align an image inside a div

October 8, 2021 by James Palmer

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;

}

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’,’‘);

t.runtimeStyle.behavior = ‘none’;

}(this)

);

}

How it works:

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:

When you have a block with fixed height (in px, em or another absolute unit), you can set the height of inner blocks in %.
So, adding one inline-block with height: 100% in a block with fixed height would align another inline-block element in it ( in your case) vertically near it.

This might be useful:
div {
position: relative;
width: 200px;
height: 200px;
}
img {
position: absolute;
top: 0;
bottom: 0;
margin: auto;
}
.image {
min-height: 50px
}

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in