02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Introducing SCSS Mixins

In this lecture, we delve into the practical application of SCSS mixins, a powerful feature for managing styles efficiently. By the end of this tutorial, you'll understand how to structure and modularize your CSS effectively using SCSS.


What Are SCSS Mixins?

SCSS mixins allow you to define reusable chunks of CSS code, similar to functions in programming. These reusable styles can take parameters, enabling you to generate dynamic CSS with minimal redundancy.


Steps to Introduce SCSS Mixins

1. Setting Up Your Environment

  • Ensure you have SCSS configured with Webpack.
  • Import Bootstrap’s SCSS utilities as needed for your project.

2. Defining a Mixin

  • SCSS mixins are defined using the @mixin keyword followed by a name and parameters.
@mixin background-it($path, $color: white) {
    background-image: url($path);
    color: $color;
}
  • The $path is a mandatory parameter for the background image.
  • The $color parameter is optional, defaulting to white.

3. Using Mixins in Your Styles

  • To use the mixin, call it with the @include directive:
.custom-style {
    @include background-it('/images/bg.png', yellow);
}
  • This generates CSS that applies the specified background image and text color.

4. Structuring Your Project with Imports

  • Modularize your SCSS files by breaking them into components, such as buttons.scss and jumbotron.scss.
  • Use the @import directive to include shared variables and mixins:
@import 'variables';
@import 'mixins';

5. Applying Modular SCSS to Bootstrap

  • Create custom SCSS files for each component you want to style (e.g., buttons, jumbotrons).
  • Use Bootstrap's mixins and variables to customize styles efficiently.

Practical Example: Custom Button Styles

  1. Create a buttons.scss file.
  2. Define and include styles specific to buttons:
@import 'variables';
@import 'mixins';

.button-primary {
    @include background-it('/images/button-bg.png', blue);
}
  1. Import the buttons.scss into your main SCSS file:
@import 'buttons';

Benefits of SCSS Mixins

  1. Reusability: Centralize shared styles to avoid repetition.
  2. Customization: Pass parameters to tailor styles for different components.
  3. Maintainability: Modularize styles for better project organization.

Next Steps

In this lecture, we laid the foundation for using SCSS mixins and modular imports to streamline your styling process. In the next lecture, we’ll explore advanced mixin usage and further integration with React components. Stay tuned!

Video Transcript

