12

I'm creating some icon font rules for using in my site. Using Sass I wanted to list all the icons in a list variable and use @each to loop through them all.

Code looks like this:

$icons: 
    wifi 600,
    wifi-hotspot 601,
    weather 602;

@each $icon in $icons {
    .icon-#{nth($icon, 1)}, 
    %icon-#{nth($icon, 1)} {
        content: "\#{nth($icon, 2)}";
    }
}

The problem is the backslash on the content: line. I need it for the character encoding, but it escapes the variable interpolation, outputting CSS that looks like this:

.icon-wifi {
  content: "\#{nth($icon, 2)}";
}

Adding one more backslash like this: content: "\\#{nth($icon, 2)}"; outputs this CSS:

.icon-wifi {
  content: "\\600";
}

Is there a way to get the Sass to output CSS with only a single backslash while keeping the variable interpolation?

2

6 Answers 6

13

I got this to work by messing with the interpolation

sassmesiter demo

// ----
// Sass (v3.4.21)
// Compass (v1.0.3)
// ----

$icons: 
    wifi 600,
    wifi-hotspot 601,
    weather 602;

@each $icon in $icons {
    .icon-#{nth($icon, 1)}, 
    %icon-#{nth($icon, 1)} {
        content: #{'"\\' + nth($icon, 2) + '"'}; // <------ See this line
    }
}

compiles to

.icon-wifi {
  content: "\600";
}

.icon-wifi-hotspot {
  content: "\601";
}

.icon-weather {
  content: "\602";
}
2
  • Best solution for me !
    – Loenix
    Aug 2, 2019 at 15:18
  • This worked for me well :) content: #{'"\\' + $unicode + '"'} May 1, 2020 at 15:45
7

If you include the backslash in the actual variable, then when the sass generates the css, it will actually generate the calculated unicode character instead of outputting the unicode in the css output. This still usually works but it's hard to debug if something is going wrong and it is a bit more prone to cause issues in the browser in rendering the icon.

To output the actual unicode in the generated CSS, you can do this:

@function icon($character){
    @return unquote('\"') + unquote(str-insert($character,'\\', 1)) + unquote('\"');
}

$icon-thing: "e60f";

.icon-thing:before {
    content: icon($icon-thing); //outputs content: "\e60f";
}
1
  • 1
    There's an extra closing bracket in your function Mar 19, 2017 at 10:37
3

You can add the backslash to the parameter in the $icons variable. That is,

$icons: wifi "\600", wifi-hotspot "\601", weather "\602";

@each $icon in $icons {
  .icon-#{nth($icon, 1)}, %icon-#{nth($icon, 1)} {
    content: "#{nth($icon, 2)}";
  }
}

Generated CSS:

.icon-wifi {
  content: "\600"; 
}

.icon-wifi-hotspot {
  content: "\601"; 
}

.icon-weather {
  content: "\602"; 
}   
9
  • Thanks! That worked. But I do wonder if there is a character sequence or function that would allow the backslash to be outside the $icons variable.
    – Ben
    Feb 6, 2014 at 17:03
  • @Ben Do you actually need to have the backslash separate?
    – cimmanon
    Feb 6, 2014 at 19:57
  • @cimmanon No, the answer provided a solution to my problem. I'm just wondering regarding the technical aspect. Is there a way to output a backslash immediately before and interpolated sass variable?
    – Ben
    Feb 6, 2014 at 20:37
  • 3
    @Ben in Sass 3.3 it's possible to overcome this problem with slicing the backslash from a string, and then combining it with any code like so str-slice("\x",1,1) + $code. See my answer here: stackoverflow.com/questions/21641833/… Feb 8, 2014 at 10:43
  • 1
    Darn, doesn't look like this works anymore. I'm getting generated CSS with strange characters instead of backslashes. Jan 27, 2015 at 23:11
1

Use unquote and double slash

$var:123 → content:"\e123"

 content:#{unquote('\"')+("\\")+("e")+$var+unquote('\"')};
0

If you are using Gulp to compile your Sass files, installing this Gulp plugin is probably the easiest way to get around the issue:

https://www.npmjs.com/package/gulp-sass-unicode

var sass = require('gulp-sass');
var sassUnicode = require('gulp-sass-unicode');

gulp.task('sass', function(){
  gulp.src('style.scss')
    .pipe(sass())
    .pipe(sassUnicode()) // <-- This is the bit that does the magic
    .pipe(gulp.dest( "css/" ));
});

There is no need to make any code alterations in your Sass files. Write out your Sass code how you want and the unicode characters are decoded back into regular escaped strings in the output CSS automatically.

Input SCSS

$testContent: "\f26e";

#test {
  content:  $testContent;
}

Output CSS

#test {
  content: "\f26e";
}
0

Unfortunately, these solutions were not entirely working for me but I was finally able to get it working with SASS maps

//node-sass 4.11.0
//libsass 3.5.4

$hexes: (
           checkmark: \2714
        );

@function out-content($var) {
    @return unquote("\"#{ $var }\""); 
}

@each $mod, $code in $hexes {    
    .#{$mod}-after {
        &:after {
            content: out-content($code);
        }
    }
}

//output
//.checkmark-after:after {
    //content: "\2714";
//}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.