<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Nazanin Ashrafi - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Nazanin Ashrafi - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 11 Jun 2026 11:17:40 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/Nazanin/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Mixins in Sass and Pass Arguments – With Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ Mixins are my favorite thing about Sass. They made my life so much easier, so I wanted to show you how they can do the same for you. Mixins can be a bit tricky to understand at first, but don't worry. You'll get the hang of it by practicing and ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-pass-arguments-to-mixins/</link>
                <guid isPermaLink="false">66d46040e39d8b5612bc0dc0</guid>
                
                    <category>
                        <![CDATA[ Sass ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Nazanin Ashrafi ]]>
                </dc:creator>
                <pubDate>Thu, 02 Dec 2021 18:45:59 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/11/sass-mixins.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Mixins are my favorite thing about Sass. They made my life so much easier, so I wanted to show you how they can do the same for you.</p>
<p>Mixins can be a bit tricky to understand at first, but don't worry. You'll get the hang of it by practicing and will fall in love with mixins like I have.</p>
<p>Before I get started, let me show you what you will read in this article:</p>
<ul>
<li><p>What mixins are</p>
</li>
<li><p>How to write mixins and include them in your code</p>
</li>
<li><p>How and when to pass arguments</p>
</li>
</ul>
<p>Now let's get to the point, shall we?</p>
<h2 id="heading-what-are-mixins-in-sass">What Are Mixins in Sass?</h2>
<p>First, let's take a quick look at what a mixin is:</p>
<blockquote>
<p>"<a target="_blank" href="https://sass-lang.com/documentation/at-rules/mixin">Mixins</a> allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like <code>.float-left</code>, and to distribute collections of styles in libraries." – Sass Docs</p>
</blockquote>
<p>To put it simply, a mixin is a code block which allows you to write your styles in it and use it throughout the whole project. You can also think of it as a <em>reusable</em> component. It also helps you to write cleaner code without having to repeat yourself.</p>
<h2 id="heading-how-to-write-a-mixin">How to Write a Mixin</h2>
<p>This is how you write a mixin in Sass:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> name {
    properties;
}
</code></pre>
<p>And here's how to include it in your code:</p>
<pre><code class="lang-scss"><span class="hljs-selector-tag">div</span> {
    <span class="hljs-keyword">@include</span> name;
}
</code></pre>
<p>Here's an example of using a mixin in your code:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background</span>: red;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
}

