Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CSS3 attr() with transform rotate

I want to be able to rotate, in CSS via an attribute i.e.

<my-object data-angle="225"></my-object>

The CSS I have so far is

transform:rotate(attr(data-angle)deg);

But this throws an error, what is the correct syntax?

like image 273
Chris Avatar asked May 24 '16 08:05

Chris


People also ask

Can I use CSS transform rotate?

The transform property applies a 2D or 3D transformation to an element. This property allows you to rotate, scale, move, skew, etc., elements.

How do you rotate objects using css3?

CSS rotate() Function. The CSS rotate() function is used to rotate elements in a two-dimensional space. The rotate() function rotates an element based on the angle that you provide as an argument. You can provide the angle using any valid CSS angle value (i.e. in degrees, gradians, radians, or turns).

What is attr () in CSS?

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.

Which css3 transform property is used to rotate SVG objects in html5?

The rotate(<a> [<x> <y>]) transform function specifies a rotation by a degrees about a given point. If optional parameters x and y are not supplied, the rotation is about the origin of the current user coordinate system. If optional parameters x and y are supplied, the rotation is about the point (x, y) .


2 Answers

I'm not holding out any hope that they'll ever fully implement according to the standard that Asim points out, but the good news is that you can achieve nearly the same thing with Custom Properties aka CSS variables

There's a Javascript API for DOM elements to get and set these variables

el.style.setProperty('--foo', 'my custom property value')

or you can even set it directly in the HTML if you don't mind the inline style attribute:

<div style='--foo:"my custom prop val";'>

Here's an example (your mileage with this snippet may vary depending on your browser's support for custom properties):

:root {
  --rotation: 5deg;
}

.rotate {
  padding: 0.2em;
  transition: transform .2s;
  will-change: transform;
}

.rotate:hover {
  transform: rotate(var(--rotation));
}

.more {
  --rotation: 15deg;
}
<button class='rotate'>rotation</button>
<button class='rotate more'>more rotation</button>
<button class='rotate' style='--rotation: 30deg;'>yet more rotation</button>
like image 116
Jon z Avatar answered Sep 30 '22 15:09

Jon z


That's not possible in current browsers out there. But the spec says:

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the style sheet. It can be used on pseudo-elements too and, in this case, the value of the attribute on the pseudo-element's originated element is returned.

The attr() function can be used with any CSS property, but support for properties other than content is experimental.

So it will be supported in near future.

Here's the MDN doc.

like image 21
Asim K T Avatar answered Sep 30 '22 14:09

Asim K T