CSS Baby Steps: You Need to Crawl Before You Walk
Cascade - Just Like a Waterfall
Like everyone who begins to learn coding for front-end development, I started off by learning some basic HTML. And like - I would guess - most people, I thought it was quite straightforward and found it incredibly satisfying to see a page develop in front of my eyes while I was inputting tags and elements in my code editor.
But then came CSS - Cascading Style Sheet. It made sense to me that we need to create a separate style sheet in our editor that we then link to our HTML pages, to make sure that we keep the content (created through HTML) neatly separated from layout, looks and style (created through CSS). What I did not understand, however, was what the C stands for.
One of the definitions of cascade that Merriam Webster offers apart from “a steep usually small waterfall” is:
“ something arranged or occurring in a series or in a succession of stages so that each stage derives from or acts upon the product of the preceding”
Looking back at the mistakes I made while trying to implement CSS - mistakes I absolutely could not understand, and which seemed completely random to me - now make a lot more sense. Cascading means there is a certain succession, and the order in which we write rules onto our style sheets is important.
Just like the flow of a waterfall: we need to create our style sheets from top to bottom. Image by sgcdesignco on unsplash.
Once I understood one needs to follow a certain order, I also understood why some of the things I had input were suddenly overruled by others. Shay Howe’s Website Learn to Code HTML and CSS perfectly breaks this down:
p { background: orange; font-size: 24px; } p { background: green; }
“ Because the paragraph selector that sets the background color to green comes after the paragraph selector that sets the background color to orange, it will take precedence in the cascade. All of the paragraphs will appear with a green background.”
Thanks to Shay Howe, I also finally understood the different kinds of selectors we are working with in CSS - type selectors, class selectors and id selectors - and that these selectors have a different specificity, and consequently a different weight in the cascade.
Type selectors such as <p> (paragraph) or <div> (dividers) select elements according to their type. They are quite broad.
Class selectors are more specific, they allow us to target elements according to their class attributes. They can be used several times throughout a page. The example Howe uses here is
<div class="awesome">...</div> <p class="awesome">...</p>
What became clear to me through his examples is that I as the developer can come up with my own class attributes (as seen in .awesome). Before, I had always thought that there are a number of attributes one needs to choose from. But in fact, I have complete creative freedom over the choice of my attributes.
Id Selectors are even more specific than class selectors. They are unique and can only be used one single time.
<div id="shayhowe">...</div>
Just like the class attributes, I as the developer can come up with my own id attributes. And most importantly to note: there more specific a selector is, the more weight it carries, which means it receives precedence in the cascade.
Combining Selectors: Just like German Compound Words
One of the most relevant things I learned from Howe’s Learn to Code HTML and CSS is that you can combine selectors and thus be more specific and detailed. To understand the logic behind this, it helped me to draw the analogy to German compound words.
The German language is famous for producing extremely long words, because it can simply “glue” words together to express something very specific. In English, for example, those words would often be separated by “of” or “by” or “from.”
Confusing? Maybe. But looking only at the very last word of this compound, we know this is just a very specific kind of Barbier, a barber.
To give a simple example:
Blume = flower; Topf = pot; Erde = soil; Dünger = fertilizer
Blumentopf= flower pot. It is a specific pot used for flowers. Topf (or pot) is the main qualifier.
Blumentopferde = a specific kind of soil that is used for flowers in a pot. Erde (soil) is the main qualifier.
Blumentopferdendünger = a specific kind of fertilizer that is used for soil that is used for flowers in a pot. Dünger is the main qualifier.
From this example we can see that the word at the end (or at the very right) is always the main qualifier. The words preceding just give extra information, and add specificity.
With regard to selectors in CSS, it seems to work the same way. Again, an example taken from Howe: the goal is to make the background of only one specific paragraph yellow amidst an otherwise completely brown-coloured block of paragraphs(the <p>s inside the <div>):
<p>...</p> <p>...</p> <p class="mustard">...</p> </div>
.hotdog p { background: brown; } .hotdog p.mustard { background: yellow; }
The combination of several selectors .hotdog p.mustard allows us to target only one specific paragraph without affecting the others. Like the German compound words, we need to read from right to left. The selector at the very right is the key selector, all the others to the left are prequalifies. Like the Barbier and the Dünger are just a very specific kind of barber and fertilizer, the .mustard selector is a very specific kind of selector that can be found inside the p selector that can be found inside the .hotdog selector.
So far the theory. Now let’s hope this will also work when put into practice.