SASS Tutorial (build your own CSS library) #12 - Mixins

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

This video tutorial demonstrates how to use SASS mixins to create reusable CSS code for buttons and other UI components, covering topics such as creating and including mixins, passing values and arguments, and applying default values and hover effects. The tutorial utilizes SASS as the primary tool for building a CSS library.

Full Transcript

all right gang so in this video i want to talk about another feature in sas mixins so mixins are a way for us to group together a bunch of css properties and values so that it can be included in many different css rules for example it might be that a group of different elements on our web page all contain the same padding margin color and font size properties and values now instead of rewriting those properties and values out for each of those elements in their own css rules we could create a mixing for those properties and values which could then be dropped in to those element rules now that might seem a little bit like gobbledygook for now but let's see an example of this in action by creating a new component and this component is going to be a button component so let's create that file inside the components folder so underscore button dot scss and inside here we'll create our button component all right then so in this file what i want to do is generate a button class for all of our different colors so that we can have a primary button or a secondary button and they're going to have those different background colors so again we're going to cycle through oops if we go to variables this color map right here so to do that remember we need to use an each loop so i'm going to say at each and then i'm going to say key val so key val and it's in the colors map and what i want to do is generate a button hyphen and then the key value right here so we say hash curly braces and then dollar sign key and generate a class for that so it's going to be button hyphen primary or button hyphen secondary etc now what i'm going to do is paste in some properties right here so we have text decoration of none because it is pointer display inline block border of none the padding is the base padding that we have declared in our variables file down here somewhere the base padding so we take that and we have that for up and down and then in the left and right direction is the base padding times two the border radius is the base border radius and the background color is the val right here of what we're cycling through currently in the colors map all right so we're generating this class for each of those colors now if i save this and check out our index.css then hopefully we're going to see that i'm going to just search for button and we don't get any results let me oh that's because we've not included it in the index file so let me include it right here components forward slash button like so then we'll save it and hopefully now yep we can see button primary button secondary button error etc all the different colors have their own button class now which is good all right so say we also want another variation for example we might want an outlined version so what i could do is generate another class to say btn hyphen outlined like so and then it's going to be whatever the key is so hyphen and then hash curly braces and the key variable so that's going to be primary secondary etc and then inside here i want to paste all these properties in again now the difference this time is that maybe the border is going to be different so i could say the border is going to have the base hyphen border hyphen and it's going to be solid and it's going to be the color of val so we need our colon right here these right here these are all the same here and here and in fact the background color for this one is just going to be white so let's do fff so with the exception of these two rules or these two properties the background color and the border and this right here the rest of these properties and values are the same in both of these selectors now we're just duplicating code here so what we can do is create a mixing which includes these different things right here these different properties and values so we just declare them once and then we include that mixing in both of these different selectors so let's try that what i'm going to do is come to the top and create a mixing now and the way we do that is by saying at mixin and then give this a name i'm going to call it btn for button and then we do parentheses and then our curly braces okay so now i can just paste in these properties right here now i'm going to take out the background color for now because that's different in each case the background color is the vowel here and over here it's white so now what i can do is delete all of these properties that i declared inside the mixing on each one of them right and then what i can do is just include this mixing in each of these selectors and the way we do that is by saying at include and then whichever mixing we want to include so we want to include the button mixing like so and i'm going to do exactly the same for down here all right then so now hopefully that's going to work exactly the same way and if i go to the index css file we're going to see we still get all of these classes with all of these different properties right here so that's working that's an easy way to bunch together a group of different properties and values that we can reuse in other selectors using this include keyword right here now you might be thinking why do we have parentheses up here and down here and that's because we can pass values or arguments into mixins so notice both of these selectors right here they both have a background color property now the value is different in each case but they both have the property so what we could do is we could actually set a background color property right here to be something and i'm just going to set it to be equal to some kind of variable called bg hyphen color it doesn't exist yet but what we can do is we can take in a variable or a parameter into this mixing which i'm going to call bg hyphen color right here and now all we have to do is pass that in when we call or include the mixing so i could pass it in right here to be val and that would mean that this right here when we call the mixing over here bg color is taking on this value val and when we set the background color it's going to be val so now we can delete it from here because we're doing it inside the mixing and the same for over here only this time we can just pass in the white color instead this hash code so i can copy that and paste it in and we pass in fff so this time when we include it the background color is going to be fff and we can get rid of this thing right here so if we now save this hopefully fingers crossed this is still going to work if i go to the css we can see the background color is going to be different in all of these different cases all right so we still have the blue we still have the outline blue and we have the red etc cool so that is mix-ins i do want to do a couple more things in here first of all i want to create a generic button class so this is if we want to have some kind of button in our template that's not necessarily one of these where we give it a background color of primary or secondary or something else it's just going to be a blank button so i'm going to say at include btn now then what's going to happen here because i'm not using parentheses and i'm not passing in a value so when it comes to this it's going to look at the bg color and say well that doesn't exist so what do i do for this well what we can do is specify a default value so i could say right here that this is going to be e2 so let me find the e2 e2 e2 which is kind of like a really light gray color and what's going to happen here is the mixing is going to say look if you don't pass me a value explicitly then i'm going to use this default value instead so in the case of this or this it's going to use these values instead of the default but in the case of this right here when we don't pass one in it's going to use this default color right here so the background color of this class is just going to be this gray color so that would be a generic button so if we don't want a button primary or a button secondary or something else we can just use this class and we get the generic color so that's the first thing i wanted to do secondly i also want to apply hover classes or hover effects to these classes so let me do that i'm going to say and so we're using the parent selector colon hover and then all we're going to do is give this a different background color when we hover so background color is going to be lighten like so and this is a built-in sass function and all it does is take a color which is going to be the val in our case that we have right here and lighten it by a certain percent now ours is just going to be by 5 all right and then for the hover case down here we'll say and colon hover it's going to be slightly different we're just going to say the background color this time is val because initially the background color in this case is white it's an outlined button so the background is white and instead we have a border which is the vowel color but when you hover over it we're making the background color the value as well so it could be primary secondary red purple whatever all right then so if i save this now we could try this out in our index.html file so i'm going to come here where we have the buttons comment and just paste in a bit of html so you don't have to watch me type it out and we have a series of anchor tags and each one has some classes the first one is just that default button class so if we go back over here we can see it's just this so it's going to include these properties then we have button primary and text white button secondary text white so we're applying this text white class as well to all of these because by default when we have buttons we're not giving it a text color and i want to make the text color white in these cases so it's down to us as developers here what color we want our text to be we can make it purple if we want but in some cases that's going to clash with the background color so i've gone for white so we have these different variations of the buttons we could have others as well like button hyphen purple or button hyphen red but i've just done a few but then also we have these button outlined ones which is these other buttons and down here button outlined and then the key so we have if we go back to the index button outlined purple button outlined orange and then we've made the text purple as well ourselves because again we don't specify the text color right here we have to do that ourselves because it's down to then the user using this library what color they want their text to be we just start the button with this the text is up to the user creating the template and then also when you hover over the button we've said make the text white because when you hover over these remember we're changing the background color of it to be purple or orange or whatever color we're using so then let's save this now and preview in a browser all right cool so now we can see all of these different buttons we have the default button right here with no theme attached to it then we have the primary the secondary the error and the info and when we hover over these they go a little bit lighter remember because we said on hover do that use the lighting function and then we have the outline buttons which look like this and when we hover over those the background goes the theme color and the text goes white so that's our second proper component we have the cards now and also these buttons and to do these we used mixins to group together a load of comment properties and values so we don't have to keep writing them out over and over

