Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@function v/s @mixin in Sass-lang. Which one to use?

Tags:

sass

After searching a lot in difference between @function and @mixin I ended up here.

Is there any advantage of using @mixin over @funcion or vice versa. In what context they'll be different, how to use them interchangeably, please come up with examples.

like image 306
Channaveer Hakari Avatar asked Jul 06 '15 07:07

Channaveer Hakari


People also ask

What is @mixin in Sass?

Sass MixinsThe @mixin directive lets you create CSS code that is to be reused throughout the website. The @include directive is created to let you use (include) the mixin.

What is difference between mixin and function?

@mixin, very similar to a function but the main difference between the two is that mixins output lines of Sass code that will compile directly into CSS styles, while functions return a value that can then become the value for a CSS property or become something that might be passed to another function or mixin.

How do you use mixin as a function in Sass?

Mixins are defined using the @mixin at-rule, which is written @mixin <name> { ... } or @mixin name(<arguments...>) { ... } . A mixin's name can be any Sass identifier, and it can contain any statement other than top-level statements.

How many types of functions are there in Sass 5?

Sass include various function for string, numeric, list, map, selector and introspection.


1 Answers

Functions are useful specifically because they return values. Mixins are nothing like functions--they usually just provide valuable blocks of code.

Usually, there are cases where you might have to use both.

For example, if I wanted to create a long-shadow with SASS, I would call a function like so:

@function makelongshadow($color) {   $val: 0px 0px $color;   @for $i from 1 through 200 {     $val: #{$val}, #{$i}px #{$i}px #{$color};   }   @return $val; } 

Which would then be called with this mixin:

@mixin longshadow($color) {   text-shadow: makelongshadow($color); } 

Which provides us with the actual code.

That gets included in the element:

h1 {     @include longshadow(darken($color, 5% )); } 
like image 64
Emma Ramirez Avatar answered Sep 21 '22 05:09

Emma Ramirez