SCSS Mixins

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Let's get a closer look at using @mixins in SCSS. You can look at mixins like the import we used before. But mixins place a certain set of codes on the element we are mixing in.
We use the @mixin directive to define our mixin, so let's go ahead and create our first one:
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
Note: Naming in SCSS can be either with - or _ they are considered the same and can be used at the same time!
To use our mixin we simply use the @include statement:
.container {
@include flex-center;
height: 100vh;
}
Our properties on the mixin will now also exist on the container element.
Another cool thing we can do is use mixins inside each other like so:
@mixin font {
font-family: Roboto, 'Helvetica Neue', Arial, sans-serif;
font-size: 2rem;
}
@mixin flex {
display: flex;
}
@mixin flex-center {
@include flex;
@include font;
justify-content: center;
align-items: center;
}
Something that is really strong for using mixins is the use of arguments.
We can define our mixin as such:
@mixin button($background: blue, $padding: 10px, $color: #fff) {
background: $background;
padding: $padding;
display: block;
border-radius: 5px;
color: $color;
}
Note: We added default parameters, but these are not mandatory, you can leave them out.
And once we call it, pass these arguments:
.box {
@include button(#7afdd6, 20px, #ffffff);
}
A really good pro-tip is to use Mixins for vendor prefixes! It will safe you so much time for border-radius for example:
@mixin border-radius($amount) {
/* Safari 3-4, iOS 1-3.2, Android 1.6- */
-webkit-border-radius: $amount;
/* Firefox 1-3.6 */
-moz-border-radius: $amount;
/* Opera 10.5, IE 9, Safari 5, Chrome, Firefox 4, iOS 4, Android 2.1+ */
border-radius: $amount;
}
And we use this like so:
@include border-radius(20px);
Have a play around with these examples on Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter