02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Comparing between only-child and only-of-type

In the last lecture we explored the CSS2 structural selectors and explored the group of nth selectors in CSS3. In this last lecture of the chapter we will look at a few more structural selectors that we didn't cover yet as we compare between the differences between only-child and only-of-type.

properties: only-child, only-of-type, css3 structural selectors, root, empty, last-of-type, first-of-type

Video Transcript

00:00That's it. This course, this lecture, this chapter, this essence of CSS3 Selectors is over.
00:07There's a few more properties we're going to cover in this lecture following up and finishing our conversation about structural CSS
00:14Selectors. We're going to meet up the basically everything that's left in CSS Selectors in this lecture.
00:19So how about we jump right into it. Thank you so much for taking this course,
00:23taking this chapter. Once you're done with it, make sure to follow up and continue in our further chapters, our further
00:29episodes of CSS3, so you become a really, really super CSS3 developer.
00:37When we're continuing working on CSS Selectors, really focused on
00:43structural CSS Selectors, and I'm going to go ahead and I'm not going to go and do each and every one of them.
00:48We've seen quite a lot of them already.
00:50I'm going to go through just talking briefly about some of the ones that we didn't cover yet, and they're very intuitive,
00:57so we won't really need to.
00:58So I'm going to go ahead and just paste a few of those as we speak about them.
01:02The first of type and the last of type is very similar to the first child and last child, only
01:08it's of a specific type.
01:10So if I want to get the first of that specific type, then I could say that or the last of that type,
01:15which by the way, it correlates to that nth of type putting one here or nth last of type
01:24with the value one. So those are basically the same.
01:28All right, so let's continue onwards.
01:29The next one that I want us to look at is the only child, and I'm not going to actually do it because
01:34just to save us some time, the only child is basically is the same thing as if we would set first child,
01:41last child, or in other words, if an element has only one child.
01:46Now we're going to see it in action in a second because really this one is the more different than maybe the more complex one,
01:51but I don't want to just walk through all the different elements first.
01:54And I want to talk about another one that is very similar that is called only of type.
02:00The only of type works very similarly.
02:02Only this one is first of type, last of type.
02:06So it's a little bit different.
02:07And we're going to look at the differences in a second once we go to the live presentation.
02:11Last but not least, there's two more structural elements that we haven't touched at all because we don't really have a usage for them in our day to day uses
02:20where the empty one would return to us any HTML element of this specific type that we're asking for or of any other type that doesn't have any content, including any text.
02:31This can be very useful in JavaScript when you want to get rid of paragraphs that no content is within them.
02:37Their root tab.
02:38So we're not going to see it in action, but it will return to you an empty element of whatever element type that you choose to bring back.
02:45The root will return us always our root itself of our document in HTML.
02:49That would be our HTML document itself.
02:52Not something that we really have a use for in our case.
02:55But I encourage you, if you do know how to use it or if you're up to a challenge, try to search up and find out a useful scenario where you would use the empty or root selectors and make sure to add that into our commenting section.
03:08So without without that with that overview done, I want to talk now more specifically live about the only child and only of type to do that.
03:16I'm going to go ahead and add one more attribute into our list of attributes of our structural selector attributes, really.
03:24And I'm going to go ahead and I'm going to say I want to span tag and I want the only when it's the only child, which basically means is whenever whenever I have a span tag that has no brethren's.
03:35So what we want to do in that scenario, I'm going to go ahead and set a background color and I'm going to set that to be yellow and I'm going to go ahead and set the color of the font to be red.
03:44Now, notice that we do have two rules here that would it would work for to one of them is going to be our item right here, which originally had we had marked and the second one will be inside of our HTML inside of our footer.
03:56So let's take a deeper look into that in our in our section in our article.
04:01If we find it up above, we'll see that the span is inside of a B tag and really it's true.
04:06It's the only child of that B tag.
04:09Same is true for our elements inside of our footer where we have our span right here, which is the only child of the span that is outside of it.
04:19Now, let's take a look at the difference between that only child and only of type.
04:24If I change my from only child to only of type, it's going to do it a little bit differently.
04:30And I want you to pay attention here.
04:32So if I go ahead and click on refresh, we'll see that first of all, for our item in the top, it's the exact same thing.
04:38But for the item below, we're marking the whole span and you might ask yourself, why is that?
04:43And the answer is very simple.
04:45Although this rule still applies, this is this is getting selected and it would be highlighted.
04:50This item is getting selected as well.
04:53The reason is is because it's the only span inside of this div and because there's only one span inside of this div, then the span is selected.
05:02And that is the reason why this whole span is selected.
05:04So the difference between the two of the only of type ignores if there's other things counting that one if that's there's one of that type inside of that specific parent.
05:15Contrary to that, the only child really reflects if there's only one item explicitly inside of the HTML inside of that bounding parent.
05:25So that's it.
05:26We're done with our CSS structural selectors and with it, we're done with a whole conversation of CSS selectors and we're done with this whole conversation.
05:36So I hope you enjoyed this group of lectures on CSS three selectors.
05:41And I hope to see your comments below on actionable items that you're doing with these new CSS selector rules that you're working with.
05:48So we'll see you in our next chapter or next course.
05:52So thanks for watching.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!