官术网_书友最值得收藏!

Interpolation of variables

Variable interpolation means the ability to build a string by using the values assigned to variables. In Sass, you can use variable interpolation to dynamically create property and selectors' names. In this recipe, you will learn how to apply variable interpolation in your code.

Getting ready

You can test variable interpolation in Sass with the Ruby Sass compiler. The Installing Sass for command line usage recipe of Chapter 1, Getting Started with Sass, describes how to install Ruby Sass on your system.

How to do it...

Perform the steps beneath to learn how to use variable interpolation in Sass to dynamically create property and selector names:

  1. Write down the Sass code as follows in a Sass template called interpolation.scss:
    $name: class; 
    $direction: left; 
    $units: px; 
    
    .#{$name} { 
      margin-#{$direction}: 20#{$units}; 
    }
  2. Compile the Sass template from the previous step with Ruby Sass by running the following command in your console:
     sass interpolation.scss
    
  3. The compiled CSS code from the first three steps should look like that shown here:
    .class { 
      margin-left: 20px; 

How it works...

In Sass, you can use string interpolation by using the #{} interpolation syntax. The syntax enables you to use SassScript variables in selectors' and property names and values.

SassScript supports quoted and unquoted strings. In SassScript, quoted strings are compiled into quoted strings. But when you're using quoted strings for interpolation, the value is unquoted first. Consider the following SCSS code:

// scss-lint:disable PropertySortOrder, PropertySpelling 
$value1: unquoted; 
$value2: 'quoted'; 

test { 
  p1: $value1; 
  p2: $value2; 
  p3: $value1 #{$value2}; 
  p4: $value1#{$value2}; 
  p5: $value1 + #{$value2}; 
  p6: $value1 + $value2; 
  p7: $value1 $value2; 
}

The preceding Sass code compiles into CSS code as follows:

test { 
  p1: unquoted; 
  p2: "quoted"; 
  p3: unquoted quoted; 
  p4: unquotedquoted; 
  p5: unquoted + quoted; 
  p6: unquotedquoted; 
  p7: unquoted "quoted"; } 

In the preceding CSS code, you can see that the quotes are removed from the $value2 variable for the p3, p4, and p5 properties when applying string interpolation with the #{} interpolation syntax. For the p5 property, the + operator has not been applied, while the p6 and p7 properties will show how the strings are concatenated, as described in the Applying operations in Sass recipe of this chapter.

There's more...

Other use cases of variable interpolation can be found elsewhere in this book too. In the Commenting your code in the SCSS syntax recipe of Chapter 2, Debugging Your Code, you can read how to use variable interpolation to generate copyright notices in your CSS code. The Applying operations in Sass recipe of this chapter describes why you should use variable interpolation when using the calc() CSS function in your code.

Variable interpolation can also be used inside mixins and functions to dynamically compile a list of selectors or values. You can read more about mixins in the Leveraging mixins and Writing mixins with arguments recipes of this chapter.

主站蜘蛛池模板: 封开县| 锡林浩特市| 黔西县| 江阴市| 黎城县| 封开县| 谷城县| 三亚市| 济南市| 孟州市| 黄梅县| 清涧县| 武夷山市| 三原县| 营口市| 平和县| 阳高县| 兴和县| 铜山县| 彝良县| 秭归县| 无极县| 渭南市| 南昌市| 历史| 禹州市| 云南省| 米泉市| 阿鲁科尔沁旗| 贵港市| 桑日县| 天柱县| 伽师县| 南江县| 南澳县| 东乡县| 永康市| 灵山县| 珠海市| 奉化市| 富源县|