In this lesson, you gave the family-friendly job on the work day notice you built in HTML its own color, tried two shortcuts that rank above every selector, watched them escalate, and then fixed the conflict with a better selector.
Key Points
- A rule that loses. Rosa wants both job descriptions in the darker green and the family-friendly mulch job in the garden’s green. The mulch paragraph has both classes,
class="job family-friendly", so both rules match it. Withp.joband then.family-friendly, both jobs are the darker green. The job rule’s selector, a type and a class, is (0, 1, 1), and.family-friendlyis (0, 1, 0), so the Styles pane crosses out the family-friendly color, although it comes later. - A
styleattribute on the paragraph beats any ordinary rule, however specific its selector. The Styles pane lists it at the top, underelement.style, and crosses out every color below it. But it lives in the HTML and styles one paragraph, so every family-friendly job needs its own copy. Only one kind of stylesheet declaration can beat it. - The important flag is an exclamation mark and the word
importantafter a value, as incolor: oklch(0.447 0.116 152.7) !important;. A declaration with it beats every declaration without one, whatever their selectors. It even beats astyleattribute without the flag, though astyleattribute with the flag wins. !importantescalates. Once.family-friendlyhas the flag, the job rule has lost, and it can only win back with a flag of its own, and in shared CSS, the next person adds one. With the flag on both colors, both inevents.css, specificity decides again, and the job rule still wins. You’re back where you started, one level up. Each important flag needs another to beat it, and none of them fixes the selector.- The cascade checks, in order: the last lesson’s three questions with a fourth. First, origin and importance: on this page, your
!importantdeclarations, then your other ones, then the browser’s defaults. Then thestyleattribute, the new step. Then specificity, and last, order. - Origin, from the last lesson: the browser’s own rule for links,
a:-webkit-any-link, is more specific than yourarule, yet its color is crossed out, because your ordinary rules outrank the browser’s. - The fix. The job rule won only because its selector named the paragraph as well as the class. Remove both flags and change
p.jobto.job. Now both selectors are (0, 1, 0), order breaks the tie, and the later.family-friendlyrule wins. Its text says children are welcome, so color isn’t the job’s only cue. - The rule: when a rule loses, find the winner in the Styles pane. Then lower the winner’s specificity (
p.jobto.job), or raise the loser’s (.family-friendlyto.job.family-friendly), only as far as you need. Keep!importantas a last resort, for a rule you can’t edit, such as one in someone else’s stylesheet. @layer: in other people’s CSS, you may meet@layerin front of a group of rules. It sorts rules into layers, and the cascade ranks layers before specificity too. That’s all you need about layers for now.
Final Code
At the end of events/events.css, after the .site-nav .featured rule from Find Out Which Rule Wins, add the two rules for the notice’s jobs, with no !important: