3

I wanna center a image within a div that has overflow hidden

Like a frame and then picture no matter size are centered.

<div class="holder">
   <div id="frame">
      <img src="http://www.travelblog.org/Wallpaper/pix/tb_fiji_sunset_wallpaper.jpg" class="centerme"/>
   </div>
</div>​

See picture for more info: enter image description here

standard state is how it always gonna be with overflow:hidden;

but need it to always center even its a picture 3 times bigger than the frame

*heres the JSfiddle http://jsfiddle.net/UL3qp/4/

*EDIT
Last needed is to loop this incident

Simon Dragsbæk
  • 2,367
  • 3
  • 30
  • 53
  • everything i tried like absolute, and stuff dont really make any solution im out of idears thats why i ask, this is being used in a foreach so the backend gonna make it for over 150 items atm. atm i just got the standard css, 1 sec then im gonna make a jsfiddle – Simon Dragsbæk Aug 30 '12 at 09:19
  • a plain `text-align: center;` will do the trick: http://jsbin.com/ufozol/1/ – balexandre Aug 30 '12 at 09:23
  • no i know how to center a image im looking for a way to center it within a frame look at the jsfiddle – Simon Dragsbæk Aug 30 '12 at 09:28

3 Answers3

1

http://jsfiddle.net/HCabZ/2/ try out this for vertical align- center as well as right & left center

Chandrakant
  • 1,971
  • 1
  • 14
  • 33
1

Centering the image with jQuery is simple - via setting a positive or negative margin-left property:

$(document).ready(function(){
    var fwidth = $('#frame').width();
    var iwidth = $('#frame img').width();
    $('#frame img').css('margin-left', Math.floor((fwidth-iwidth)/2));
});

With only the pure CSS it can be done via a background image css property instead of using the img tag.

Community
  • 1
  • 1
Stano
  • 8,749
  • 6
  • 30
  • 44
0

Add the styles:

#frame{padding:15px; text-align:center}
#frame img{max-width:100%}

Check this fiddle http://jsfiddle.net/HCabZ/

Barrie Reader
  • 10,647
  • 11
  • 71
  • 139
Sowmya
  • 26,684
  • 21
  • 96
  • 136