Original Description

Hey gang, in this SASS tutorial we'll learn how to use mixins to reuse a collection of css properties in other rules. 🐱‍👤 Get the full SASS course now on Net Ninja Pro: https://netninja.dev/p/complete-sass-tutorial 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/complete-sass-tutorial 🐱‍💻 HTML & CSS Crash Course: On Net Ninja Pro - https://netninja.dev/p/html-css-crash-course On YouTube - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G 🐱‍💻 Modern JavaScript Course: On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 CSS Flexbox Course (on YouTube): https://www.youtube.com/watch?v=Y8zMYaD1bz0&list=PL4cUxeGkcC9i3FXJSUfmsNOx8E7u6UuhG 🐱‍💻 SASS docs - https://sass-lang.com/documentation 🐱‍💻 VS Code - https://code.visualstudio.com/ -------------- Net Ninja Links: 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/ 🐱‍👤 Get access premium courses on Net Ninja Pro: https://netninja.dev/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video tutorial teaches how to use SASS mixins to create reusable CSS code for buttons and other UI components, covering topics such as creating and including mixins, passing values and arguments, and applying default values and hover effects. By following this tutorial, viewers can learn how to build a CSS library using SASS. The tutorial is beginner-friendly and provides a comprehensive introduction to SASS mixins.

Key Takeaways
  1. Create a new component file for the button component
  2. Generate a button class for all different colors using an each loop and the colors map
  3. Create a mixin for button styles with different colors and variations
  4. Generate classes for different button colors and variations using the mixin
  5. Create a mixin for outlined button styles
  6. Specify a default value for a mixin in SASS
  7. Use the parent selector to apply hover classes or effects to a mixin
  8. Use built-in SASS functions like `lighten` to change the color of an element on hover
💡 Using SASS mixins can help create reusable CSS code and streamline the development process for building UI components.

Related Reads

📰
Angular vs React: Key Differences, Pros & Cons Compared
Learn the key differences between Angular and React to decide which framework suits your project needs, considering factors like team size, project complexity, and desired level of flexibility.
Dev.to · Elsie Rainee
📰
Next.js Web App Development: Complete Guide for Fast, Scalable Applications (2026)
Learn how to build fast and scalable web applications using Next.js, a popular React framework, to improve user experience and conversion rates
Dev.to · Avanexa Technologies
📰
Next.js Quietly Fixed the Prefetch Problem Nobody Wanted to Talk About
Next.js fixed the prefetch problem, learn how to leverage this fix for better performance
Medium · JavaScript
📰
A Fast Request, a Fast Parse and a Slow Page
Optimize each phase of webpage loading to achieve target performance
Medium · JavaScript
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →