Structural Pseudo-classes
Websites CSS Foundations Advanced Selectors Structural Pseudo-classes
Author Guil Hernandez
Total Time 10 minutes 3 seconds
In this video, we'll cover structural pseudo-classes that allow us to select elements based on their positions in the document tree.
Video Transcript
-
00:00
[Deep Dive]
-
00:03
[CSS Fundamentals: Structural Pseudo-classes with Guil Hernandez]
-
00:06
CSS added powerful pseudo-classes
-
00:08
that allow us to select elements based on their positions in a document tree.
-
00:12
In this video, we'll go over these advanced structural pseudo-classes. So, let's get started.
-
00:18
I've set up two HTML files for our examples
-
00:20
that link to a style.css file that I created just to get us started.
-
00:25
We'll be adding our new pseudo-classes and a separate file named pseudoclasses.css.
-
00:31
First, we need to understand how nth-child works.
-
00:35
Nth-child is the most useful and one of the most powerful structural pseudo-classes available.
-
00:41
It selects elements based on their positions within a parent element's list of child elements.
-
00:48
In our HTML, we have an unordered list which we'll use as our parent element.
-
00:55
Nested inside the unordered list are 10 list items.
-
01:01
I've added some styles to the list to help illustrate how nth-child works.
-
01:05
For this example, we'll be selecting different list item child elements
-
01:09
base on their position in the unordered list.
-
01:13
Nth-child uses a function line syntax that allows an argument to be passed in between parentheses.
-
01:20
Inside the parenthesis is where we declare how the element will be selected.
-
01:25
One of the arguments that can be accepted in between the parentheses
-
01:28
is a keyword of odd or even, which can be used to select every other child element.
-
01:35
So here we'll specify that we are selecting a list item,
-
01:40
and we will give it a color property and a background color.
-
01:55
When we refresh the page, notice how only the list items
-
01:58
with the even numbers have the styles applied.
-
02:04
If we change it from even to odd and refresh the page,
-
02:11
now only list items with odd numbers are selected.
-
02:16
Another argument that we can use in between our parentheses is an integer.
-
02:20
So for example, if we use the number three,
-
02:26
notice how only the third list item inside the parent ul is selected,
-
02:30
and if we replace it with a value of nine,
-
02:36
now the ninth list item is the one selected.
-
02:40
What makes arguments really powerful is when using expressions
-
02:43
to select the particular combination of child elements.
-
02:47
Expressions are a little more complicated, but as we'll find out shortly,
-
02:50
they are not so bad once you start experimenting with the numbers.
-
02:54
Our basic expressions syntax looks something like this: an + b.
-
03:00
The values a and b are always represented by integers, and the value n does not change.
-
03:08
Let's go over what these values mean.
-
03:11
Starting from the right, the b value is an offset value that determines the first element selected.
-
03:17
If we make this a value of three, this means that the third list item
-
03:22
would be the first one selected in our list--ignoring all sibling list items that precede it.
-
03:28
The a value determines the cycle of elements to select after the first one has been selected.
-
03:34
If we make value 2, this means that the third list item will be selected first,
-
03:41
then it will select every second list item after that.
-
03:46
Now the n value doesn't change in an expression.
-
03:49
We can think of n as a counter that starts at zero and it indicates the a value to the browser.
-
03:55
Let's see how this works.
-
03:58
When we refresh the browser, we'll see that the first list item selected is the third,
-
04:03
which was determined by our b value, then it selects every second list item
-
04:08
until there are no more list items to select.
-
04:12
If we change our expression to 3n + 4, now we see that the 4th list item is selected first
-
04:22
then every third one after that.
-
04:26
They are a few more things to remember when using expressions.
-
04:30
For example, if the a value is represented by the number 1,
-
04:35
such as in 1n + 3, you can actually omit the a value as it will be the same as using n+3.
-
04:44
This selects the third one first, then every list item after it.
-
04:49
If the b value is a zero like in 3n + 0, we can actually omit the b value
-
04:57
as it's the same as using 3n, which selects every third list item.
-
05:05
Finally if the a and b values are the same like in 3n + 3,
-
05:11
then we can go ahead and omit our b value
-
05:15
as it will also be the same as using 3n in our argument.
-
05:19
We can also use negative values in our expressions.
-
05:23
What if we wanted to select the first five list items only in our unordered list?
-
05:30
In our css, we can use the expression -n + 5.
-
05:36
This will select the 5th list item first, then all others above it.
-
05:42
When we refresh the browser, notice how just the top five list items are the only ones selected.
-
05:49
If we were to change it to -n + 3, now the first three list items are the one selected.
-
05:58
Another type of structural pseudo-class is nth last child.
-
06:02
Nth last child works much like nth-child except that it starts counting from the last child.
-
06:09
In our expression, if we use 3n + 1,
-
06:16
when we refresh we can see how it matches the last list item in the selection first,
-
06:20
then every third one going up the list.
-
06:24
We can get even more specific and apply styles only to particular types of elements.
-
06:30
For example, the nth-of-type pseudo-class selects an element base on its position
-
06:35
within a parent elements list of child elements of the same type.
-
06:40
Now, this is a more flexible and useful pseudo selector.
-
06:43
If you want to be sure you're selecting the same type of element no matter where it is
-
06:47
inside the parent element or what other elements appear before it.
-
06:52
For this example, I've set up another HTML page containing sibling heading, divs,
-
06:59
and a paragraph element that are all children of the body element.
-
07:06
I also added a few styles to make the divs appear as boxes.
-
07:11
Now if we only want to select and style these div boxes on the page in our css,
-
07:18
we will need to specify the div element first followed by nth-of-type and then the argument
-
07:26
For our argument, let's go ahead and select the second div,
-
07:30
and we will apply a background color of blue.
-
07:36
We'll refresh the page and see the styles applied only to the second div.
-
07:42
If we wanted to style every other div, then we can change the argument from two to odd.
-
07:51
Now you see that the odd number divs are the ones selected.
-
07:56
In our markup, even if we add more sibling elements before and after our divs here,
-
08:05
notice how the div elements are still the only ones selected, because with nth-of-type
-
08:10
we specifically instructed the browser to select the odd div child elements of the parent.
-
08:18
We can also use nth-last-of-type, which works much like nth-of-type
-
08:24
except that it start counting from the last child.
-
08:27
If we change odd to the number 1 for our argument,
-
08:32
when we refresh you see that it starts counting from the last child.
-
08:38
Now, if we go ahead and change it from 1 to 3,
-
08:43
notice that the third div from the last of this type is the now the one selected.
-
08:49
The last one we'll cover is useful for selecting an element
-
08:52
that is the only one of its type and its parent element.
-
08:56
In our markup, notice how there is only one heading one and one paragraph element.
-
09:02
They are the only ones of their type in the document.
-
09:07
In our css, we'll create an only of type selector
-
09:16
and here, we will specify a color of blue.
-
09:25
When we refreshed, we see how the h1 and the paragraph are selected,
-
09:28
because they are the only elements of their type within the parent body element.
-
09:33
Now, if we specify the paragraph element,
-
09:41
now it only selects the paragraph element that's the only one of its type
-
09:44
within the parent element.
-
09:46
In our markup, if we add a paragraph as a child element of one of our divs,
-
09:55
notice how the new paragraph also gets selected and styled
-
09:58
because it's the only element of its type within the parent div.
Stage Steps
-
Play
Preview
Substring Matching Attribute Selectors
5 minutes 42 seconds
-
ABC
Preview
Quiz: Substring Matching Attribute Selectors
5 questions
-
Play
Preview
Structural Pseudo-classes
10 minutes 3 seconds
-
Code Challenge
Preview
Code Challenge: Structural Pseudo-classes
4 objectives
-
Play
Preview
More Pseudo-classes
7 minutes 38 seconds
-
Code Challenge
Preview
Code Challenge: More Pseudo-classes
4 objectives
-
Play
Preview
Pseudo-elements
10 minutes 20 seconds
-
ABC
Preview
Quiz: Pseudo-elements
5 questions
Use pseudo-elements to generate quotes before and after the following text:
<p class="quote">Hello World!</p>