Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position a rotated image within a container?

Using CSS3, HTML (and javascript/jquery if needed), I need to rotate an image 90/270 degrees and have it position to fill its parent div/container. Sounds simple, but when images are rotated, there positioning changes and I can't figure out how or why.

Here is a jsFiddle to explain - http://jsfiddle.net/UPGkU/2/ - I just want the blue logo to be position exactly within the red div.

Of course, I could use specific offsets, but if a different image is used, those offsets change, so I really want to find a generic solution.

Any help would be fantastic, thanks!

like image 659
Tom Carnell Avatar asked Sep 11 '12 09:09

Tom Carnell


People also ask

Which command is used to rotate the position of an image?

Answer. Answer: Rotate tool is used to rotate the position of a image.

How do you rotate an image in Imagej?

A couple of options: Image > Transform > Rotate 90 Degrees Right ( do this twice) Image > Transform > Rotate... and set 180. Image > Transform > Flip Vertical (not quite you asked for but may suffice)

How do you rotate an image at an angle?

Manually rotate a picture or shape Select the picture or shape. Manually rotate the text box by selecting the shape or picture rotation handle and dragging in the direction you want. To keep the rotation to 15 degree angles, press and hold Shift while you drag the rotation handle.


2 Answers

You need to set a transform-origin - in this scenario, a point around which the image gets rotated.

#image {
  -webkit-transform: rotate(90deg);
  -webkit-transform-origin: 0 0; //initially 50% 50%
  margin-left: 100%;
}

90deg fiddle / 270deg fiddle

Update:

The challenge with the latter is that we can't really modify transform-origin as its position is relative to the not yet transformed element and we can't just set margin-top:100% since margin values (even vertical ones) are calculated as a percentage always relative to the width of the containing block. The following code should work:

#image {
  -webkit-transform: rotate(-90deg);
  -webkit-transform-origin: 0 0;
  position:relative;
  top:100%;
}
like image 175
Oleg Avatar answered Oct 08 '22 09:10

Oleg


try with

#image {
    -webkit-transform: rotate(90deg);
    -webkit-transform-origin: 0 100%;
    position : relative;
    top      : -50px;
}

like image 26
Fabrizio Calderan Avatar answered Oct 08 '22 07:10

Fabrizio Calderan