Skip navigation
alexr62
Currently Being Moderated

Vertically centering an image within a floated element

May 28, 2012 5:53 PM

I'm trying to vertically center an image within a left-floated div. The image is horizantally centered using the div's text-align property. I've gone to several help websites that say to set the vertical-align property of the image to "middle," but that doesn't seem to work. Is the div's float property affecting anything?

 

I don't think I can use absolute positioning or any other method that uses an exact margin based on the height of the image. I'm trying to create an image gallery that replaces the image when a thumbnail is clicked, and since there are portrait and landscape images in the gallery, the solution has to work for multiple image sizes.

 

Here's the HTML for the element:

 

<div id="imagewrap">
    <img src="/GlennThomas/images/graphics/black.gif" />
  </div>

 

Here's the CSS:

 

 

#imagewrap {
    width: 720px;
    height: 695px;
    float: left;
    clear: right;
    margin-left: 10px;
    position: relative;
    text-align: center;
}
#imagewrap img {
    width: 710px;
    vertical-align: middle;
}

 

Thanks,

Alex

 
Replies
  • Currently Being Moderated
    May 28, 2012 6:08 PM   in reply to alexr62

    Try this:

     

    #imagewrap {
        width: 720px;
        float: left;
        margin-left: 10px;

              border: 1px dotted red; /**for demo purposes only, you may omit this**/

    }


    #imagewrap img {
        width: 710px;
        margin-top: 20%; /*adjust margins to suit*/

              margin-left: auto;

              margin-right:auto;

    }

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    May 29, 2012 8:54 AM   in reply to alexr62

    Image margins are calculated from containing div -- imagewrap.

     

    Portrait and landscape photos are challenging because aspect ratios are so different.

    Do you think I need to use javascript to do this?

     

    No.  To further refine image styles, use 2 classes: .one for portrait; one for .landscape.

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    May 29, 2012 2:19 PM   in reply to alexr62

    Without a link, I'm not sure I understand what you're trying to do or why vertical centering is so important.

     

    Vertical centering is mainly a print concept that doesn't translate well to the web.  On the web, everyone uses different sized devices & viewports. Absolute vertical is impossible to calculate without hard and fast numbers.  If it's impossible to calculate, why spend time worrying about it?

     

    As food for thought, my CSS Image Viewer Demo below has a self-adjusting container.  No vertical centering required on images.

    http://alt-web.com/DEMOS/CSS-Image-Viewer.shtml

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    May 29, 2012 2:24 PM   in reply to Nancy O.

    The best way to vertically center is to indeed use the display:table-cell approach, since content in a table cell is always vertically centered by default.  To use that approach you have to be willing to take the compromises that go along with it.  And I believe that one would not refer to this approach as an inline centering method, so I'm not sure how applicable your help sources are (@alexr62).

     
    |
    Mark as:
  • Currently Being Moderated
    May 30, 2012 12:32 AM   in reply to alexr62

    alexr62 wrote:

     

    Can you explain why the table-cell method isn't working? Are you implying that using it with a floated element is one of the compromises?

     

    You can't use float directly on the <div> where you require an element vertically centered in conjuction with display: table-cell; What you can do is float a wrapper container (see below).

     

    This technique works in IE8 and above.

     

     

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Untitled Document</title>

    <style type="text/css">

    #imageWrapContainer {

        float: left;

        width: 720px;

        margin-left: 10px;

    }

    #imagewrap {

    display: table-cell;

    width: 720px;

    height: 695px;

    vertical-align: middle;

    border: 1px solid #930;

    text-align: center;

    }

     

    </style>

    </head>

     

    <body>

    <div id="imageWrapContainer">

    <div id="imagewrap">

    <img src="photo.jpg" width="400" height="300" alt="" />

    </div><!-- end imageWrapper -->

    </div><!-- end imageWrapper Container -->

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 1:27 AM   in reply to alexr62

    Youre missing a closing } bracket after the '#container' css which is stopping the css cascade.

     

    PLUS vertical-align: center; below should be vertical-align: middle;

     

     

    #imagewrap div {

        width: 720px;

        height: 695px;

        display: table-cell;

        vertical-align: center;

        text-align: center;

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points