SASS Tutorial (build your own CSS library) #14 - Making Utility Classes

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

Key Takeaways

This video tutorial demonstrates how to create utility classes for a CSS library using SASS, a CSS preprocessor, and applies these classes to HTML elements, similar to Bootstrap 5's utility methodology, utilizing tools like SASS and Bootstrap 5

Full Transcript

okay then so the next thing i'd like to do is to create some utility classes for this library that we're creating now in a lot of other css libraries and frameworks you often see classes like this like m hyphen 4 and that would be for margin in all directions and apply it to a strength of four or if you wanted padding left we'd use a class of p l padding left hyphen three and that would be a strength of three in the left direction our opacity o hyphen 40 which would mean 40 for opacity so we're going to generate these kind of classes in this lesson and it is going to be a little more complex than some of the things we've been working on so far so the first thing i want to do is create a file to create all of our utility classes in so inside the shinobi folder underscore utilities dot scss like so and then i also want to import that down here at the bottom so at import and it's going to be utilities like so all right then so in this utilities file now we're going to create basically a very big map of different utilities now the first thing i want to do is use the math module because we will be using that for some of the different values so sas math like so okay and then i'm going to create a utilities map so it's a variable which is a map now inside this map we're going to have different properties for example we're going to have a utility set for padding we might also have a utility set for padding left which will be a map as well and we might have a utility set for margin which would also be a map right and then later on we're going to cycle through this map we're going to get each one of these different properties and we're going to cycle through the different values inside that utility as well and generate classes for each one so what i'm going to do is first of all just start with the padding and in here i'm going to first of all give this a prefix value and that's going to be set equal to p now this will be basically our class name our root class name so if we want to apply padding to something it will be p something and it could be p hyphen zero to strip away the padding or p hyphen one to give it the base padding or p hyphen two to give it the base padding times 2 etc so that's the prefix of the class the next thing we're going to create is a property called values and this will be all the different values that we can have for padding so it could be strength 1 which is the base padding like i said strength two which is the base padding times two etc so this in itself is gonna be a map as well all right so inside this map i'm gonna say that first of all zero so it would be p hyphen zero and the value of that is going to be just the base or rather zero because we don't want any padding for p hyphen zero the next one would be one and that would be the base hyphen padding right and then we'll have two so strength two that would be p hyphen two and that would be the base padding and then we would multiply that by two now i'm gonna do the same thing but i'm just gonna paste these in right here so we have these different values all the way up to five will be times the base padding by eight so now we have all these different values for our padding utility now we don't have classes for these yet we're going to create those classes later on but what i'm going to do now is just paste in another one and this is going to be for padding left like this so this time we would have a prefix of pl that would be the class name the base class name and then the value zero three to five again this time it's only going to apply to padding left now the names of these utilities are important because they're gonna match the property names the css property names that we apply these classes to all right so i just want to leave it there for a second because what i want to do now is create some kind of sas code down here that's going to cycle through these different utilities and generate classes for each one so down here again i'm just going to paste this code in because i don't want you to watch me write this out from scratch but i'm going to explain everything that i do here so we're generating utility classes right that's what we're doing and we're saying for each property in the map and basically this is the property and this is the map that we get so normally we say key and value but in this case i'm being more explicit the property name which is the css property that we're going to basically apply values to and the map for that css property so for each property map in utilities which is the big map we have right here that's what this is called so we're cycling through that and for each one i'm getting first of all the prefix for each property so we use the map get function to get that we've seen that before and the map we want to get the prefix from is this map right here so whatever map is associated with that property and we're getting the prefix from that map so we're storing that right here so that could be p for margin or pl sorry p for padding or pl for padding left now the next thing we're doing is getting the values and again we say map get we want the map which is the map associated with whatever we're iterating at the minute and we want the value property of that which is also a map right so we're getting that right here the values so now we have the prefix which is the base class name and also the values which is a map of values we want to cycle through to generate classes for each value using this prefix as well so this time i'm using key and value but just short versions k and v so for each k v in values which is this we're cycling through those if k is default then we're basically just going to take the prefix right here and by the way k is the property name so we're saying if this is ever equal to default because later on we will have some default uh values then what we're doing is we're taking the prefix and we're just making a class out of that so it could just be for example pl on its own or p on its own now that's not the case for these because we don't have a default property but later we will so we just take that prefix and that is the class name and then also the property right here remember the property is these things right here so it could be the padding or the padding left in our case that's the property we're applying or right here so we're using a variable to create that property and then the value of that property is just whatever value we're cycling through in the values map right so if default doesn't exist which it doesn't in our case yet then all we're doing is taking the prefix and then also saying hyphen and we're adding on k which is this value so the prefix and then this value so hyphen zero or hyphen one or hyphen two and then we're applying the property here again in the selector and we're saying the value of that is v so for for example p l hyphen 1 it's going to be this because this is v down here all right so that's generating all of these different utility classes for us now if i save this and if we go to our index file i'm just going to open up the terminal to make sure there's no uh errors first of all and there are we can see right here we have an error it's saying that this is an undefined variable so let's go to the top have i spelt it correctly util it is all right so save it again now let me see okay that's got rid of the error so now if we go to the index.css file and search for pl for example we can see all of these different pl classes these utility classes so pl hyphen zero is padding left zero pl one padding left this pl2 etc and the same is going to be true for just padding in all directions as well all right so that's a nice way to cycle through these utilities right here so what i'm going to do now is just add in a load more utilities and then we're going to go through each one now i'm going to paste these and i'm going to copy them from my repo because there's an awful lot of these that i want to add and it would make no sense for me to write them all out from scratch that's going to take about 20 minutes so i'm pasting them all in and let me scroll up and let me go through each one of them quickly so we already had padding and padding left and remember these are the css property names they have to be the css property names we also have padding rights which is pr for the prefix the same values and padding top pt same values pb for padding bottom same values we do exactly the same thing then for margin the prefix is m this time instead of p but the values are pretty much the same only this time we use the base margin and times that by two four six eight so we have margin left we also have margin right we have margin top as well and margin bottom all right then we create an opacity one so again this is the css property name opacity the prefix this time is going to be zero and these are the values so if we say zero or not zero or rather oh hyphen 10 then the opacity is going to be 0.1 if we say oh hyphen 60 it's going to be 0.6 etc all right so the next one is border radius the property name and then the prefix for this the class base name is br for border radius and now we have a default case now we'll come to that in a second but we also have br high for none which is going to be this value be our hyphen xs for extra small which is this we take the border radius the base one and divide it by four using the math module small we divide by two large we take the base border radius and times it by two and full is fifty percent so this default case right here remember if we just add the class of br because the case is default it's not going to be br hyphen default for the class name it's just br because we have that if check down here if k is equal to default as we cycle through the values we don't tack on default to the class name it's just whatever the prefix is which in this case is just br right so that's what the default thing does if we just apply a class of br we get this base border radius otherwise it's br hyphen this high from this hyphen this or this etc all right next one is display the prefix is display values n for non b for block f for flex i've inline i hyphen beef inline block all right so font size again the prefix which is font the value small medium large xl and extra extra large and we're just using these font size variables that we created earlier on they are in the variables folder or file rather down here whew so that's just a few different utilities we've created right here and what we're doing now is cycling through the entire utilities map to generate classes for each one of these utilities that we've added so it would be very easy in the future to add extra utilities as well and you can do that if you want you can extend this for now i'm just going to save this and i'm going to check out the index.css file which will have absolutely ballooned because we're adding loads more classes in you can see down here we've got all the different directions of padding margin and if we scroll right down we can see opacity the border radius display and the font sizes as well and now we can use any of these different classes inside our index file so that's what we're going to do right here we're just going to output a few font sizes so let me paste those in right here like so so we have an h2 first of all and that is using a margin utility class margin bottom two so that's the strength of two and if we take a look inside the index and search for mb hyphen two we can see it's this amount margin bottom all right now over here we have the font size small inside of div and then we have font size medium then large then xl then xxl all right cool we also have this hr down here with a class of mt which is margin top of strength four and mb of strength four so margin bottom so what i'm going to do is also add this hr below the other section so below the buttons right here just so that we can separate our different sections of this webpage i'm also going to do it below the cards down here and then later it's going to be also below the grid system and also below the utilities i'm going to scroll up and place it below the buttons right here i think that's what this is or is it the colors who knows it's the colors all right i think that's it now we have it below every section so we're using those utility classes now inside our index file so if i save this now and preview it in a browser then we can see all of these different utility classes taking effect so this is the font sizes right here and we've also got these hr's right here which also have margin applied to them we can see that margin if we hover over the hr tag right here awesome so there we go my friends that is how we can generate all of these different utility classes using this utility map if you've used bootstrap 5 this is pretty similar to their utility methodology they have a big map with all these different utilities and they perform code to cycle through them obviously a bit more sophisticated than the one we've done here but this is a good start and if you wanted to you could extend this to add your own utility classes as well

Original Description

🐱‍👤 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 create utility classes for a CSS library using SASS and apply them to HTML elements, covering topics like Sass maps, utility-first approach, and CSS preprocessors. By following this tutorial, viewers can learn how to generate utility classes similar to Bootstrap 5's utility methodology and improve their CSS coding skills. The tutorial provides a comprehensive guide on how to create and apply utility classes, making it easier for viewers to understand and implem

Key Takeaways
  1. Create a file for utility classes
  2. Import the file at the bottom of the main file
  3. Use the math module for calculations
  4. Create a map of utilities with properties and values
  5. Cycle through the map to generate classes for each utility
  6. Paste code into the Sass file
  7. Cycle through the map to create classes for each property and value
  8. Use the map get function to get the prefix and values for each property
  9. Create classes with prefixes and values for padding and margin properties
  10. Use variables to create class names and apply properties
💡 The utility-first approach in CSS allows for more efficient and flexible styling of HTML elements, and using Sass maps to generate utility classes can simplify the process of creating and maintaining these classes.

Related Reads

Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →