2

I have a few images on my site that will change over time. To be sure the CSS forces updates when I make changes, I have appended a query variable with a version string to them:

#branding{ background: url(../img/branding.jpg?v2); }

I'd like to be able to do this with LESS so I don't have to change all of the image references:

@mediaVersion: 101;
#branding{ background: url(../img/branding.jpg?v@mediaVersion); }

However Less.app can not recognize the variable @mediaVersion in that string and leaves it as written. I tried wrapping it in {@mediaVersion} and also (@mediaVersion) but those cause errors when compiling. I tried adding a semi colon after it.

How can I get this to work?

2
  • Question: What do you do if the browser caches the CSS file? Commented Feb 15, 2012 at 22:57
  • link rel="stylesheet" src="style.css?v2" Commented Feb 16, 2012 at 14:02

1 Answer 1

4

Try this:

@mediaVersion: 101;
#branding{ background: url("../img/branding.jpg?v@{mediaVersion}"); }

The section "String interpolation" at http://lesscss.org/#-variables shows that this should work.

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

2 Comments

perfect! not for others: i had to use both the {} AS WELL AS wrapping the inner contents of url() with the double quotes, just as you suggested.
How could you do this using variable math? E.g. @mobile: ~"(max-width: @{main} - 1)"; where I need the - 1 to work?

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.