0

is there a way that you can write a full CSS line using a LESS variable?

For example, if I declare a variable:

@dir: test;

and then want that variable to be used within another variable:

@bg-img: url(http://example.com/@dir/img/bg.png;

When I try to use @bg-img in my CSS, it compiles like this:

body{background-image:url(http://example.com/@dir/img/bg.png);

How do I get @dir to echo out as test?

I know that I can just replace @dir but since I have various different microsites, it'll be quicker to change the one instance of @dir rather than go through and change every instance of the microsite name.

1 Answer 1

2

This is possible by interpolation, but not certain if this would work in your particular situation without seeing more code. However, give this a try:

body{background-image:url(http://example.com/@{dir}/img/bg.png);

Found it in the docs, down the page under String interpolation. Hope this helps.

EDIT: and escape, just as @Rob W mentions in the comments. Also in the doc, just below interpolation.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly. Thank you very much! Will make this accepted answer ASAP!
@BeatAlex Still outputs as @{dir} to me in LESS 1.4. You need to escape the value, as @bg-img: ~"url(http://example.com/@{dir}/img/bg.png)".

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.