Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate Sass variables

Tags:

sass

How can I concatenate a Sass variable?

This is the the code in the scss file.

$url = 'siteurl.com';  #some-div{     background-image: url(+ $url +/images/img.jpg); } 

I want the result in the CSS file to be:

#some-div{     background-image: url('siteurl.com/images/img.jpg'); } 

I found this question, but it didn't worked for me: Trying to concatenate Sass variable and a string

like image 370
DavSev Avatar asked Sep 19 '17 06:09

DavSev


People also ask

How do I concatenate in sass?

The single Sass string operator is concatenation. Strings can be concatenated (linked together) using the + operator. If you mix quoted and unquoted strings when concatenating, the result will be whichever is on the left side of the operator.

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

What is concatenating in SAS?

Concatenating combines two or more SAS data sets, one after the other, into a single SAS data set. You concatenate data sets by using either the SET statement in a DATA step or the APPEND procedure.


2 Answers

Multiple issues here, the correct syntax is

$url: 'siteurl.com';  #some-div{   background-image: url($url + '/images/img.jpg'); } 
  • When you assign a value to the variable, you need to use : and not =
  • You should quote the image path as it's a string.
  • Remove the stray + symbol before $url

You can see the above code in action at SassMeister

like image 166
Mr. Alien Avatar answered Oct 09 '22 13:10

Mr. Alien


Use this:

$domain: 'domain.com'; #some-div {     background-image: url('#{$domain}/images/img.jpg'); } 
like image 38
Dmitry Shashurov Avatar answered Oct 09 '22 12:10

Dmitry Shashurov