SASS Tutorial (build your own CSS library) #18 - Using @extend

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

Key Takeaways

This video tutorial demonstrates the use of SASS, a CSS preprocessor, to build a CSS library, focusing on the @extend feature to inherit CSS properties from another rule, and creating a navbar with various styles and layouts.

Full Transcript

all right then gang so another feature we can use in sas is the extend feature now the extend feature allows us to extend or inherit css properties from another rule it's a little bit like using a mixing but with some differences which we're going to talk about later to begin with we'll see in action by creating a new simple navbar component for our library so let me first create a new file called underscore navbar.s css inside the components folder and then we also need to import this new file inside the index sas file where we bring everything together so let's do that below the other components by typing at import and then we want to go into the components folder and then forward slash navbar all right then so the first thing we need in this navbar file is a basic navbar rule so we're going to make a selector which is a class of navbar and then inside this selector i'm going to give it a padding property which will be the base padding variable that we made earlier in the y direction and then the base padding times 2 in the x direction up and down i'm also going to give this a box shadow property which will be the base box shadow variable that we made earlier as well all right so nested inside our nav bars we might have a site title right so i'm going to nest another rule inside this now bar which will be the class selector site title and then inside that we'll just give it a font size property and that is going to be the font size large variable that we created earlier as well all right so we have this basic navbar rule but i want to add some styles for its layout too i want it to display as flex i want to justify the items etc now i could add these properties in here directly but i'm not going to do that because it would make that group of properties less reusable so instead i'm going to make another rule at the top of the page called flex layout which is a class selector and we can see here the properties i've already added to it it's got a width of 100 display flex align items centrally that's in the vertical direction justify content using space between and then a box sizing of border box so these are basically all the properties i want to add to the navbar class down here so to do that i can just say down here at extend and then whatever or the select i want to extend in our case dot flex hyphen layout which is the name of the class up here at the top and that will basically apply all of these properties up here to this navbar class as well so like i said before this is a bit similar to a mixing in that it groups together a selection of css properties which can easily be reused in other selectors but there are a couple of differences first we don't pass arguments when we're using extend we literally just extend another css rule so if you need arguments you'd use a mixing second the way this is compiled into css is different than if we'd used a mixin so let me demo that if we go to the output css file and search for navbar then we're gonna see the rules are bunched together comma separated and they include those common extended styles and any other rule that extended the flex layout class would also be bunched up here in the same kind of list of rules now if we were using a mix in instead then they wouldn't become a separated like this they'd all have their own separate rules with the properties duplicated all right so now we know how to extend rules let's carry on with this navbar component in fact we'll try using this in our html page to see what it looks like so far so at the top of the body in here outside of the container i'm going to create a nav element and i'm going to give that a class of navbar which is what we just made a rule for i'm also going to give this a class of text high from white and a class of mb hyphen 4 to give it some margin at the bottom all right so inside that now we'll do an h2 with a class of site hyphen title which we also created a rule for to style the font size of this nav title right and i'll just type in here shinobi css okay so finally we're going to have a paragraph tag below the h2 with a bit of text inside him and i'll just type inside that a lightweight css library for dev ninjas and then now we've done that we can save this and we can take a look in the browser but actually before we do that i just want to get rid of this text white again because i just realized that if we do that the text isn't going to show up because we've not given this a background color yet so let me save that now and now we'll preview this in the background or rather in the browser and we can see this nav bar at the top so we have the title on the left and this way over on the right and it's over on the right because we have the space between value for the justified content property so there's a lot of space between these two things right here now this is good but i don't want it to be full width i want the nav bar in the background to be full width with this box shadow but the content i want to bring in to this central column right here so the title and this thing so what we could do is we could place a container around this content right here so let's try doing that i'm going to create now a div with a class of container and then i'm going to grab this stuff and i'm going to paste it inside here so now if i save this we can preview it again in the browser and you can see right here there's a bit of a problem the content inside the container is sitting on top of each other instead of being side to side like they were before because this p tag should be way over here to the right and the reason for this is that the container doesn't have a display flex property the navbar around it does so its immediate children will be flex items but if we choose to put a container inside the nav bar then the container is the immediate child of the nav not the things inside the container so the things inside the container will not be flex items so we need to make sure that the container when it's inside a navbar is displayed as flex so in essence we need to make the container inside a navbar extend the flex layout that we already created before so now let's nest the container class inside the navbar bomb and that way we're not affecting containers that sit outside of the nut bar only those inside it and then inside here we can extend the flex layout class again which means it will have all of those css properties applied to it so now we've done that we can save it and preview again okay and now this looks much better now we have that display flex of the container so this sits on the left and this sits on the right awesome okay so there's a couple more things i want to show you first currently we're not using this flex layout class anywhere in our html and we probably won't do in the future we just created it so that we can extend it in other rules however when the sas is compiled it still generates a class for this in the final css output file which we don't need so when this is the case when we don't need a rule to be output to the final css you can change this into a placeholder rule and we do this by deleting the dots and replacing it with a percentage sign and now when we compile this these are the rules we'll still extend this one and it will contain those properties but the placeholder rule itself up here will not be compiled to css and we can preview that by searching for the rule in our css file now and we should see that it's not there all right so that's one extra thing i wanted to show you the next thing i wanted to show you is how to make color variations of the navbar so we could have classes like napa primary or navbar purple or something like that so the way we're going to do that is by coming down here and using an each loop so we say each key and val and that's going to be in the colors maps that we created because we want to create a color variation for the nav bar so the class name is going to be navbar and then hyphen whatever the key is because remember if we go to the variables the color map up here the key are things like this primary secondary error info blue etc so the classes will be navbar hyphen primary or navbar hyphen secondary or hyphen purple or whatever other color is in this map okay so inside here all we want to do is extend the nav bar stuff now what do we do do we extend this right here well we don't have to we could extend this right we don't have to extend this and add the other properties on ourselves we could just extend the nav bar and then give it a background color on top of that and that's what we're going to do so we'll say at extend and it's going to be the navbar we want to extend and then all we need to do is say the background hyphen color of this is going to be the val like so and that's all there is to it now we should have all of the variation classes for the navbar and if we take a look inside the index you can see all of these now all of these different rules that are extending the nav bar and now if we go down here we can see we've got all of these different variations with a different background color as well so if i go to the index file i can come up here and say navbar hyphen primary to make a primary color navbar and if we take a look at that in the browser now we get that blue background color now i do want to make the text white so let's add back in that text hyphen white class that we had before we'll spell this correctly as well for a start save it and if we preview now that looks a lot better and we could have different variations if we wanted to as well so i could make this secondary or i can make it a different color and we get those different colors right here i'm going to change it back to primary because i think that looks better for this web page and that's how we use extend my friends to extend these type of properties and also how we can extend classes as well and remember this thing right here the percentage means that this is not going to get output in the final css

Original Description

Hey gang, in this SASS tutorial you'll learn how to use the @extend rule. 🐱‍👤 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 to build a CSS library, focusing on the @extend feature to inherit CSS properties from another rule, and creating a navbar with various styles and layouts. By following this tutorial, you can learn how to create reusable CSS code and improve your front-end development skills.

Key Takeaways
  1. Create a new file called _navbar.scss inside the components folder
  2. Import the new file in the index.scss file
  3. Define a basic navbar rule with padding and box shadow properties
  4. Nest a rule inside the navbar for the site title with font size property
  5. Create another rule at the top of the page called flex layout with display, width, align items, justify content, and box sizing properties
  6. Use @extend to extend a placeholder rule
  7. Use an each loop to generate color variation classes
  8. Override the background color of a class by extending it and adding a background color property
💡 The @extend feature in SASS allows you to inherit CSS properties from another rule, making it easier to create reusable CSS code and improve your front-end development skills.

Related Reads

Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →