Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple functions inside string with LESS

Tags:

css

less

I asked a similar question to this a while ago, and received a great answer. Unfortunately, this time round the answer is not sufficient, and the question is slightly more complex.

I am using LESS with the LESSHat mixins to build a theme at the moment. I have defined a number of colours as variables, and am currently trying to define a gradient using LESSHat's .gradient() mixin. The problem is that the mixin accepts a string as a single argument, rather than a series of arguments for each gradient parameter, for example:

#element {
    .gradient(~"linear-gradient(90deg, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%)");
}

All is well and good with the above, and I can use my variables inside the string using String Interpolation (i.e. @{color-var}). However, I would also like to run some functions on the variables, something like this:

.gradient(~"linear-gradient(top, @{green} 0%, @{green} 50%, darken(@green, 10%) 50%, darken(@green, 10%) 100%)");

The problem is that darken(@green, 10%) is never compiled, and some browsers simply interpret this color as green. Does anyone know the correct way to include the return of the darken() function inside the string above, without creating a separate variable for that?

For reference, I have tried the following to no avail:

.gradient(~"linear-gradient(top, @{green} 0%, @{green} 50%, "darken(@green, 10%)" 50%, "darken(@green, 10%)" 100%)");
.gradient(~"linear-gradient(top, @{green} 0%, @{green} 50%, {darken(@{green}, 10%)} 50%, {darken(@{green}, 10%)} 100%)");
like image 379
BenM Avatar asked Apr 16 '13 11:04

BenM


2 Answers

This should work, it's like your first approach but you should also include the ~:

.gradient(~"linear-gradient(top, @{green} 0%, @{green} 50%, " darken(@green, 10%) ~" 50%, " darken(@green, 10%) ~" 100%)");
like image 92
Lipis Avatar answered Oct 14 '22 02:10

Lipis


Have you tried saving the darkened color into a variable first? Like this:

@darkened-green: darken(@green, 10%);
.gradient(~"linear-gradient(top, @{green} 0%, @{green} 50%, @{darkened-green} 50%, @{darkened-green} 100%)");
like image 26
Hai Nguyen Avatar answered Oct 14 '22 03:10

Hai Nguyen