Skip to main content
View on YouTube to comment and like!

Syntax Video

CSS :has - Use JS Less and CSS More - 10 Real World Use Cases

Chapter 1 0:00

Intro

the CSS has pseudo selector is supported across all modern web browsers and in this video I'm going to show you 10 examples of how you can start using it today I'm CJ welcome to syntax now all of the tips I'm going to show you originated from a tweet by West boss so he tweeted 10 CSS has tips and then in episode 714 of syntax he discussed all of those tips with Scott and I'm going to show you all the same tips but I'm going to show you some real world examples and all of the examples in codee I'm showing you are going to be available at the link in the description so you'll be able to play around with and kind of inspect every single thing that we talk about today first let's start with the basics and the simple idea is Select some parent element if it has some child element so in this example we're going to change the background color of a post if it has an iframe if it has an audio or if it has only an audio and not iframe so in this example you can see this post here is styled green because it only has an

Chapter 2 0:14

Where to find these examples

Chapter 3 0:40

The Basics

iframe inside it it this next one is red because it has an audio element and an if frame inside of it this next one is blue because it only has an audio element and so here you're seeing us combine it with the not pseudo selector that says if this post does not have an iframe and it does have audio then change the background to blue and then this last one doesn't apply to any so it just has the default styles of a post next up we're calling this the anywhere selector and what this allows us to do is if something exists on the page we will select some other thing on the page so in this example we are blurring the answers here of this question and answer set if this input box is checked if we uncheck it we are unblurring them also we're changing the label of the checkbox and it's important to note that we're not using any JavaScript in this example it's all done with CSS has so we say if the body has an input with the blur answers class and it is checked select all answer elements on the page so all elements with the answer class and blur them out we can see this in action if we click on any of our answer classes here right now since

Chapter 4 1:21

Anywhere Selector #1

it's checked they have have this class applied but if we uncheck them and we take a look they no longer have that class applied and in order to change the label here we're actually just changing the content using CSS so we have that same has selector here so if that input box is checked then select the visibility label and change the content before that element and then if it is not checked select the visibility label and change it to show and you'll notice here we're we're again combining this with the not pseudo selector so if the body does not have this element then apply this class we can kind of see that in action right here so if we look at the before pseudo element we can see that this is being applied so it's changed the content to show and then if we check it it is now hide this next example is also of the like anywhere selector and this is actually a real world use case that we're using on the syntax website and the idea is we have a nav that on every other page except for

Chapter 5 2:46

Anywhere Selector #2

the homepage we want to show the logo but when we're on the homepage this homepage hero section exists so we want to hide that logo and so you can see that example here in the nav you can see the logo right now is actually display none because the homepage hero section exists also we're changing the flex alignment of this nav here uh based on if that logo is in there or not because if the logo isn't in there we actually want to flex end everything now if we take a look at the about page we'll see that the logo isn't hidden and that's because the homepage hero section doesn't exist because we're on the about page also the uh alignment of that nav Flex box is just using its default style of space between now that the logo actually is displayed next up is the previous element selector and developers have wanted to do this for so long literally select me if I have some sibling and it's it's super simple so essentially what we do is we have the sibling selector here inside of the has

Chapter 6 3:41

Previous Element

and so this says if a div has a element with target class as a sibling select this div and so we can see right here this div that we've selected based on the fact that its sibling has the target class but we're actually selecting this div and then setting that yellow background next up are Target layouts and the idea here is you might want to change the layout of a CSS grid depending on its contents and so this example comes from a slide deck that Wes was creating where when a slide only has header elements we want to set the grid to show the header element across all four cells but if a slide has a header and an image we want the header in the top two cells and the image in the bottom two cells and then if a slide has a header and two images we want the header in the top two cells and then each image spread across to those two separate cells and we can see that in action here so this first slide only has a header element inside of it so it gets this specific grid template areas this

Chapter 7 4:13

Target Layouts

next slide has a header and an image so it gets this grid template areas and then this last slide has a header and then two images so we give it this layout now if those last examples didn't blow your mind this one absolutely will and so for the forever we almost always have needed JavaScript to hide and show elements based on the validation status of an input or change the background color of a label based on the validation status of an input but now we can do it all with CSS so everything I'm about to show you is just CSS there is no JavaScript involved and let me just talk about the example first before we dive into the code so this input here is using browser form validation we have Min set to zero um but anytime you're using like Min or Max or pattern or required those are things that are just built into the web browser so here we have Min set to zero so if I put in a value that's negative I'll need to blur the input to kick in the status of the validation but if I blur the input you'll see we are showing an error message and changing the background

