Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESSCSS method with IE FIlter Alpha Opacity CSS

Tags:

I am using LESSCSS. I'm trying to create a method for opacity:

.opacity (@opacity)  {     opacity: @opacity;     -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=(@opacity * 100))";      filter: alpha(opacity = (@opacity * 100)); } 

So, If I call it using:

h1 {   .opacity(.5); } 

I want it to output:

h1 {   opacity: .5;   -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";   filter: alpha(opacity = 50); } 

But instead, LESS throws the error:

Expected '}' on line 30 in file '/Content/styles/style.less.css':  [29]:     -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=(@opacity * 100))";   [30]:     filter: alpha(opacity = (@opacity * 100));        ----^  [31]: } 

What am I doing wrong?

like image 261
Evan Larsen Avatar asked Feb 24 '12 19:02

Evan Larsen


People also ask

Does opacity work in IE?

Note: Alpha filters in IE accept values from 0 to 100 . The value 0 makes the element completely transparent (i.e. 100% transparent), whereas the value 100 makes the element completely opaque (i.e. 0% transparent).

What is filter alpha in CSS?

The Alpha Channel filter alters the opacity of the object, which makes it blend into the background.

What is the CSS property for changing the opacity to 0.5 in Internet Explorer 8 and earlier?

Answer: Use Microsoft "alpha filter" property.


2 Answers

In dotless, do this. (I would NOT recommend script tags - they are ugly, language specific and not supported by dotless).

.opacity (@opacity) {     @opacityPercentage: @opacity * 100;     opacity: @opacity;     -ms-filter: ~"progid:DXImageTransform.Microsoft.Alpha(opacity=(@{opacityPercentage}))";      filter: ~"alpha(opacity = (@{opacityPercentage}))"; } 

in dotless 1.2.3 (when it is released in a couple of weeks, or github head, you should be able to do this...

.opacity (@opacity) {     @opacityPercentage: @opacity * 100;     opacity: @opacity;     -ms-filter: progid:DXImageTransform.Microsoft.Alpha(opacity=(@opacityPercentage));      filter: alpha(opacity = (@opacityPercentage)); } 

and re: the comment from Mathletics, dotless is not "the worst compiler".. It matches less.js up to 1.1.5, soon to be 1.2.2 and many of the 600 bugs against less.js are fixed in dotless. You may have used dotless over 8 months ago, but things change and bugs are fixed... dotless also has better support for comments and variable scoping.

like image 199
Luke Page Avatar answered Sep 19 '22 07:09

Luke Page


You need to prefix the string with ~, like so:

-ms-filter: ~"progid:DXImageTransform.Microsoft.Alpha(opacity=50)"; 

From the docs: Escaping

UPDATE: you need to wrap the variable names in curly braces.

.opacity (@opacity) {     opacity: @opacity;     -ms-filter: ~`"progid:DXImageTransform.Microsoft.Alpha(opacity=(" + "@{opacity}" * 100 + "))"`;      filter: ~`"alpha(opacity = (" + "@{opacity}" * 100 + "))"`; } 

Here's what's happening: after the prefix, wrap the entire expression in backticks so that it is evaluated as JavaScript. Then you can add the result of the multiplication to the rest of the string; you also need to wrap the LESS variable in quotes and curly braces so the compiler can evaluate it before the multiplication.

This has been a cool question; I didn't actually know LESS could do this.

like image 26
Mathletics Avatar answered Sep 21 '22 07:09

Mathletics