CSS Cascade Layers Tutorial #1 - Selector Specificity 101

Net Ninja · Beginner ·🌐 Frontend Engineering ·2y ago
Skills: HTML & CSS80%

Key Takeaways

The video tutorial covers CSS Cascade Layers and selector specificity, demonstrating how to use cascade layers to group selectors and reduce conflicts in CSS stylesheets, utilizing tools like CSS libraries and cascade layers.

Full Transcript

hey there gang and welcome to this crash course all about CSS Cascade [Music] layers okay then so if you've been using CSS for any length of time then you've probably been hit in the face on several occasions with conflicting Styles like this for example where I've created a BTN class for any elements including links they should be styled as buttons and they're meant to look like this one right here in the browser but at the bottom of the article the link with a class of B BN isn't styled that way and it should be according to that BTN class right and that's because we've got a conflict going on between that BTN selector and a selector that targets all anchor tags inside an article tag within this main content class and because of the rules of selector specificity the selector which targets all the links in the article wins and those CSS rules are applied instead of the BTM ones so then it becomes a game of trying to make the button selector longer and more specific so that it wins the conflict battle or even worse throwing a few important keywords here and there and both of these workarounds are not ideal because they just escalate the problem of conflicting Styles in the future and you sometimes end up with a crazy CSS soup of longar selectors and important statements on every other rule so to combat this we now have something called Cascade layers to group selectors together within different layers and these Cascade layers remove some of the headaches that come with conflicting selectors and specificity so in this series we're going to be taking a close look at what Cascade layers are and how we use them to good effect but to begin with I think it's best to recap on what exactly selector specificity is and how the browser determines which selectors are more specific than others so let's go back to this BTN class example which is basically targeting any element with a class of BTN and that ultimately includes anchor tags that have that class but it could be other elements too like button elements and then we also have another selector which is targeting all anchor tags in inside the article tag within the main content class and in this case we get a bit of crossover where we have an anchor tag with a class of BTM which might also be inside an article element within the main content and that means for this anchor tag we have two selectors trying to style it and those styles conflict with each other one of the selectors wants to make the text white the other green and one wants to make the background green the other transparent so the browser needs to make a decision now about how to style this anchor tag whether to use the GN class Styles or the article anchor tag Styles within the main content and it does this by trying to figure out which of these selectors is more specific in this case it determines the article anchor tag within the main content to be more specific and it uses this selector to style it but how exactly did it determine which one was more specific well in CSS we have different groups of selectors and each group of selectors has a weight associated with it so type selectors which are selectors which Target elements directly like P H2 article a All That Jazz they have a weight of one class selectors have a weight of 10 so that's things like BTN or leading and ID selectors have a weight of 100 and that's for any selector which starts with the hash sign to select an element with a given ID something we don't really do much of in CSS these days so when we make a complete selector we might use a combination of these different things to Target specific elements for example I could write a select which was just BTN and the total weight of that selector would just be 10 because it's just a class selector on its own but then I could write another selector dot main content then article then a and to work out the total weight of that selector we' apply weights to each individual part of it and then add them all up in this case we start with a class selector which has a weight of 10 then a type selector which has a weight of one and then another type selector which also has a weight of one so the total weight of this full selector would be 10+ 1 + 1 which is 12 so the first selector had a total weight of 10 and the second selector had a total weight of 12 and that means if these selectors both end up targeting the same elements and have conflicting Styles then the selector with the highest total weight wins and those Styles will be applied in this case that's the second select with a total weight of 12 and that's why the BTN Styles don't get applied to the Anchor tag inside the article in the browser so with that in mind let's have a quick look at another couple of examples and try and figure out which selectors have the highest total weight or in other words which selectors are more specific all right so we've got two sets of selectors really we've got this set which is both targeted in the same thing and we've got this set and both of these are targetting the same thing so we've got to work out which is more specific out of these two and then which one's more specific out of these two so let's start with this one we've got a type selector which has a weight of one another type selector P which has a we to one so that's two so far then a class selector that's 10 so that's 12 in total then this thing right here so 13 in total right so the weight of this is 13 this is also targeting the same span but we have a different selector so we have a 10 for the class selector one for the type and then one for the type so that's 12 so this is 12 that's 13 so therefore this selector would win any kind of Battle of conflicts if they were you know styling the same properties differently all right then so let's look at this one bit longer so we have this type selector which is one then this which is one so two then the class so 10 so that's 12 in total 13 14 and then another 10 so 24 okay this one 10 11 12 13 23 and then 24 so they're both the same this is 24 and this is 24 so which selector would win the battle out of these two well it really depends on which one comes last in the Star sheet in that case so if if both selectors have the same weight or specificity then the one further down in the stylesheet would take precedence over the one Higher above okay so this one in this case would win all right so that's just a couple of examples now these examples we've been going over are quite trivial on their own and they can be easily rectified by slightly altering selectors to make them more or less specific but in larger code bases for larger projects where you have multiple large stylesheets perhaps it's not always that simple and it can cause quite a lot of headaches when you're trying to juggle hundreds of different selectors now one of the so-called cures to these headaches would be to follow CSS best practices for example always using single class selectors in your CSS and nothing more that way every selector is of equal weight and only the OD of the selectors in the sty sheet dictates which rules effectively win but that's not always possible because in some cases you might be integrating third part to CSS libraries and you might have no control of how they structure their selectors also a lot of the time we might not have direct access to the template code so can't dictate what elements have what classes and so forth so following so-called best practices doesn't always pull you out of a hole another technique that some people advocate for is doubling up on selectors to add more weight to them when you need to so this means the selector becomes twice as weighty as it was to begin with and therefore twice as specific if you like but this technique can end up escalating the problem rather than solving it the more you reach for it you might also be tempted to throw in a few important statements to override specific rules but this is an anti-pattern in CSS and when we use it without consideration it can make your stylesheets really Mucky and make the problem even worse and before long you'll end up with important statements in every other selector somewhere so if none of these Solutions are really viable options what is the answer to this problem of conflicting rules well this is where Cascade layers can play a really big part and we're going to talk about exactly what they are and how we use them in the next lesson

Original Description

Learn all about CSS Cascade Layers & how to use them in this CSS series. 🚀🥷🏼Get early access to this entire course now on Net Ninja Pro: https://netninja.dev/p/css-cascade-layers-crash-course 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/cascade-layers-tutorial 💻🥷🏼 HTML & CSS Crash Course: on YouTube - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G on Net Ninja Pro - https://netninja.dev/p/html-css-crash-course 🔗🥷🏼 VS Code - https://code.visualstudio.com/
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 CSS tutorial introduces the concept of cascade layers and selector specificity, providing a foundation for writing more efficient and conflict-free CSS code. By understanding how selector specificity works and using cascade layers, developers can improve the organization and maintainability of their CSS stylesheets. The tutorial also covers common issues with selector specificity and how cascade layers can help resolve them.

Key Takeaways
  1. Understand the basics of selector specificity in CSS
  2. Learn how to calculate the weight of different selectors
  3. Use cascade layers to group selectors and reduce conflicts
  4. Avoid using important statements to prevent 'mucky' stylesheets
  5. Apply cascade layers to resolve conflicts with third-party CSS libraries
💡 Cascade layers can be used to group selectors together and reduce conflicts between them, making it easier to manage and maintain complex CSS stylesheets.

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →