Using css to center an image with position:absolute

I ran into an interesting problem yesterday while building a simple web page. I needed to cause the main image at the top of the page to do two things: 1) Be centered on a liquid layout page, so that no matter what size the window the image was centered. 2) "Float" above the design on it’s own z-index.

Of course to get the image to be on it’s own layer, it must be set to position:absolute; but once you give it an absolute position it loses all references to being centered.

I thought I had the solution at one point, until I tested it in IE and it was all wrong. After a few hours of struggling, I finally came up with what now seems like a simple solution:

Adding styles to the image itself was my mistake. In this case I had to wrap the image with a div and give it an id. I then applied the following styles to that div in my stylesheet:

#image {
  position:absolute;
  width: 100%;
  text-align: center;
}

The image itself doesn’t need any other styles.

Bookmark and Share

13 Responses  |  add yours »

13 Responses to “Using css to center an image with position:absolute”

  1. joe says:

    I think you could have alternatively used
    margin: 0 auto;
    not sure if it can be applied directly to an image or not

  2. Jason says:

    Thanks for the suggestion Joe. I did try that. It didn’t work.

  3. Ros says:

    hi i’m having the same problem but im not familiar with that block of code, could you write it out in full. not your whole page, but the DIV wrapping etc. whatever makes it work. thanks,
    Ros

  4. David says:

    Thanks, you solved my search!

  5. Rachel says:

    Thanks so much – I’ve been wrestling with this for a couple of days, off & on. I know I knew this a few months ago – AH, THE RELIEF OF SOLVING THE MYSTERY!!!! Thanks.

  6. Sawyer says:

    Awesome! Solved my problem, thanks so much!

  7. Peter says:

    Dude, I ow you a beer. If you ever visit belgium.

  8. Jabbadoo says:

    thanks

  9. SCat says:

    yo .. thanks you helped me out with that one!

  10. honey says:

    thanks for the tip. It worked like a charm!

  11. Memfis says:

    Amazing, this has been baffling me for so long! Thank you!

  12. thank you so much! just spent a half hour trying to figure this one out and finally gave up and searched … nifty solution!

  13. This worked fine on Firefox and IE 8, but in order to make it work on earlier IE versions I needed to add a magic “clearfix” div right after the div containing the centered image.

    /* slightly enhanced, universal clearfix hack */
    .clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: ” “;
    clear: both;
    height: 0;
    }
    .clearfix { display: inline-block; }
    /* start commented backslash hack \*/
    * html .clearfix { height: 1%; }
    .clearfix { display: block; }
    /* close commented backslash hack */

    For the origins of clearfix and pointer to newer stuff, see
    http://www.positioniseverything.net/easyclearing.html

    Donna

Leave a Reply

(if you want a pic to show with your comment, go get a gravatar!)

Search