There is nice css trick to always position image in the center of div regardless of image size or aspect ratio.
<style>
.img_wrap {
padding-bottom: 56.25%;
width: 100%;
position: relative;
overflow: hidden;
}
#imgpreview {
display: block;
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
<div class="img_wrap">
<img id="imgpreview" src="http://i.imgur.com/RRUe0Mo.png" alt="your image" />
</div>
Then I added jquery code for rotating image
$(document).ready(function(){
var rotation = 0;
jQuery.fn.rotate = function(degrees) {
$(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
'-moz-transform' : 'rotate('+ degrees +'deg)',
'-ms-transform' : 'rotate('+ degrees +'deg)',
'transform' : 'rotate('+ degrees +'deg)'});
};
$('#imgpreview').click(function() {
rotation += 90;
$(this).rotate(rotation);
// $(this).toggleClass("imgpreview");
});
});
However, when I am rotation image, it gets cropped. I want to avoid that.
I tried to play with addClass feature but with no success. If someone could suggest any solution would appreciate a lot.
I have created jsfidlle for this question. Here the updated fiddle
overflow: hidden;from the .img-wrap and it will not cropimg_wraphas zero height - any height it has is from thepadding-bottom