2

What I basically want to do

@gradientType: radial-gradient;

background: @gradientType(to bottom, ...)

I want to put a CSS Function, a gradient in this case (btw: are these called functions? I'm not sure) in a Less variable and call it later with the variable like @variable()?


Why? Maintainability!

I have a file variables.less where I want to change to gradient (ie) for a couple of places on the fly.

So instead of

.a { background: linear-gradient(..) }    
.b { background: linear-gradient(..) }    
.c { background: linear-gradient(..) }

I would have something like

.a { background: @myGradient(..) }    
.b { background: @myGradient(..) }    
.c { background: @myGradient(..) }

Linear gradients (linear-gradient(to bottom, red, blue)) and Radial gradients (radial-gradient(circle, red, blue)) kind of have the same syntax. So I would have four variables like gradientType, gradientOpts, gradientStart, gradientStop and change the gradient type based on the variable. This is just a simplified example.

4
  • There is a way but before answering I'd like to know the actual use-case just to see if there is a better option. What is the purpose behind putting this into a variable? Commented Dec 24, 2015 at 1:38
  • @Harry updated my question Commented Dec 24, 2015 at 1:43
  • Ok, I will give the solution in answer. But I still don't get it fully. You can't use the same syntax (or parameters) as a linear-gradient for a radial-gradient. The first param is different for the two. Commented Dec 24, 2015 at 1:44
  • 1
    not necessarily. linear-gradient(to bottom, red, blue) and radial-gradient(circle, red blue) have kinda the same syntax. So I would have four variables like gradientType, gradientOpts, gradientStart and gradientStop. It's a simplified example Commented Dec 24, 2015 at 1:47

1 Answer 1

3

To answer your question directly, you can use variable interpolation to achieve this.

@myGradient: radial-gradient;

.a { background: ~"@{myGradient}(to bottom, red, blue)"; }    
.b { background: ~"@{myGradient}(to bottom, green, yellow)"; }    
.c { background: ~"@{myGradient}(to bottom, orange, gold)"; }

But I would suggest using something like a parameterized mixin to handle things like this. Below is the code for a sample mixin which creates either a radial-gradient or linear-gradient depending on the input variable.

@gradType: linear-gradient;

.a { .background(@gradType; to bottom; red; blue); }    
.b { .background(@gradType; to bottom; green; yellow); }    
.c { .background(@gradType; to bottom; orange; gold); }

.background(@gradType; @gradOpts; @gradStart; @gradEnd){
  & when (@gradType = linear-gradient){
    background: linear-gradient(@gradOpts, @gradStart, @gradEnd);
  }
  & when (@gradType = radial-gradient){
    background: radial-gradient(@gradOpts, @gradStart, @gradEnd);
  }  
}

The catch with using guarded mixins (like the above) is that when the @gradType variable has a value outside of the two possible ones (say conical-gradient was the value) then the code would just fail and will not cause any errors to be thrown. This would be a problem when you're writing a library that is going to be used by others (and so want to throw an error when the value is invalid). In such cases, you could change the code like given below. When this approach is used, Less compiler would throw an error when an invalid input value is provided for the @gradType variable.

@gradType: linear-gradient;

.a { .background(@gradType; to bottom; red; blue); }    
.b { .background(@gradType; to bottom; green; yellow); }    
.c { .background(@gradType; to bottom; orange; gold); }

.background(linear-gradient; @gradOpts; @gradStart; @gradEnd){
  background: linear-gradient(@gradOpts, @gradStart, @gradEnd);
}
.background(radial-gradient; @gradOpts; @gradStart; @gradEnd){
  background: radial-gradient(@gradOpts, @gradStart, @gradEnd);
}  
Sign up to request clarification or add additional context in comments.

2 Comments

Just to clarify: I know a mixin would be an option too. I was more curios if this would work than actually using this. I just like to know some neat tips&tricks.. ;)
@Brettetete: No problems. I just wanted to make sure that you were choosing this option as an informed decision. Sometimes people who are using a language/tool newly would tend to do things the hard way. I would add a mixin sample also to the answer shortly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.