Table of contents
1.
Introduction
2.
Content Blocks
2.1.
Passing Arguments to Content Blocks
3.
Passing content block to a mixin
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

SASS Passing Content Block to Mixin

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Saas Mixin Directive is a way to reuse styles across the stylesheet. It can store multiple values to avoid writing repetitive codes. It is a powerful SASS directive that can also pass arguments, making it less complicated than other options. We can also create a mixin with a default value to avoid errors that can be further overwritten by passing a value to the variable in the argument.

In this blog, we will understand Sass passing content block to a mixin in complete detail.

Content Blocks

As we know, a mixin can take arguments. It can also take an entire block of styles known as a content block. A mixin declares that it takes a content block by including the @content at-rule in its body. The content block is passed using curly braces ({ }) like any other block in Sass, and it is included in place of the @content rule.

Consider the following example, which explains how the content block is passed in Sass:  

Example:

@mixin hover {
  &:not([disabled]):hover {
    @content;
  }
}
.button {
  border: 3px solid blue;
  @include hover {
    border-width: 2px;
  }
}

 

After compilation into CSS it will become:

.button {
  border: 3px solid blue;
}
.button:not([disabled]):hover {
  border-width: 2px;
}

 

A content block is lexically scoped, implying that it can only see local variables in the scope where mixin is included. It can’t see any variables defined in the mixin even if they are defined long before the content block is invoked.

A mixin can include multiple @content at-rules, including separately for each @content block.

 

Passing Arguments to Content Blocks

A mixin can also pass arguments to its content block in the same way it passes arguments to another mixin using @content(arguments...). The content block user can accept arguments by using the @include directive. The argument list for a content block works similar to mixin’s argument list.

If a mixin passes arguments to its content block, it must declare that it accepts those arguments. This means that one should pass arguments by position rather than their name.

 

Example:

Scss File

@mixin media($types...) {
  @each $type in $types {
    @media #{$type} {
      @content($type);
    }
  }
}
@include media(screen, print) using ($type) {
  h1 {
    font-size: 40px;
    @if $type == print {
      font-family: Roboto;
    }
  }
}

 

When transpired, the CSS file will look like this:

@media screen {
  h1 {
    font-size: 40px;
  }
}
@media print {
  h1 {
    font-size: 40px;
    font-family: Roboto;
  }
}

 

Passing content block to a mixin

The content block is passed to the mixin for placement inside the styles. Styles are included in the mixin at the @content directive location. The content block is specified in the scope, and the scope is passed in the mixin where the block is defined.

Consider the following example:

<html>
   <head>
      <title>Mixin Example of SASS</title>
      <link rel = "stylesheet" type = "text/css" href = "file.css"/>
   </head>
   <body>
      <div class = "block">
         <h1>Coding Ninjas</h1>
         <p>Different Colors</p>
         <ul>
            <li>Yellow</li>
            <li>Purple</li>
            <li>Blue</li>
         </ul>
      </div>
   </body>
</html>

 

Create file.scss file

@mixin element {
   @content;
}
@include element {
   .block {
      Color: blue;
   }
}

 

Use the following command to tell the Sass to watch the file and update the CSS whenever the file changes:

sass --watch file.scss : file.css

 

Executing the above command will automatically create a CSS file that will look like this:

@include element {
   .block {
      Color: blue;
   }
}

 

Output:


Check out this problem - Redundant Braces

Read Also -  Difference between argument and parameter

FAQs

  1. Explain Sass maps and their uses in web development?
    Sass map is structured data hierarchically. It helps in organizing the code. 
    Following are the use of Sass maps in web development:
    - It is useful in dealing with layers of elements in our project
    - It is helpful in color management when there is a long list of different colors.
    - It is also used as an icon map for various social media icons. E.g., Facebook: ‘\e607’.
     
  2. Why is Sass considered better than Less?
    Saas allows us to write reusable methods and use logic statements. Listed below are some reasons why we should consider using Sass over Less:
    - Saas users can access the compass library and awesome features like legacy browser hacks, dynamic sprite map generation, and cross-browser support.
    - Compass also allows users to add an external framework like Bootstrap, Foundation, or Blueprint.
    - In Sass, we can write our functions.
    - In LESS, we can write a basic logic statement using ‘guarded mixin’, equivalent to Sass IF statements.
    - We can loop through numeral values using recursive functions in LESS, while Sass allows us to iterate any data.
     
  3. State some key features of Sass indented syntax?
    Saas uses indentation rather than curly braces to delimit blocks. It uses newlines instead of semicolons(;) to separate statements. The property declaration and selectors must be made in its line.
     
  4. Which directive is used to detect the errors in Sass?
    The @debug directive is used to detect the errors in Saas. It also displays the Sass script expression values to the standard error output stream.  
    Example:
     
$font-sizes: 30px + 20px;    
$style: (    
  color: blue    
);    
.container{    
  @debug $style;    
  @debug $font-sizes;    
}     

Key Takeaways

This blog discussed mixin directives and how a content block is passed to mixin, with a few examples. Mixin is a SASS directive that can be used for reusing styles. The content blocks are passed to the mixin for placement inside the styles. 

If you are a beginner and interested in learning similar fields such as DSA, core computer subjects, web development, or competitive programming. In that case, you can follow our guided path to get a good grip on your concepts.

Live masterclass