views:

852

answers:

4

If I have a fixed sized container div, and an unknown sized image, how do I horizontally and vertically center it?

  • using pure css
  • using JQuery if css can't do it

This answer makes sense for fixed width images, but not variable ones.

Something like this structure (I have in mind item renderers similar to these in the list, but where the image on the left wouldn't always be the same size:

<ul id="gallery">
    <li id="galleryItem1">
        <div class="imageContainer">
            <img src="gallery/image1"/>
        </div>
        <p>Some text to the right...</p>
        <!-- more stuff -->
    </li>
    <li id="galleryItem2">
    <!-- ... -->
</ul>

Thanks for the help!

+3  A: 

You could use background-position for that.

#your_div {
    background-position: center center;
    background-image: url('your_image.png');
}
WoLpH
Side note: If it is a PNG you're trying to center, make sure it's not transparent otherwise you might run into problems with IE6/etc. -- unless of course you use a jQuery plugin or something to fix the transparency problem.
Nicholas H
sorry, I probably didn't word my question correctly. The image would be one loaded into an img tag. I've updated the question. But this is useful, thanks!
viatropos
@Viatropos: in that case I recommend using the crossbrowser solution posted by edtsech
WoLpH
+1  A: 

If setting the image as a background image and centering it that way isn't an option, the jQuery to adapt the answer you linked for static images would go:

$(".fixed-div img.variable").each(function(){
  //get height and width (unitless) and divide by 2
  var hWide = ($(this).width())/2; //half the image's width
  var hTall = ($(this).height())/2; //half the image's height, etc.

  // attach negative and pixel for CSS rule
  hWide = '-' + hWide + 'px';
  hTall = '-' + hTall + 'px';

  $(this).addClass("js-fix").css({
    "margin-left" : hWide,
    "margin-top" : hTall
  });
});

assuming a CSS class defined as

.variable.js-fix {
  position:absolute;
  top:50%;
  left:50%;
}

with the fixed-width div having a height and position:relative declared.

[important js edit: switched '.style()' to '.css()']

D_N
A: 

Using display: table-cell trick for div

Working correctly in: Firefox, Safari, Opera, Chrome, IE8

CSS example:

div {
  width: 300px;
  height: 300px;
  border: 1px solid black;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
img {
  display: inline;
}

HTML example:

<div>
  <span></span>
  <img src="" alt="" />
</div>

Firefox example

edtsech
Note that IE has issues with that: http://www.quirksmode.org/css/display.html#t07 (in general--don't know about for this instance, have never tried).
D_N
I thought this was pure genius but there's one snag. The auto margins I set for the outer div go away, so I get a picture centered in a box, but the box is jammed in the upper left corner.
Anthony
I using this trick with IE and i try to find solution for IE at soon.
edtsech
+1  A: 

Crossbrowser solution

<style>
  .border {border: 1px solid black;} 
</style>
<div class="border" style="display: table; height: 400px; width: 400px; #position: relative; overflow: hidden;">
  <div class="border" style=" #position: absolute; #top: 50%;display: table-cell; text-align: center; vertical-align: middle;">
    <div class="border" style="width: 400px; #position: relative; #top: -50%">
      <img src="smurf.jpg" alt="" />
  </div>
</div>

Original solution for vertical div positioning

edtsech
What's with the # marks?
D_N
It is hack for IE
edtsech
... proper browsers will discard CSS properties if their names begin with an octothorpe. IE does not, however.
LeguRi