HTML and CSS · Chapter 7 of 12
The cascade
By the end of this chapter you can say which of two CSS rules wins and why (where the rule comes from, how exact its selector is, which comes last), see which values pass down from an element to the elements inside it, give colours names with custom properties and change them for one part of a page, and style a link for the moment someone points at it or reaches it with the keyboard. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: CSS: the look
- 4 tasks
- HTML and CSS
- In the all labs plan
The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.
Why you need this
Parda's home page now links seven stylesheets: Ira's parda.css first, then one a task. Every element on the page is reached by several rules at once. A paragraph in a venue card is touched by the browser's own rule for p, by parda.css's main p and .venue-card p, and by whatever you write next. When two of them set the same property, only one value can show.
Two things went wrong this week. Ira's launch colours, a cream and two maroons, are typed out as hex codes in four places, and she wants a night version for the late shows. And Datta wrote a rule for the sold-out notice that looks right and does nothing: the notice stays dark grey on beige. Chapter 6's rules cannot explain either. The browser has a precise way of choosing between rules, called the cascade, and this chapter is that way.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tokens.css: Ira's launch colours, as custom properties
In tokens.css, define three custom properties in a :root rule: --launch-paper with the value #fff3d6, --launch-ink with #3d0a15 and --launch-heading with #7a1c2e. Then style the launch cards, the elements with class launch, with those names through var(), never typing the hex codes again: each .launch gets background-color var(--launch-paper) and color var(--launch-ink), and the h3 inside a .launch gets color var(--launch-heading).
The Check reads the three properties on the page, and looks at the Opening weekend card: its background must be #fff3d6, its paragraphs #3d0a15 (they take the colour from the card, so they need no rule of their own) and its h3 #7a1c2e. tokens.css must read each of the three names with var().
- Task 2
links.css: the venue links, pointed at and tabbed to
The venue names under Where to buy tickets are links inside the elements with class venue-card. In links.css, give them two states. While the mouse points at a venue link (hover), its background-color is #fff3d6. While a venue link has the keyboard's focus after the Tab key (focus-visible), its background-color is #f0b94d and its color #3d0a15.
The Check points the mouse at Kala Talkies and Sahyadri Lawns, then presses Tab until it reaches Kala Talkies. The rest of the time the venue links have no background (transparent), and the footer's link help@parda.example never changes when pointed at.
- Task 3
theme.css: a night theme for the late shows
In theme.css, give the Late shows card (its class is card launch late) a night look by setting only Task 1's custom properties on .late: --launch-paper to #3d0a15, --launch-ink to #fbf6ee and --launch-heading to #f0b94d. Task 1's rules in tokens.css do the rest, so the card shows a #3d0a15 background, #fbf6ee paragraphs and a #f0b94d h3, while the Opening weekend card stays #fff3d6.
theme.css must set only custom properties, never background-color or color themselves. Task 1 must work first: a theme changes only what a rule reads with var().
- Task 4 · interview question
Interview: notice.css, the rule that does not apply
The notice Puneri Patya on Fri 4 Apr is sold out. Every other show of the launch week has seats. should be white, #ffffff, bold (font-weight 700), on maroon, #7a1c2e, as the rule in notice.css says. The Preview shows it dark grey on beige: two other things on the page beat the rule, one in index.html, on the notice's own tag, and one in datta.css. Make the notice #ffffff on #7a1c2e without !important anywhere (the Check looks in every file), and keep its text as it is.
Ira will ask: how does the browser decide which rule wins?
What you will learn
- Origin and order: the browser's own styles lose to yours; between two equal rules, the later one wins.
- Specificity: an id beats a class, a class beats a type selector, and how to count it.
- Inline styles and !important: what beats everything, and why to keep away from both.
- Inheritance: colour and fonts pass down to the elements inside; borders and padding do not.
- Custom properties: --name: value and var(--name), Ira's colour names, and setting them on one part of a page.
- Pseudo-classes: :hover, :focus-visible and :first-child, rules for a state or a place.
More in CSS: the look
Build The cascade yourself
Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.