Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a picture

Tags:

html

jquery

css

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

like image 207
Simon Dragsbæk Avatar asked Jan 01 '26 20:01

Simon Dragsbæk


1 Answers

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.

like image 124
Stano Avatar answered Jan 03 '26 11:01

Stano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!