How you can simplify your CSS with :is()

Kevin Powell · Advanced ·🛠️ AI Tools & Apps ·5y ago
Skills: HTML & CSS90%

Key Takeaways

Simplifies CSS using the :is() pseudo-class

Full Transcript

in this video we are exploring the is pseudo class and what is the pseudo class what does it do why does it matter how is it going to simplify your life because it definitely well it lets us write much drier css so we're going to be exploring all of that in this video stick around so this is in codepen right here if you do want to follow along you can just delete everything before general styling when you open the link in the description and you can follow along because this is all this is just general styling here we're going to start right here and we're going to be looking at the is pseudo class what is it what is it for and i'm sure in one point or another you've had selectors where you want to select say you have a header and you want to get both the h1 and the link in here to be a different color compared to maybe things you have in other places so you come in and you do something like header h1 comma header header you got to do things right like that header h1 header a and then we can say a color of let's go with lime green for something interesting and look at that they're lime green don't use that color on that background it is terrible accessibility but it is working and you've selected both of them so we have a compound selector here and with this compound selector we have a comma separating so we can do multiple selectors now if you can get really complex with these now we do want to be careful with compound selectors like this where we're spacing them out we're getting the children i do think it's fine doing this but you just want to be a little bit careful and not go overboard it pushes up specificity there can be other issues but sometimes it's very useful and you can get much drier css by chaining your selectors together like this and having comma separated ones but you can also run into issues a on some really big things it can get really bulky but if you put an invalid selector here and i don't know how this would happen but say you're going fast or something and you do that and that means nothing or whatever it is something happens and for some reason you have this big chain of selectors you have all of these different things and one of them is invalid it doesn't work in your code anymore this entire css rule here it is gone in the garbage the browser ignores the entire thing this is very different than when you have a typo here so if i said uh like color is one t 0 p and the browser doesn't know what that is this is very different right because then this one line of code will get ignored but if you have something like this it's throwing the entire rule out let's even you know for fun font family we'll go with a serif just so we can see a difference we'll say that the colors there we'll see a font size of 10 ram just so we can really see nothing is working but then when i fix that one little thing the entire thing will kick in and it works everywhere and it looks terrible um but just to show you how that is working and why you do have to be a little bit careful i don't think that's something you'll run into often but it can be really annoying when it does happen where is comes in is instead of writing things like this what you could do is you could say header and then here we could say is and inside of this we can put all the different things that we want so i could say h1 comma a and here we can see that this is working for my serif and then let's come in here with the color we won't use lime green let's use uh turquoise now turquoise you gotta spell spell things right turquo baseball turquoise turquoise ha i got it that's even worse than before okay let's go with a dark color there we go so we can see that it's working so here uh what we're doing is a header is and then h1 and comma a so the nice thing with this is you don't need to have such a big rule you can sort of space things out a little bit like this you're only putting the parent selector once when you have this um when using a compound selector like this one you only have to list this at once so like say you had other things in here you had a header say we had a button that could have end up in there and then here you'd have a header let's just say if ever there's a small tag for some reason anything like that you know this gets longer and longer and longer and when you want to make a change in here it can be a little bit annoying so here it's a little bit easier where you can do h1 then dot btn comma small and you can list them all out here so you only have to do it on one selector instead of having a whole bunch of selectors and the other thing that's nice with this you can see my text is all purple is if i muck one of these up so this button somehow it ends up being invalid and i'm using something that doesn't work or i shouldn't do on the button actually we should leave that as a class i'll do it on the a again is you'll see that the links actually uh purple's a bad color though let's go back with green because one of them was purple by default but you can see now this is green um but the green isn't working on my links down here and the reason that's happening is because this is my invalid selector so only the invalid selector is being thrown out it's not ditching the entire rule so it's much more forgiving on things like that so that is a nice benefit of the is selector like this and another way we could use the is selector in a bit more of a cool way is you don't have to use it the way i was doing it so just to reiterate a little bit here we have my card then we could say is h2 comma a so we could select my h2 and my a that are in there and say color is red and now over here you can see that and my links inside of here have all changed to red but another really cool thing you can actually do here instead of doing it that way is the other way around so what you could do is you could actually say something like we could start with our is selector so we could say is uh we could even say header space card dot card and just for fun i don't know why you would want to do this but then we could say uh h or then we in here we could say p hover and i don't know why you do it on a paragraph maybe a link would make more sense but p hover here is color of red quick example just so we can see it actually working and you can see all of these paragraphs now are working this i think is the more useful used uh use of this you know this while this is can be a little bit bulky here this is the equivalent of writing header p hover comma uh dot card p hover and then comma and you keep going like that you have to keep doing your p hover here so it's kind of cool that you can just have that one p hover out here so you list all the things you want put the one compound here and it just works just like that now there is one really important thing that you're going to have to take into account if you're doing this um and i don't think you'll run into it too often but there is a specific specificity specificity i got it a specificity thing that comes up when you are doing this and so on this if i say card and then we do our is selector our pseudo pseudo class here and we say is title comma key and we say color is red now what's really interesting here is you can see they've all changed to red and later on maybe you forgot you did that and then here you have a card p and this has a color of yellow well it actually will be staying there we go it is staying red and that's because this actually has higher specificity than this even though you would think this is my card with my p on here the is itself does not contribute to the specificity but what does is this title here so it's whatever you have here the height the thing with the highest specificity within this group is what is getting the we're just setting the specificity of all of this um so you do want to be a little bit careful with that um and just to show you that this would work uh without the is there if i just put is p here like that now the yellow is actually going to kick in because this specificity is the same or equal to this one and this one is coming second because if i move that up to the top here now they will switch to red because this is coming second so you do have to be a little bit careful with that and just know that it's the thing with the highest specificity within this that is working another thing another little thing along the way that could be important to know as well is you can do some interesting stuff so i could actually say p space a here and you can see that the paragraph uh the links inside the paragraph are changing to red so you can do complex selectors within the is here i'm not sure what the browser support is for that i know complex selectors within these pseudo classes like not and other things the support is gaining but it's not everywhere so you might want to be a little bit careful with that or just double check and do some cross browser compatibility but for overall browser support it's actually really good and as long as you don't worry too much about internet explorer these days which i hope you're not having to i know some people have to because of their work but overall i think internet explorer is something we don't have to worry about anymore and if that is the case then the is selector or the pseudo class i should say is good to go and you can definitely be using it on your projects and that is it that is the is pseudo class i really hope that you like this if you'd like to know more advanced selectors and combinators and all of that type of thing i do have a video that dives into it it should be showing up on the screen right now or it is also linked down in the description below as well thank you very much for watching i really hope you enjoyed this and i hope you can find some good use cases for it we just sort of scratched the surface here but it is something that i'll definitely be looking at using more and more in my projects going forward a huge thank you to my patrons for their support each and every month including zach who is my enabler of awesome and of course until next time don't forget to make your corn on the internet just a little bit more awesome

