Sass interpolation (#{}) __sass

Source: Internet
Author: User
Tags css preprocessor

One of the main reasons for using the CSS preprocessor language is to use Sass to get a better structure. For example, you want to write cleaner, more efficient, and object-oriented CSS. The interpolation (interpolation) in the Sass is an important part. Let's take a look at the following example:

$properties: (margin, padding);
@mixin Set-value ($side, $value) {
    @each $prop in $properties {
        #{$prop}-#{}: $side}
}
. Login-box {
    @include set-value (top, 14px);
}

It can make variables and properties work perfectly, and the code above is compiled into CSS:

. login-box {
    margin-top:14px;
    padding-top:14px;
}

This is a simple example of Sass interpolation. When you want to set the property value you can insert it using a string. Another useful use is to build a selector. You can use this:

@mixin generate-sizes ($class, $small, $medium, $big) {
    . #{$class}-small {font-size: $small;}
    . #{$class}-medium {font-size: $medium;}
    . #{$class}-big {font-size: $big;}
}
@include generate-sizes ("Header-text", 12px, 20px, 40px);

Compiled CSS:

. Header-text-small {font-size:12px}
. Header-text-medium {font-size:20px}
. Header-text-big {font-size:40px;}

Once you discover this, you will think of the mixins, which generates code or generates another mixins. However, this is not entirely possible. The first limit, which may very well remove the interpolation for the Sass variable.

$margin-big:40px;
$margin-medium:20px;
$margin-small:12px;
@mixin Set-value ($size) {
    margin-top: $margin-#{$size};
Login-box {
    @include set-value (big);
}

The Sass code above compiles and you get the following information:
Error STYLE.SCSS (line 5:undefined variable: "$margin-".)
Therefore, the #{} syntax is not readily available and you cannot invoke it in Mixin:

@mixin updated-status {
    margin-top:20px;
    Background: #F00;
}
$flag: "Status";
Navigation {
    @include updated-#{$flag};
}

The above code also complains when translated into CSS:
Error Style.scss (line 7:invalid CSS after "... nclude updated-": Expected "}", was "#{$flag};")
Fortunately, you can use interpolation in @extend. For example:

%updated-status {
    margin-top:20px;
    Background: #F00;
selected-status {
    font-weight:bold;
}
$flag: "Status";
Navigation {
    @extend%updated-#{$flag};
    @extend. selected-#{$flag};
}

The Sass code above can be run because he gives us the power to dynamically insert. Class and%placeholder. Of course they can't accept parameters like Mixin, the above code compiles the CSS:

. navigation {
    margin-top:20px;
    Background: #F00;
Selected-status,. Navigation {
    font-weight:bold;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.