Paraphrasing 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.
In 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.
We 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.
In this lesson we have looked at two means of identifying child elemnts within a container element:
The most intuitive was using the :first-child and :last-child pseudo classes, whose very names indicate their nature
the :nth-child() pseudo class. In our case we used two parameters for the clas: 2n for indintifying the even position child elements and 2n + 1 for identifying the odd positioned child elements.
Copy the code in Listing 1 into an HTML editor, then link it to a CSS file that will style even positioned poems with a yellow background and the odd positioned ones with an orange background. Of course you may pick any other colurs you wish or use different styles or text or borders etc.
Copy the code in Listing 3 into an HTML editor. To give yourself space for the next part add at least another 12 paragraphs inside the article block. Next find more information from the :nth-child() pseudo class link. Using information from this link create a CSS file that will apply styling to every third, every fourth or fifth paragraph.
In this assignment, you are invited to demonstrate your comprehension of CSS child elements, particularly focusing on the use of pseudo-classes such as :first-child, :last-child, and :nth-child. The goal is to provide a floating narrative analysis of the key concepts presented, ensuring that your explanation connects the theoretical insights with the practical examples detailed in the lesson.
To structure your submission, craft a flowing narrative that addresses the following:
Begin by introducing the concept of child elements in HTML and CSS. Use the analogy of container elements housing nested elements, as presented in the lesson. Discuss the hierarchical relationships, using examples such as the <html> element containing <head> and <body>, and extend to deeper levels like <main> containing <article>, <section>, and so on.
Proceed to explain the :first-child and :last-child pseudo-classes. Provide a detailed narrative on their functionality, using the example of formatting the first and last poems in a list. Reflect on how their usage targets specific elements solely based on their positional order within the parent.
Shift your focus to the :nth-child pseudo-class. Narratively explore its versatility, emphasizing how it can target elements based on patterns such as odd or even positions, or custom formulas like every third or fifth child. Discuss the practical applications of this pseudo-class in styling lists or alternating rows for better readability.
Conclude your narrative by summarizing the importance of understanding child elements in web design. Highlight how these pseudo-classes provide precise control over styling, enabling designers to create visually organized and accessible layouts.
Throughout your submission, ensure that your explanations are cohesive, connecting theory with practical examples. Feel free to use additional examples to enrich your narrative, but focus on demonstrating a clear understanding of the material provided.