How can I center a box of unknown width in CSS?

后端 未结 7 771
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 19:39

I have this html:

This is the caption &l
7条回答
  •  清歌不尽
    2020-12-13 20:05

    I had to do this for a jQuery photo gallery... What I ended up doing was when a photo was selected, the current photo would fade out and the new picture would be loaded, then calculate the difference of the width of half the container minus half the width of the photo. I would then set margin-left (and margin-top vertically) with that value.

        thewidth = $('#thephoto').width();
        theheight = $('#thephoto').height();
        $('#thephoto').css("margin-top",250-(theheight/2));
        $('#thephoto').css("margin-left",287.5-(thewidth/2));
        $('#thephoto').fadeIn(300);
    

    This is where my Flash background really came in handy :)

提交回复
热议问题