00:01It is time for us to look at, look at another feature
00:03of SaaS which enables us to import external CSS
00:06or SaaS files, which basically makes it so much easier
00:09for us to break apart our CSS to fit for
00:12what we actually need.
00:13Some CSS for the button, some CSS for the jumbotron.
00:17So in the process we're gonna take a look at
00:19how these variables work
00:20and really technically inside of webpac,
00:23how do variables work
00:24because they have scope issues that we're gonna need
00:26to solve in that process.
00:28We're also gonna look at the how we're gonna import files
00:31to get us closer to creating our React components completely
00:34integrated with Bootstrap.
00:37Okay, what I wanna do next is, so far in our application,
00:42our HTML itself was loading the full bootstrap CSS
00:47that we've downloaded,
00:48but I really don't wanna do that for one, there are a lot
00:51of components in Bootstrap
00:53and two, we don't need all of them.
00:56So wouldn't it be wonderful
00:58as our application is built based on whatever we're using,
01:01we would only import the code
01:02that is relevant to those items.
01:05I think that would be incredible. I'm sure you do as well.
01:09Now to do that, we're gonna have to go through and use SaaS.
01:12And first of all, we're gonna also have to make sure,
01:15and let me just stop our server.
01:17What we're gonna have to do is we're gonna install the
01:20Bootstrap package, but we're actually not going
01:22to use all the things inside of the package.
01:24Now I'm gonna go ahead and type NPM install
01:27and I'm not gonna use this for production,
01:29so I'm gonna save it into the dev as well.
01:31And I'm gonna ask for Bootstrap
01:34and I'm gonna take the version that is currently
01:37the latest version of the Alpha.
01:39So if I go inside of my V four Alpha, I'll see
01:42that currently it's V 4 0 0 dash alpha a.
01:46That's basically the version.
01:48Now there might be an a newer version
01:50by the time you watch this video
01:52and most probability there would be, so you could
01:55or go with the version that we're going
01:56and make sure that you're,
01:57everything's gonna work according to plan.
02:00Or if you take a version that is later than this,
02:02then you might have some errors, which is fine.
02:04And then you'll just have to sort them out yourself.
02:06Just like we're gonna be sorting out these errors one by one
02:09as we're building it out.
02:11So while this is going through the process of installation,
02:15I want to talk to you about basically what we're going
02:18to do now.
02:19Oh, it's done installing, so even better.
02:21So let's just go and let's find it.
02:23So in our modules, let's just go into our bootstrap
02:25and you'll find inside of your bootstrap a folder
02:28that's called CSS or as well SCSS.
02:32In it you'll find a lot of pieces.
02:34And these pieces are basically components
02:36that you could then use.
02:37For example, your bootstrap file itself actually has
02:41everything that is needed.
02:43Now, before I test things out, let me just make sure OTO
02:46that I am running my server,
02:53Okay, our server is running.
02:54And while our server is starting to run, first of all,
02:56let's just see how it looks like right now.
02:58Now we don't have Bootstrap added in. Beautiful.
03:02The next step for us to do is
03:03let's go ahead and just import it.
03:04But this time around, I'm gonna go into my app source,
03:08SCSS here, and this is gonna start
03:10everything up here for me.
03:11So I'm gonna go ahead inside of here just
03:13before everything, I'm gonna add the import command,
03:17which is uh, command of SCSC or SaaS.
03:21And I'm gonna literally just go ahead
03:23and define my import line.
03:25Basically what are the files that I wanna load up?
03:28So in this case, what I really want to go is I want
03:30to go one step back from my source folder.
03:32So lemme go ahead one step back
03:34'cause I'm relative to this file
03:36and I'm gonna go to my node modules.
03:39Then Bootstrap, as we saw Bootstrap,
03:44as we saw, there's A-S-C-S-S file
03:47and in it there's the Bootstrap, which basically holds all
03:53strap, basically holds all the different features
03:56that are available within Bootstrap.
03:57Now, just for the sake of, uh, showing you off
04:00how this thing really works, if you go into that bootstrap,
04:03if I find it here for a second, uh, boo boo
04:07boo, where are you?
04:09There you go. Bootstrap. STSS.
04:11And if we take a peek
04:12and understand what's actually going on inside
04:14of our SCSS here, literally what's going on,
04:17there's all these import imports going on
04:20where you have the core features that are the variables
04:23and mixings, which we'll talk about in a lecture soon.
04:26And then there's different dependencies
04:29and there's all these different core things
04:31that are needed almost for everything.
04:33And then there's components and so on and so forth.
04:36Now theoretically, once we built everything out,
04:38we would want all the core things there,
04:40but not necessarily all the components
04:42or all the different modules such as JavaScript,
04:44which we're not using, and different utilities
04:47that we might not be using and so forth.
04:50But in our situation,
04:51and let me just go back into our SCSS,
04:54what we're actually doing is we're loading
04:55up absolutely everything.
04:57Now, if I go back and I look into my application,
05:00we should be back up and running just as we were previously.
05:04Now what I wanna do next is I want to go ahead
05:07and actually import and create this construct ourselves.
05:11Literally taking it step by step
05:14and defining what do we want to import
05:16and what we don't want to import.
05:18So I'm not gonna load up all the
05:20bootstrap by the way we want to.
05:21Another reason for that is
05:23because SCSS in node
05:26runs independently.
05:28So while in a lot of different environments you could,
05:31what we are, what we're really trying to do,
05:33and let me just close this 'cause it's way too confusing.
05:36Once, once we load something up.
05:38So in the case, for example, inside of our bootstrap,
05:41let's say button, what do we want?
05:43Load in a button. So for one, we know
05:45that there's one critical thing that we wanna load
05:48and that one critical thing that we wanna load is sitting
05:51inside of here, which is the bootstrap variables.
05:53We always wanna load that up.
05:55So I'm gonna go ahead and copy that
05:57and I'm not gonna delete it,
05:58I'm just gonna copy it from there.
06:00And what I wanna do is I wanna place
06:01it inside of my project.
06:02So I'm gonna go into my source, um, bootstrap
06:06because it relates to Bootstrap.
06:07And I'm gonna go ahead and just literally
06:09just paste it right in here.
06:11Now that I have my variable set
06:12and I'm just gonna go inside of this variable,
06:15I could literally go through and you're more than encouraged
06:18to change things such as the themes and the colors.
06:20'cause every place where it's used,
06:21they will basically override whatever's there.
06:24That's the power of working with SaaS.
06:27Okay, so now that we have our variables here, I also want
06:29to create a buttons element.
06:32So a CS CSS buttons element.
06:35So to do that, I'm gonna go ahead
06:36and I'm gonna click on the folder.
06:38I'm gonna create a new file. I'm gonna call it buttons.
06:41I'm gonna call it button dot s css.
06:44I'm gonna go ahead and save it
06:46and save it exactly where it's at there.
06:49Now what I'm gonna do next is I just want to go ahead
06:51and peek for a second at what would it look like inside
06:55of our node module?
06:56So
06:57bootstrap that didn't
07:04Bootstrap SCSS.
07:06I just want to take a peek at how buttons look like.
07:08So I'm just gonna take a peek at it.
07:10So you can see that we have your R button, which we've met
07:13before, and it's using all these different variables
07:16that are defined inside of that var.
07:18Now what I want to do is I want to import everything inside
07:21of this button without
07:22actually changing anything inside of it.
07:24'cause we're not really editing anything in our button.
07:27But the only thing that we do need to do is basically when,
07:30whenever one, once we're starting our application,
07:33once we're calling that element, I need to go ahead
07:36and I need to import.
07:38Let me go ahead and do that. I need to import.
07:41Import and what do I need to import?
07:44I need to import variables.
07:47Now notice that I'm only writing variables without the
07:50underscore and without the dot s Cs s.
07:53Now whenever something has an underscore,
07:55really it's just an indicator for us to let us know
07:57that this is not something that stands on its own.
08:00It needs to be loaded within something else.
08:02In our case, our buttons,
08:03SCSS could be load loaded on its own.
08:06So I'm gonna go ahead and also what I want
08:08to do is I wanna load in that button.
08:10So to do that, lemme just go ahead in here
08:12and let me undo that delete.
08:14'cause I really want to take that import again
08:16because I need that full file path there.
08:19And this time around, I'm not gonna take the whole
08:21bootstrap, I'm just gonna ask for buttons.
08:24Okay? So we have here our button s css, that corresponds
08:28with our button js
08:30and we're loading in our the Bootstrap buttons logic.
08:35But before we do that,
08:36we're loading in our local variables that we control over.
08:39Now we really have to, because those buttons need access
08:42to those elements.
08:43So if I just go ahead
08:44and I save this, all that's left for me
08:46to do do is go into my buttons and click on import.
08:50And because we're inside of the same folder,
08:52I'm just gonna go ahead and import buttons, s
08:57css, and now I have oto, my visuals imported as well.
09:01Anytime I use a button.
09:03Now I'm gonna go ahead and I want to implement this Otto
09:05for our ju uh, jumbotron.
09:07So to do it for our jumbotron, I'm just gonna go ahead
09:09and create another variable
09:11and I'm gonna call it jumbotron s
09:15css, save that.
09:18And this time around,
09:19I'm gonna do the same thing like we've done in the,
09:21in our button only.
09:22Instead of asking for the button, I'm gonna ask
09:25for the jumbotron.
09:26So let's go ahead and do that.
09:29And now I'm basically fetching that jumbotron.
09:32Now the importance here is really I need
09:34to import the variables each time.
09:35Because when, if I would load this without it,
09:38every time I load in node SCSS files,
09:42it loads them up in their own scope.
09:45So really what I'm doing is I'm setting the variables in the
09:47same scope so it'll have access to those variables.
09:50So now that I've loaded up the jumbotron, the jumbotron,
09:54I've, no, I didn't actually, I needed
09:56to load up the jumbotron as well.
09:57So let's go and do that in our jumbotron as well.
10:00Let's go ahead and import that as well.
10:04And only whenever they're used use,
10:06then we're gonna actually load them up.
10:08So jumbotron dot s, css, and let's go ahead and save that.
10:13And notice, and don't forget that we've removed out
10:16of our app source, our bootstrap, we're removed out
10:20of our HTML, the bootstrap as well.
10:23And when we go now into our browser, let's see,
10:26uh oh, we have a little error.
10:28So first of all, file
10:30to import not found unreadable variable uh,
10:35variables because that's
10:36because I didn't write it correctly.
10:38Uh, let me go ahead
10:40and just double check our, this is how you write a variable.
10:45Let me don't save and let me go into there
10:50and ask for that.
10:52Oh, you know what? I made a double error here.
10:55So one is like that's true,
10:57but I also need to say that it's in the local folder.
10:59Okay? So I need to import it from the local folder
11:02and it's not, yeah, it is variables.
11:04So let me just make sure I'm doing
11:05that OTO on the button itself, local folder.
11:08'cause if not, it's gonna search for it inside
11:10of the node modules.
11:12And we have one more error.
11:13So that's because we have now our path is
11:16different, we're not in the app.
11:17So do we need to go actually two folders back,
11:19not one folder back.
11:21So let's go ahead and save that
11:23and OTO do that in our jumbotron and save that as well.
11:27And if we go back into our app, we should be making
11:30progress, but we're not completely there.
11:32Okay? Right, because there's one more thing we have to learn
11:34before we actually get this to work.
11:36So worry not, we're not gonna fix it right now
11:39because in the next lecture we're gonna talk a little bit
11:41about mix-ins and then add those in
11:44to make our application work.
11:46Again. In this lecture we got us so much closer
11:50to creating dynamic CSS that will work
11:52for all of our components.
11:54In the next lecture, we're gonna intro,
11:56we're gonna introduce Mixings into our, our SAS to be able
12:00to really create crazy cool things.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!