Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate image with CSS only?

I would like to rotate an image by 90 degrees with CSS only.

I can do the rotation, but then the position of the image is not what it should be. First, it will overlay some other elements in the same <div>. Second, its vertical dimension will become bigger than the containing <div>.

Here is my code where the two classes are defined:

.imagetest img {   transform: rotate(270deg);   -ms-transform: rotate(270deg);   -moz-transform: rotate(270deg);   -webkit-transform: rotate(270deg);   -o-transform: rotate(270deg);   width: 100%; }  .photo {   width: 95%;   padding: 0 15px;   margin: 0 0 10px 0;   float: left;   background: #828DAD; }
<article>   <section class="photo">     <div>Title</div>     <div class="imagetest">       <img src="https://picsum.photos/200/100"/>     </div>   </section> </article>

Is there a way of keeping the image within the section? I can translate and scale the image so that it is within the section, but that works only, if I know the image size beforehand. I would like to have a reliable method that does not depend on the size.

like image 470
v923z Avatar asked Dec 02 '12 21:12

v923z


People also ask

How do I rotate an image using CSS?

css file, stylesheet, or <style> tags, you can use the CSS class name in any of your image tags. To rotate an image by another measure of degrees, change the "180" in the CSS code and <img> tag to the degree you desire.

How do I rotate an object in CSS?

The rotate() CSS function defines a transformation that rotates an element around a fixed point on the 2D plane, without deforming it. Its result is a <transform-function> data type.

How do I rotate an image 90 degrees CSS?

90 degrees would equal to 100 gradient or 0.25 turns. Applying this property to the required element would rotate it by 90 degrees. Syntax: // Using CSS transform: rotate(90deg); // Using JavaScript element.


1 Answers

The trouble looks like the image isn't square and the browser adjusts as such. After rotation ensure the dimensions are retained by changing the image margin.

.imagetest img {   transform: rotate(270deg);   ...   margin: 10px 0px; } 

The amount will depend on the difference in height x width of the image. You may also need to add display:inline-block; or display:block to get it to recognize the margin parameter.

like image 184
James Bone Avatar answered Sep 20 '22 10:09

James Bone