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

Getting to know the structural selectors

When it comes to creating smart and dynamic layouts one of the more powerful features that was first introduced in CSS2 was the :first-child selector that enabled us to do something with the first child in a group of elements. CSS3 takes that idea and pushes its further with more structural controls. In this lecture we will explore the CSS2 structural selectors and explore the group of nth selectors in CSS3.

properties: first-child, last-child, nth-child, nth-last-child, nth-of-type, nth-last-of-type, css3 selectors, css2 structural selectors.

Video Transcript

00:00We are nearing the end of this course and really
00:03The this lecture group on selectors and the next two lectures are going to be all about structural selectors now structural selectors enable us to basically
00:13Control things that have some sort of structural behavior such as asking for the first item or last item in a group of items
00:20So we're really talking about the structure of that HTML tree of the node
00:24So if an eye if an element has ten elements inside of it
00:27What if we wanted to highlight every second item or every third item in that node?
00:32So we're gonna learn a lot of those in this lecture covering one CSS to structural element and a lot of CSS 3 ones
00:39And in the next lecture, we're going to continue on working on those type of structural elements. So let's jump right into them
00:45Let's jump into structural elements
00:50We are nearing the end of this group of lectures on CSS 3
00:55Selectors now with the next group of topics which will be included in this lecture and the next one is all about structural
01:04Selectors so let's take it let's take a gander at it
01:08Let's right away start ahead and I'm just gonna go ahead and just put our headline here so you could easily find it
01:13And we're gonna start off talking about CSS 2 structural pseudo class selectors
01:18That was a big full mouth of a lot, but really there's only one of them and really in CSS 2
01:24That's where the door was opened to all these new structural
01:28Selectors that we're gonna be discovering in this lecture and the next one
01:32now what I want to do is I basically want to make sure that my first item here has basically a
01:37Dash in the beginning basically to bound our whole all of our elements really in many ways
01:42I really I really want to create a box around this so this this whole list in general
01:47But I don't want to use the the the regular flow
01:50I just want to hear the I want to have a
01:52Marker on the top and maybe a marker on the bottom
01:55So let's start with the top and I'm just gonna go ahead and say maybe I want our list item
02:00But not any list item. I want our first child and by sending it to be our first child
02:06I could literally just go ahead there and put whatever rule I want so in this case
02:10I want to do a border top, and I'm just gonna go ahead and set it to be two points
02:14I'm gonna make it dashed, and I'm gonna set a color to be
02:18Let's set it to be black
02:22Save it let's go ahead and click on refresh
02:24And now we should see that we have your a dash
02:28bounding our first selectors making it very easy for us if we want to do something specific to a specific item for example if
02:35You have the different
02:37Paragraphs and you want to have a different treatment to the first paragraph or the first of any type inside of a group
02:43Then there'll be a perfect answer your first child of a certain section
02:48So once you get into a section you could get its child now
02:52But by the way for example if you had a plethora of different items
02:56And you don't want to list it out as a list item because it's not a list item you might have just gone to your
03:01UL and say okay
03:02I just want the first item or first child inside there and if I've done that and click down refresh
03:07We would see that the first child would still be each this time around which would be each item because each item is
03:14the first child of that element
03:16Con on contrary if I set it to be the UL and I set it up and I refreshed it notice
03:22What would happen it wouldn't work at all because the first child here would be the whole document because our UL is
03:28Configured and sitting inside of the full document so this is not the first child, and it's somewhere in the body
03:35So it's really not getting us anywhere thus when we're setting us our first child of the Li
03:40We're literally saying the first child where our Li is one of the children
03:46Because notice when we don't have a space we're basically talking directly to that level
03:50So we're asking the Li's and that level of Li's for the first child
03:56So basically in
03:58Plain language what we're basically saying is okay
04:01There's an Li right here, and I want to get now the first child so really we're talking to all the Li's
04:07asking for the first child and by
04:10Coincidence we're really talking to Li as an Li as well
04:13So let's continue now that we're done with the CSS to structural codes
04:17It's time for us to move on to the structural codes of CSS 3 and there's plenty of them
04:21And we could let's start off with the most simple ones
04:24I'm just going to copy here our first child because really what we want to do is to do the exact opposite the last child
04:31So I'm going to go ahead and set it to be last child and this time around we're going to set it to the bottom
04:35And instead of making a dash how about we make it solid and if I go ahead and save this we'll see that now
04:41We have also a dash on the last item in our selectors as well, but that's nice
04:47This is all could have been done. Maybe with tables if we went into that extent
04:52Obviously some things we couldn't like if we wanted to make this in a different background color or maybe larger or smaller or so forth
04:59But let's take it a step further. What if you wanted to have a different background per child?
05:05So what if every second or every third child or every whatever you would want to have a different color or a different
05:11Configuration to it lucky for us again
05:13Incredibly easy all we have to do is set an li again and then ask for the nth
05:18Child which it basically means and and I'm gonna put here a value such as the second every second item or every third item
05:25And just by sending that I could go ahead and I'm just gonna copy a background color that I pre-configured earlier
05:30I could basically say hey every third item
05:33Every third item inside of this parent inside of this element mark its color to be the color that we're identifying
05:40Right here now notice if I click here on refresh
05:44If I click here on refresh if I save it, and then I click on refresh
05:49Oh, you know what is the color is so light that it's almost invisible so you can see that this item is
05:59Selected this item is selected if I go ahead and just comment out that background
06:04You'll you'll notice a slight change if you take if you notice there is a break so that break was undone
06:10So my mistake I used the wrong color for this one
06:13But let's continue anyways because I want to showcase the different capabilities
06:16So I'm gonna go ahead and continue with another color
06:19But this time I'm gonna add my 3n and say plus 1 which will basically be an odd one step further and in this case
06:25I'm gonna set a color as well
06:27But it's gonna be a much better color one that will probably notice and if I go ahead and click on refresh now
06:33We'll have here a stepping stone and now we have a stepping stone all that's left for us to do is also color code our
06:39Third item so I'm gonna go ahead and color code our third item as well
06:43Notice how I could put here the steps with the 3n 2n whatever amount of steps
06:47I want it to be to create that nth ruling or I could just go ahead and set an
06:52Absolute value such as 1 2 3 if that was something that I needed
06:57All right, I'm gonna go ahead and save that and just click on refresh once I change the color
07:01So I'm gonna set here another shade of blue and I'm gonna go ahead and save that click on refresh
07:07And now we could see that every single step has been blocked by our regular element with these beautiful elements
07:13now I want to talk about a few more things before we wrap up this lecture and
07:18Really what I want to talk about is I want to talk about we already seen it more or less
07:22But I want to take it one step further
07:24So we said we could take the nth child and we said with our end child
07:28I'm just going ahead and copy this we could go ahead and say hey
07:31I want the second child by putting directly the second child
07:35And I'm gonna go ahead here and set your color. That is very different
07:38I'm gonna set it to be I'm gonna set it to be white a
07:41Type of a shade of white really in many ways and if I click on refresh
07:45We'll see that in the shade of white we could see our item is selected our second item is selected
07:50now I want to take it one step further and
07:54and I want to talk about a few more things and and and
07:58Among them is really the the capability of us to define a type and when would you actually want to do that?
08:04So to be able to do that
08:06I'm gonna go to my UL here and I'm gonna add more children that are not of Li type just to showcase the problem
08:13So I'm gonna go ahead here and just said you're an h2 as the first thing here
08:16And I'm gonna put another one right here for our links of our site
08:20So we'll have your check our links and we'll have here also the descriptions of our lectures all sitting in one place
08:26But I want you to notice something our second item that was supposed to be
08:30White you notice what is going to be marked white now the first item
08:34The reason is is because it is the second item inside of our
08:38UL as you as you remember if you remember we talked about it
08:43We said that the Li the way it works an nth child is
08:46Not dependent on the Li the Li is just a reference and we're asking for the parent for the children
08:52So in many so basically when I'm asking for the second item
08:55I'm asking for the second item that is actually inside of that UL
09:00so I'm gonna go ahead and I'm gonna show you the way to define it in a way that would work and for that we're
09:04gonna use that of type nth of
09:07type and the nth of type will say
09:11from the type of Li the second item
09:14Enabling us to then go ahead and mark the second item and again any item any CSS rule that starts with nth
09:21all these and the
09:23Numeric systems that we worked with so far will work on them as well now
09:27I just want to show you one more item, and I'm gonna go ahead, and I'm gonna comma this and I want to show
09:32One more thing so we we've seen also the if I'll do the Li I could do oto and last
09:40child
09:41Say I wanted the second from last to be and if I go ahead and I click on refresh here
09:45This is all good still it is the second from last
09:48But if I went ahead and I said hey
09:50I wanted third from last right if I said it to be the third from last then again
09:54I'm gonna have a problem because it's now not gonna select my Li because there is no third item that is
10:00Selected so for me to be able to say hey
10:02I want it to be from there is no third item so basically this rule of the third from last just never gets applied
10:09For me to be able to apply what I'm gonna have to do is ask for the nth last of type nth last of
10:17type and
10:19By setting the nth last last of type I could then go ahead and say okay ignore anything that is not of that
10:25And then we're gonna highlight those elements
10:28So that's it really and I'm just gonna go ahead here, and I'm gonna paste inside all the items
10:32Just walk through them really quickly so how about we put it right in the beginning here
10:36Before then two and three selectors. I'm just gonna paste it right here, so we met the first child
10:41It was literally the first child
10:43We met the last child right after that and the last child enable us to select the last item in a group without
10:50relevance to the actual
10:52What item it is, but if the last item was not that item then just nothing would happen because that rule would not be applied
10:58We've then seen the nth child and the nth last child and again with the same type of structural logic
11:05That was behind it that defined us based on whatever rule we put inside of our element
11:10last but not least
11:12well not last but at least we saw the type elements enabling us to choose from the nth type of
11:18Making sure that we're only selecting the items based on their type
11:22ignoring the numeric system of things that are not of that type and thus we've seen it in the
11:28For the the regular flow or the last basically going backwards instead of going forwards
11:33So they're basically the two of these are comparable to the two of these
11:37So that's it for these core structural pseudo classes
11:41But we're not done with pseudo classes
11:43We're gonna continue looking at a few more pseudo classes that are only available in CS3 in the next lecture
11:48So I hope to see you then

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!