Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain the "Cascade" to CSS newbies?

I have trouble explaining to people who are new to CSS how the cascade works when using stylesheets. For whatever reason, newbies seem to naturally start out by just adding a class to every element. Take the code snippet from this question, for instance (no offense to the OP, overtherainbow).

Doing this:      ul#nav { } and ul#nav li { }
Is better than: ul.nav { } and li.navLinks { }

That is a very basic example, but you get the point. Using inheritance in this case is clearly beneficial.

I have tried to use semantics as a reference point, but that has not proven to be effective. It is quite possible that the term "class" is intruiging/confusing due to prior knowledge of OOP. Eventually, after seeing good example after good example, they always have the breakthrough moment where they finally "get it". But, I am looking for a way to streamline this process, because I don't want to be the guy who has to go back and maintain this classitis by-product that slips into production.

I particularly enjoyed this answer that explains recursion. I had a similiar explanation when I was in school, and I "got it" immediately. I am hoping that somebody brilliant out there has a similiar way of explaining the cascade succintly but thoroughly.

like image 839
Josh Stodola Avatar asked Aug 15 '10 20:08

Josh Stodola


People also ask

What is cascade in CSS explain with example?

The cascade is an algorithm that defines how user agents combine property values originating from different sources. The cascade defines the origin and layer that takes precedence when declarations in more than one origin or cascade layer set a value for a property on an element.

What is the meaning of the C cascading in CSS?

What does C in CSS stand for? CASCADING. So cascading means, if we list a style earlier in the document and we say it's one way, and then later in the document, we can say it's a different way, the thing that's later in the document wins. Styles of higher precedence will override the rules of lower precedence.

What are Cascading Style Sheets short answer?

Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS describes how elements should be rendered on screen, on paper, in speech, or on other media.

Why is CSS called cascading and why is this feature important?

The name cascading comes from the specified priority scheme to determine which style rule applies if more than one rule matches a particular element. This cascading priority scheme is predictable. The CSS specifications are maintained by the World Wide Web Consortium (W3C).


3 Answers

Maybe you could start by ignoring id and class in the beginning. Just doing "stupid" things like addressing elements very specific according to the hierarchy. Then explaining grouping and labeling, and combine them?

like image 56
nicomen Avatar answered Oct 07 '22 09:10

nicomen


It may help to describe and view the CSS Cascade as a hierarchy orthogonal to the DOM hierarchy. The two hierarchies meet at a display element. DOM gives the structural nesting, CSS gives the display attribute nesting. CSS is just the language used to describe a display attribute hierarchy.

The real difficulty in understanding the CSS Cascade is that it contains enough Rube Goldberg features to ruin any single unifying principle capable of "turning on the lights" for your students. There are always going to be a few WTF's between darkness and understanding.

The best description and explanation of the CSS cascade I can think of comes from www.w3.org Cascade. I simply recommend reading this over and over and over until the dimmer switch is pushed high enough to at least "see the light".

Start with a plain HTML document, add some simple CSS, figure out what happened and why, add something, figure it out, add something, figure it out... I can see the light.

like image 41
NealB Avatar answered Oct 07 '22 10:10

NealB


for me .. its easy to understand a css when you read it from right to left like:

div#nav li:hover a

i'd translate it to:

FOR ALL "links" INSIDE A "li" THAT HAS hover THAT ARE INSIDE A div WITH id=nav DO THIS{ ....

like image 33
pleasedontbelong Avatar answered Oct 07 '22 10:10

pleasedontbelong