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

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.

主站蜘蛛池模板: 乌鲁木齐县| 多伦县| 龙南县| 夹江县| 龙山县| 扎兰屯市| 织金县| 开化县| 佳木斯市| 盐津县| 虎林市| 泗水县| 正安县| SHOW| 绥滨县| 慈溪市| 湟中县| 呼和浩特市| 邛崃市| 博兴县| 呈贡县| 剑川县| 乌鲁木齐县| 广灵县| 嫩江县| 连城县| 西吉县| 砚山县| 永福县| 汨罗市| 江西省| 浦东新区| 桦南县| 瓮安县| 马山县| 九龙坡区| 桐乡市| 中西区| 凌云县| 唐海县| 郓城县|