Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Rank Inline Styles and !important

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. With p.job and 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-friendly is (0, 1, 0), so the Styles pane crosses out the family-friendly color, although it comes later.
  • A style attribute on the paragraph beats any ordinary rule, however specific its selector. The Styles pane lists it at the top, under element.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 important after a value, as in color: oklch(0.447 0.116 152.7) !important;. A declaration with it beats every declaration without one, whatever their selectors. It even beats a style attribute without the flag, though a style attribute with the flag wins.
  • !important escalates. Once .family-friendly has 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 in events.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 !important declarations, then your other ones, then the browser’s defaults. Then the style attribute, 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 your a rule, 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.job to .job. Now both selectors are (0, 1, 0), order breaks the tie, and the later .family-friendly rule 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.job to .job), or raise the loser’s (.family-friendly to .job.family-friendly), only as far as you need. Keep !important as 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 @layer in 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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In Find Out Which Rule Wins, you compared selectors by specificity, with order as the tiebreaker. Now you’ll meet what ranks above it, and why that’s rarely the fix.

00:11 Open the work day notice. Rosa wants both job descriptions in the darker green, and the family-friendly mulch job in the garden’s green, so it stands out.

00:20 That paragraph has both classes, job and family-friendly. At the end of events.css, add a rule for paragraphs with the job class, then one for the family-friendly class.

00:31 Save, and both jobs turn the darker green. Select the mulch job, and look at the Styles pane. The job rule’s selector, a type and a class, is (0, 1, 1). The family-friendly selector is (0, 1, 0).

00:47 So its color is crossed out, although it comes later.

00:52 One quick way out is a style attribute on the paragraph, with the garden’s green. Save, and the mulch job turns green. In the Styles pane, the attribute’s color sits at the top, under element.style, and every color below it is crossed out.

01:08 A style attribute beats any ordinary rule, however specific its selector. But it lives in the HTML and styles one paragraph, so every family-friendly job needs its own copy.

01:21 Only one kind of stylesheet declaration can beat it. Delete the attribute, and try that instead.

01:28 In events.css, put an exclamation mark and the word important after the family-friendly color. That’s the important flag. Save, and the mulch job is green again.

01:41 This time the job rule’s color is crossed out. A declaration with the important flag beats every declaration without one, whatever their selectors. It even beats a style attribute without the flag, though a flagged one wins.

01:56 The job rule has lost. Only a flag of its own wins it back, and in shared CSS, the next person adds one. Put the important flag on the job rule’s color too.

02:08 Both colors have the flag now, so which green does the mulch job get? Your turn: pause the video and predict its color. The darker green. Between these two important declarations, both in events.css, specificity decides again, and the job rule still wins.

02:28 You’re back where you started, one level up. Each important flag needs another to beat it, and none of them fixes the selector.

02:37 So the last lesson’s three questions get a fourth. First, origin and importance: here, your flagged declarations, then your other ones, then the browser’s defaults.

02:48 Then the new step, the style attribute. Then specificity. And last, order.

02:56 You met origin in the last lesson, on any link. Here’s the link to the garden’s home page. The browser’s own rule for links is more specific than yours, yet its color is crossed out, because your ordinary rules outrank the browser’s.

03:12 Remove both important flags. The job rule wins only because its selector names the paragraph as well as the class. Drop the p, and keep the class. Now both selectors are (0, 1, 0), so order breaks the tie, and the later family-friendly rule wins.

03:30 Save, and the mulch job is green, with no flag anywhere. Its text says children are welcome, so color isn’t the only cue.

03:40 When a rule loses, find the winner in the Styles pane. Then lower the winner’s specificity, or raise the loser’s, only as far as you need. Keep the important flag as a last resort, for a rule you can’t edit, such as one in someone else’s stylesheet.

03:57 In other people’s CSS, you may meet @layer in 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.

04:10 Next is the exercise Settle the Events Page Conflicts, where you’ll get the events page’s rules to agree on the look Rosa asked for.

Become a Member to join the conversation.