Chapter 8 5:05

Form Validation

color to be red and now that the input has been blurred if I put in a valid input we're changing the background to green and displaying a success message and the field set example Works in a similar way except we are changing the Border color instead of the background color now the code to change the background color and the Border color is fairly simple we just say in the case of the uh label if the label has an input inside of it that has the user valid status now user valid and user invalid are just built into the web browser so if we're using these validation attributes on inputs we just get those for free we don't have to add those manually basically when the user blurs the input the browser is going to apply user valid or user invalid based on the stat validation status of that input and so if it's valid we're changing the background to Green if it's invalid we're changing the background to Red now for the field set very similar but we're just changing the Border left instead now hiding and showing the success and error messages is a little more

complicated but it's duable so in the case of a label the elements with the success and error class are actually siblings and so we we are using the subsequent sibling selector the TIY to say if a label has an input that has the user valid status so label has an input inside of it with the user valid status then select the subsequent sibling with the success class and display it uh if it has the user invalid status select the subsequent sibling with the error class and display it and by default they are hidden and for the field set it's a little bit easier because those are actually nested inside the field set so we don't have to use the TIY we can just say if a field set has a an input with user invalid status select the success element inside of that field set and display it or in the case of invalid select the error element and display it this next example will allow us to select all of the siblings of a hover element so here you can see if I hover over one of these all of its siblings uh get a lower opacity so that way the one

Chapter 9 7:42

All Siblings

we're hovering over stands out a little bit more and the way that we do this is we first use the parent selector so this says select the cards element which is the parent that has a card inside of it that is currently being hovered from there select all cards inside of the cards element that are not being hovered and set their opacity to point to so to see this in the dev tools here if I select one of them and then trigger the hover class now if I hover over any one of these other ones you can see that it's applying that opacity too because they are not hovered inside of the parent container this next example will allow us to select elements that have a given number of children we're calling these quantity queries so here we're saying if a card has at least four children select that element and set the background to red and so here we we're using the child combin combinator in combination with the inth child selector and you can see here that if an element has more than four children in this case

Chapter 10 8:26

Quantity Queries

they're just little divs but if it has at least four then we are applying that red background and four or more so if it has five children we're applying that red background as as well this next example is for selecting elements that have empty children now this might be useful if you want to implement something like skeleton loaders or maybe you just want to use it for debugging to kind of like highlight any elements on the page that are completely empty so in this case we are selecting any post that has any empty children and again we're using that child combinator but this time we're using the empty pseudo class and so here we can see that this post is selected because it has an empty H2 and in this case we're just giving it a dotted red bordercolor this next example is for styling nested dropdowns so if you ever have a list of let's say like nav items and one of those nav items has a nested list because it's going to be a nested dropdown this will allow you to style that specific element to add like a drop down arrow using CSS and so here we are saying select any list item

Chapter 11 8:59

Empty Children

Chapter 12 9:29

Nested Dropdowns

inside of a nav that has a list inside of it so here we can see we've got our main list but then this one actually has a list inside of it and from there we want to select an anchor tag that has a list as the adjacent element and then we're going to style the using the after pseudo selector so because this anchor tag has a list as the adjacent element we're going to style it and then in this case put the drop- down arrow now the reason we need this qualifier here is I don't want that drop- down arrow to appear on all of the children so we're just applying it to the parent this last example will allow you to select elements based on specific attributes and this is a real world example that comes from the syntax branding webpage so we have a bunch of different logos of various colors but if a logo has black text inside of it and both of these logos have transparent backgrounds If This Were to have a black background we wouldn't be able to see that image at all so what we want to do is select any asset element which is the parent and if it has an image inside of it whose SRC

Chapter 13 10:21

Attribute Matching

attribute has the word black inside of it in this case it's just a full-on URL with the with the word black and if that's the case change the background to White and the color to Black now those are all the examples I have for you uh but like I I said everything I showed you will be available on the web so check the link in the description you can go there you can play around with these examples you can copy them to use them in your own apps now if you found these useful let us know down in the comments also if you have some more interesting use cases for has I'd love to hear about it as well and uh that's all for now so I'll see you in the next [Music] one [Music]

Chapter 14 10:56

Where to find these examples

Chapter 15 11:07

Thanks!

Quote it with credit and a link. Quoting Syntax