<span class="hljs-selector-tag">div</span> {
   <span class="hljs-keyword">@include</span> circle;
}
</code></pre>
<p>Now let's see what's happening in the above code:</p>
<ol>
<li><p>First we define a mixin using the <code>@mixin</code> at-rule.</p>
</li>
<li><p>Then we give it a name – choose whatever you think will fit what you're gonna be using it for.</p>
</li>
<li><p>Add your CSS properties.</p>
</li>
<li><p>By simply using <code>@include</code> you pass it to the mixin block.</p>
</li>
</ol>
<h2 id="heading-mixin-example">Mixin Example</h2>
<p>Now let's look at an example of a mixin in action.</p>
<p>Here's how to create a pink circle with a mixin:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#ea0185</span> ;
}
</code></pre>
<pre><code class="lang-html">.circle {
    @include circle;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-from-2021-11-18-19-27-24--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now you might ask <em>"why should I use a mixin to create a pink circle? I could just give my element a class and style it."</em></p>
<p>Mixins are reusable, remember? We use them when we know we'll be repeating ourselves a lot. So the whole point is to <em>avoid</em> repetition and keep the code clean.</p>
<h2 id="heading-passing-arguments">Passing Arguments</h2>
<p>Now that we've seen how to write a mixin, let's move on to the next section. I want to divide this section into smaller parts:</p>
<ul>
<li><p>What are mixin arguments?</p>
</li>
<li><p>When to pass arguments?</p>
</li>
<li><p>How to pass arguments? + Examples.</p>
</li>
</ul>
<h3 id="heading-what-are-mixin-arguments">What Are Mixin Arguments?</h3>
<p>An argument is the name of a variable which is separated by a comma.</p>
<h3 id="heading-when-should-you-pass-arguments-to-a-mixin">When Should You Pass Arguments to a Mixin?</h3>
<p>I'll start this section with an example:</p>
<p>What if you were to create two different circles? Like a green circle and a pink circle?</p>
<p>You could create two separate mixins, one for the green one and one for the pink one:</p>
<pre><code class="lang-scss"><span class="hljs-comment">// a mixin for the green circle</span>
<span class="hljs-keyword">@mixin</span> green-circle {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">background</span>: green;
}

<span class="hljs-comment">// and another mixin for the pink circle</span>
<span class="hljs-keyword">@mixin</span> pink-circle {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">background</span>: pink;
}
</code></pre>
<p>But this isn't great because you're repeating your code. And we should stick to the DRY (Don't Repeat Yourself) principle, remember?</p>
<p>And that's where mixin arguments come in.</p>
<p>In a regular mixin (and by regular I mean a mixin when no argument is passed) you define some certain styles. But an argument allows you to define different styles by turning them into variables. It's like customizing each style for each element. Let's move on to the next section and see some examples.</p>
<h3 id="heading-how-to-pass-arguments-to-mixins">How to Pass Arguments to Mixins</h3>
<p>We've seen what an argument is and when to use it. And now it's time to see how to pass the arguments:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> name(<span class="hljs-variable">$argument</span>,<span class="hljs-variable">$argument</span>) {
    property: <span class="hljs-variable">$argument</span>;
    property: <span class="hljs-variable">$argument</span>;

}
</code></pre>
<p>Here's an example:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle2 (<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$color</span>) {
    <span class="hljs-attribute">width</span>: <span class="hljs-variable">$width</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-variable">$height</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-variable">$color</span>;
}
</code></pre>
<p>You can think of arguments as customizable variables that you can use in different situations to create different things without repeating yourself.</p>
<p>Like when you pass <code>$width</code> to the width property, you can define it in different situations. Maybe you need the width to be 50px in one place and 500px somewhere else.</p>
<p>Does that make sense? Let me break it down for you with another example.</p>
<p>Okay, back to our circles.</p>
<p>I want to make one big red circle and one small green circle (two different things) with just one mixin.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-from-2021-11-22-21-25-44--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now what properties do I need to make a circle?</p>
<pre><code class="lang-python">width, height <span class="hljs-keyword">and</span> background-color, right?
</code></pre>
<p>Since we're building circles, the border-radius will be 50% in both situations. So I will leave it alone and won't pass any argument to it.</p>
<p>Now we're down to 3 properties:</p>
<ol>
<li><p>width</p>
</li>
<li><p>height</p>
</li>
<li><p>background-color</p>
</li>
</ol>
<p>That means we only need 3 arguments:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle(<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$color</span>) {
    <span class="hljs-comment">// We passed $width to the width property</span>
    <span class="hljs-attribute">width</span>: <span class="hljs-variable">$width</span>;

    <span class="hljs-comment">// We passed $height to the height property</span>
    <span class="hljs-attribute">height</span>: <span class="hljs-variable">$height</span>;

    <span class="hljs-comment">// And we passed $color to width background-color</span>
    <span class="hljs-attribute">background</span>: <span class="hljs-variable">$color</span>;

    <span class="hljs-comment">// no argument for this property, beacuase it's gonna be the</span>
    <span class="hljs-comment">// same in both circles</span>
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
}
</code></pre>
<p>So now let's see how we can pass arguments to our mixin:</p>
<h4 id="heading-for-the-big-red-circle">For the big red circle</h4>
<pre><code class="lang-scss"><span class="hljs-selector-class">.circle-red</span> {

    <span class="hljs-comment">// circle ($width,$height,$color);</span>
   <span class="hljs-keyword">@include</span> circle (<span class="hljs-number">350px</span>,<span class="hljs-number">350px</span>,red);
}
</code></pre>
<h4 id="heading-for-the-small-green-circle">For the small green circle</h4>
<pre><code class="lang-scss"><span class="hljs-selector-class">.circle-green</span> {

     <span class="hljs-comment">// circle ($width,$height,$color);</span>
    <span class="hljs-keyword">@include</span> circle (<span class="hljs-number">200px</span>,<span class="hljs-number">200px</span>,green);
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-from-2021-11-22-21-25-44--1--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you want some more info about passing arguments to mixins, here's a little video to help you out:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/0s-xjyXOtP4" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<p>Alright, back to our tutorial. As I said earlier, I didn't pass any arguments to the border-radius property because it's always gonna be 50% (in <em>this</em> case).</p>
<p>But if I were to make one square and one circle, then I would need to pass an argument to <code>border-radius</code> too:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle(<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$color</span>,<span class="hljs-variable">$radius</span>) {
    <span class="hljs-attribute">width</span>: <span class="hljs-variable">$width</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-variable">$height</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-variable">$color</span>;
    <span class="hljs-comment">// passed argument to border-radius to have control over it </span>
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-variable">$radius</span>;
}

<span class="hljs-selector-class">.square</span> {
            <span class="hljs-comment">// ($width,$height,$color,$radius)</span>
    <span class="hljs-keyword">@include</span> circle (<span class="hljs-number">350px</span>,<span class="hljs-number">350px</span>,red, <span class="hljs-number">10px</span>);
}

<span class="hljs-selector-class">.circle</span> {
            <span class="hljs-comment">// ($width,$height,$color,$radius)</span>
    <span class="hljs-keyword">@include</span> circle (<span class="hljs-number">200px</span>,<span class="hljs-number">200px</span>,green, <span class="hljs-number">50%</span>);
}
</code></pre>
<p>Now we have a big red square and a small green circle:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-from-2021-11-22-22-17-12--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Let's take a look at another example. This time let's try using a mixin on some text.</p>
<p>This is what I want to make, a green text with black background and red text with a transparent background:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/Screenshot-from-2021-11-25-19-26-49--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>First I created two h2 elements:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text1"</span>&gt;</span>Text<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text2"</span>&gt;</span>Text<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
</code></pre>
<p>We need <code>font-size, color, and background</code> properties here. Now I should pass arguments by turning them into variables.</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> text(<span class="hljs-variable">$font-size</span>,<span class="hljs-variable">$color</span>,<span class="hljs-variable">$bg-color</span>) {

     <span class="hljs-comment">// we pass the $font-size to font-size property</span>
    <span class="hljs-attribute">font-size</span>: <span class="hljs-variable">$font-size</span>;

    <span class="hljs-comment">// we pass the $color to color property</span>
    <span class="hljs-attribute">color</span>: <span class="hljs-variable">$color</span>;

    <span class="hljs-comment">// we pass the $bg-color to background property</span>
    <span class="hljs-attribute">background</span>: <span class="hljs-variable">$bg-color</span>;
}



<span class="hljs-selector-class">.text1</span> {
          <span class="hljs-comment">// ($font-size,$color,$bg-color)</span>
    <span class="hljs-keyword">@include</span> text(<span class="hljs-number">3rem</span>,green , black)
}

.text2 {
          <span class="hljs-comment">// ($font-size,$color,$bg-color)</span>
    <span class="hljs-keyword">@include</span> text(<span class="hljs-number">5em</span>,red , transparent)
}
</code></pre>
<p>And there you have it.</p>
<p><strong>Quick tip:</strong> Remember that <em>the order of arguments matters.</em></p>
<p>It matters because the only way to know what value you meant to pass for each parameter is by using the correct order.</p>
<p>For example, if your arguments order is <em>$width, $height, $color</em>, passing them should be in order as well:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> circle(<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$color</span>) {
    <span class="hljs-attribute">width</span>: <span class="hljs-variable">$width</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-variable">$height</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-variable">$color</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;

}
</code></pre>
<pre><code class="lang-scss"><span class="hljs-selector-class">.circle-red</span> {
             <span class="hljs-comment">// ($width,$height,$color)</span>
    <span class="hljs-keyword">@include</span> circle (<span class="hljs-number">350px</span>,<span class="hljs-number">350px</span>,red);
}
</code></pre>
<p>You can't pass color first followed by the width and height:</p>
<pre><code class="lang-python">.circle-red {
<span class="hljs-meta">    @include circle (red,350px,350px);</span>
}
</code></pre>
<p>Regarding this wrong order, we passed <code>$width</code> to width property, therefore the first value needs to be a number. So if you pass <code>$color</code> first, the value won't be recognized. That's why we have to pass arguments in order.</p>
<h2 id="heading-heres-a-quick-review-of-what-weve-talked-about-in-this-article">Here's a quick review of what we've talked about in this article</h2>
<ul>
<li><p>Mixins are reusable code blocks.</p>
</li>
<li><p>We use them when we know we'll be repeating pieces of code a lot.</p>
</li>
<li><p>This is how to we write a mixin:</p>
</li>
</ul>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> name {
    properties;
}
</code></pre>
<ul>
<li><p>An argument is a name of a variable which is separated by a comma.</p>
</li>
<li><p>Arguments allow you to define different styles.</p>
</li>
<li><p>The order of arguments matters.</p>
</li>
<li><p>This is how we pass arguments:</p>
</li>
</ul>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> name(<span class="hljs-variable">$argument</span>,<span class="hljs-variable">$argument</span>) {
    property: <span class="hljs-variable">$argument</span>;
    property: <span class="hljs-variable">$argument</span>;

}
</code></pre>
<p>And that's a wrap for this article – I hope you liked it and found it useful. 😊</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/11/image-113.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>You can also connect with me on</em> <a target="_blank" href="https://twitter.com/nazanin_ashrafi"><strong><em>twitter-</em></strong></a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
