Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate(90deg) not being applied using .transform in Chrome 37

I have some code that, until recently, worked on all browsers supporting CSS transforms. It broke in the newest Chrome (37). I found the issue. The transform from the computed style of an element is not accepted by other elements.

HTML

<div class="one">One</div>
<div class="two">Two</div>
<span></span>

CSS

div {
    width: 100px;
    height: 100px
}

.one {
    background-color: red;
    transform: rotate(90deg);
}

.two {
    background-color: blue
}

Javascript

var oneStyle = window.getComputedStyle(document.querySelector('.one'));
var oneTransform = oneStyle.transform;
document.querySelector('span').innerHTML = 'Tranform value is: ' + oneTransform;
var twoStyle = document.querySelector('.two').style;
twoStyle.transform = oneTransform;

Here is a Fiddle: http://jsfiddle.net/acbabis/0v8v2xd7/

The issue is that the second (blue) element does not rotate the same as the first (red) element is even though I told it to in the javascript.

This looks like a bug to me. Is it?

EDIT: My actual code was working in every browser but the newest Chrome, but it appears my sample code breaks in all browsers. I'd still like to understand why the above problem occurs.

EDIT 2: Got it to break in only Chrome 37 again. My guess is that it doesn't like the scientific notation; but then why would the computed style have it?

like image 408
aebabis Avatar asked Sep 05 '14 17:09

aebabis


People also ask

How do I rotate an image 180 degrees CSS?

Rotating an image using CSS Once the CSS code is applied to your . 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 a Div 90 degrees?

An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale and others to perform various kinds of transformation to elements. The rotate() transformation function can be used as the value to rotate the element.

How do you apply rotation in CSS?

rotate() 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 a div?

The rotate() method rotates an element clockwise or counter-clockwise. This a normal div element.


1 Answers

This is a fairly common problem, similar errors happen with older versions of Chrome and other vendors as well.

The usual fix is, as Hashem mentioned partly, to either change the rotation to something like 89.9deg or force GPU rendering by doing something like translateZ(1px) in addition to the rotation. Demo. In the future we can likely force this as well by using the will-change property

This is because browsers have trouble rendering certain things and rendering elements rotated exactly 90 degrees is one of those things. Sometimes they need a little help :)

like image 190
Zach Saucier Avatar answered Sep 18 '22 16:09

Zach Saucier