Learning Outcomes
On completion of this lesson you will know how to identify child elements from their position within the parent element
Go to topOn completion of this lesson you will know how to identify child elements from their position within the parent element
Go to topParaphrasing the rhyme above we could say that great container elements have little container elements repeating themselves inside them while little container elements have lesser container elements but not to infinitum.
To put this in the context of HTML the <html> element has the lesser elements <head> and <body> inside it. The <body> element itself has the lesser still <header>, <main> and <footer> inside it.
In the next level down main itself can have any combination of <article>, <section>, <aside>, <figure> etc inside of it. Each of those elements can have repetition of other elements inside them. However if you go down more than three move levels, it is time to rethink the design of your page.
Go to topIn Listing 1 below the <body> contains one <article> which has six <section> inside it, each containing a poem or an extract from a play. At fist we shall look at those as formatted all with the same style and then we shall look at how we would give a special formatting to the first and last poem.
Fig 1 is a screenshot of Listing 1 with styling according to lines 1 to 47 from Listing 2. If you compare this with the contents of Listing 1 itself you can see that the poems are sequenced in the same order in both cases, with a left to right sequence and moving to the next line when they run out of space.
As there is nothing new in Lines 1 to 47 of Listing 2, we won't comment on them further and instead we shall look at lines 48 to 53, where we introduce our first-child and last-child pseudo classes.
In Listing 1 the article element spans lines 14 to 81. Inside this are six section elements. These section elements are referred to as child elements of the article element. The order of those child elements is determined by their appearance in the HTML file. Thus, referring to Listing 1 above, the first section element spans lines 15 to 30 while the second element spans lines 31 to 46. The last child element spans lines 73 to 80. Form this we deduce the first-child element spans lines 15 to 30 while the last child element spans lines 73 to 80.
Those are the two elements that are targeted by lines 48 to 53 of Listing 2. The pseudo classes first-child and last-child identify them solely on the basis of their positions in the list of section blocks.
The styling applied to those two child elements in Lines 48 to 53 of Listing 2 is quite simple and therefore there is no need to explain it.
Fig 2 above shows the list of poems with the first and last poem styled differently to the rest.
Before we leave this example it is worth stating that there is nothing special about the two poems that are styled differently to the rest, just that they were in the first and last positions respectively inside the article block. If we were to add another poem before the current first one and another after the last one, the two newly added poems would take on the identities of first-child and last-child For this reason the two newly added poems would have the special styling applied to them and the two who are currently styled like that would return to normal formatting.
Go to topWe have just looked at how to identify the first and last element in a group of elements within a container element. We are not, however, limited to identifying elements from the two extremeties of a group. Here we are looking at being able to identify elements within a group depending on whether they are in an odd or even position. This allows us to be able to colour odd and even positioned elements differently. This is useful for presenting a long lits since alternate lines being colured differently makes the list easier to read.
For our example here, however, we shall use a list of very short paragraphs where the odd positioned paragraphs are colured blue and the even ones coloured yellow.
Above is our HTML code. I don't think any explanation is required for it. It simply displays twelve paragraphs, each one announcing its position in the list and whether it is odd or even.
In Listing 4 above, if we were to use only lines 1 to 10 then our page would look like Fig 4 above - not very inspiring. Lines 11 to 16 will, however, take care of that.
At line 11 we meet our first pseudo class: :nth-child(n+1). If you are familiar with algebra then you will know that as long as n is an integer the expression 2n + 1 will always be an odd numbe. Thus lines 11 is targeting the odd numbered lines.
Line 12 tells us that the background colour of the target will be aqua, in othe words a bright blue.
At line 14 we meet the pseudo class :nth-child(2n). Again, as long as n is an integer 2n will always be an even number. Thus line 14 is targeting the even lines. Line 15 changes the background colour of those to #ffa, which is a bright yellow.
Fig 4 above shows the alternating blue and yellow lines.
Go to topIn this lesson we have looked at two means of identifying child elemnts within a container element:
Click on this link for more information on the :nth-child() pseudo class.
Go to top