Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertical align image inside div [duplicate]

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>
like image 583
Arnaud Le Blanc Avatar asked Sep 01 '11 16:09

Arnaud Le Blanc


People also ask

How do you Vertical-align an image in a div?

Answer: Use the CSS vertical-align Property You can align an image vertically center inside a <div> by using the CSS vertical-align property in combination with the display: table-cell; on the containing div element.

How do I vertically align an image in HTML?

To center an image vertically, you can wrap it in a block element like a div and use a combination of the CSS position property, the left and top properties, and the transform property.

How do I vertically align two items in a div?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.

How do I vertically align text in a div?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .


2 Answers

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.
like image 198
kizu Avatar answered Sep 29 '22 11:09

kizu


This might be useful:

div {     position: relative;     width: 200px;     height: 200px; } img {     position: absolute;     top: 0;     bottom: 0;     margin: auto; } .image {     min-height: 50px } 
like image 27
Tahir Yasin Avatar answered Sep 29 '22 09:09

Tahir Yasin