Using some jQuery, you can get the offset parent, subtract from the new rotated offset and use the margin to return it to the container. It works with all corners. Here is the script .
JS:
function rotate(elm, deg) { var offsetContLeft, offsetContTop, offsetLeft, offsetTop, newLeft, newTop; $(elm).css('transform', 'rotate('+ deg +'deg)'); // Get the container offset offsetContLeft = $(elm).parent().offset().left; offsetContTop= $(elm).parent().offset().top; // get the new rotated offset offsetLeft = $(elm).offset().left; offsetTop = $(elm).offset().top; // Subtract the two offsets. newLeft = offsetContLeft - offsetLeft; newTop = offsetContTop - offsetTop; // Apply the new offsets to the margin of the element. $(elm).css('margin-left', newLeft).css('margin-top', newTop); } $("#myrotate").click(function (e) { // pass in the element to rotate and the desired rotation. rotate('#myimage', 90); });
Lost left stack
source share