Original Description

The :is() pseudo-class is relatively new, but browser support is pretty good now! It's a nice way to dry up your CSS and turn some otherwise complex selectors into something much more managable! Codepen: https://codepen.io/kevinpowell/pen/RwojMKB Step up your game with advanced CSS selectors and combinators: https://www.youtube.com/watch?v=Bcr70LIJcOk&list=PL4-IK0AVhVjPRj8P56TtFX2hg33BlbT3x&index=2 #css -- Come hang out with other dev's in my Discord Community 💬 https://discord.gg/nTYCvrK --- Keep up to date with everything I'm up to ✉ https://www.kevinpowell.co/newsletter --- Help support my channel 👨‍🎓 Get a course: https://www.kevinpowell.co/courses 👕 Buy a shirt: https://teespring.com/stores/making-the-internet-awesome 💖 Support me on Patreon: https://www.patreon.com/kevinpowell --- My editor: VS Code - https://code.visualstudio.com/ --- I'm on some other places on the internet too! If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter. Instagram: https://www.instagram.com/kevinpowell.co/ Twitter: https://twitter.com/KevinJPowell Codepen: https://codepen.io/kevinpowell/ Github: https://github.com/kevin-powell --- And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Kevin Powell · Kevin Powell · 0 of 60

← Previous Next →
1 How to create an awesome navigation bar with HTML & CSS
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
2 Improve your CSS by Keepin' it DRY
Improve your CSS by Keepin' it DRY
Kevin Powell
3 HTML & CSS for Beginners Part 6: Images
HTML & CSS for Beginners Part 6: Images
Kevin Powell
4 HTML & CSS for Beginners Part 7: File Structure
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
5 HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
6 HTML & CSS for Beginners Part 5: Links
HTML & CSS for Beginners Part 5: Links
Kevin Powell
7 HTML & CSS for Beginners Part 3: Paragraphs and Headings
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
8 HTML and CSS for Beginners Part 1: Introduction to HTML
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
9 HTML and CSS for Beginners Part 2: Building your first web page!
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
10 HTML & CSS for Beginner Part 8: Introduction to CSS
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
11 HTML & CSS for Beginners Part 9: External CSS
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
12 HTML & CSS for Beginners Part 10: Divs & Spans
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
13 HTML & CSS for Beginners Part 11: Classes & IDs
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
14 HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
15 HTML & CSS for Beginners Part 13: Background Images
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
16 HTML & CSS for Beginners Part 14: Style Text with CSS
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
17 HTML & CSS for Beginners Part 15: How to style links
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
18 HTML & CSS for Beginners Part 16: CSS selectors and Specificity
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
19 HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
20 HTML & CSS for Beginners Part 18: How Floats and Clears work
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
21 HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
22 HTML & CSS for Beginners Part 20: How to center a div
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
23 HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
24 HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
25 How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
26 How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
27 How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
28 How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
29 How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
30 Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
31 End of the year upate and what's coming to my channel to start the new year
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
32 Create a CSS only Mega Dropdown Menu
Create a CSS only Mega Dropdown Menu
Kevin Powell
33 CSS Tutorial: Outline and Outline Offset
CSS Tutorial: Outline and Outline Offset
Kevin Powell
34 CSS Blending Modes
CSS Blending Modes
Kevin Powell
35 Parallax effect | 2 different ways to add it with jQuery
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
36 CSS Units: vh, vw, vmin, vmax #css #responsive #design
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
37 How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
38 100 Subscribers speed coding bonus video
100 Subscribers speed coding bonus video
Kevin Powell
39 How to Create a Website - Complete workflow | Part 02: The Markup #HTML
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
40 How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
41 How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
42 How to Create a Website - Complete workflow | Part 05: Typography & Buttons
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
43 How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
44 How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
45 Redesigning & Coding My Website #CreateICG
Redesigning & Coding My Website #CreateICG
Kevin Powell
46 How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
47 How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
48 How to Create a Website - Complete workflow | Part 09: The CTA and Footer
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
49 How to Create a Website - Complete workflow | Part 10: Making it responsive
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
50 How to Create a Website - Complete workflow | Part 11: Making it responsive con't
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
51 How to Create a Website - Complete workflow | Part 12: Putting the site online
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
52 Create a Custom Grid System with CSS Calc() and Sass
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
53 CSS em and rem explained #CSS #responsive
CSS em and rem explained #CSS #responsive
Kevin Powell
54 Should you use Bootstrap?
Should you use Bootstrap?
Kevin Powell
55 How to add Smooth Scrolling to your one page website with jQuery
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
56 Let's learn Bootstrap 4
Let's learn Bootstrap 4
Kevin Powell
57 How I approach designing a website - my thought process
How I approach designing a website - my thought process
Kevin Powell
58 Build a website with Bootstrap 4 - Part 1: The setup
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
59 Build a website with Bootstrap 4 - Introduction
Build a website with Bootstrap 4 - Introduction
Kevin Powell
60 Build a website with Bootstrap 4 - Part 2:  Customizing Variables
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell

Related Reads

📰
Three Token-2022 Mints in One Week: Fees, Yield, and Soulbound
Learn how to build three different Token-2022 mints on Solana devnet in one week, including fee-bearing, interest-accruing, and soulbound tokens
Dev.to · atharv shukla
📰
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Maximize Google Workspace AI power by safeguarding data and boosting performance to stay ahead in 2026
Dev.to AI
📰
What is Gemini Spark, and what can it actually do for you?
Gemini Spark integrates AI into daily Google apps to enhance productivity, learn how to leverage it
TechCabal
📰
How I use python to save hours every week
Learn how to use Python to automate tasks and save hours every week, with practical steps for beginners and experienced users alike.
Dev.to AI
Up next
Implement Microsoft Entra ID Auth with Delegated Graph API Calls in ASP.NET Core Web App Razor Pages
Dewiride Technologies
Watch →