thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

04.01 · Intermediate CSS / Practice · 20 min

Which CSS rule wins?

The same paragraph can match several rules. Find where its color comes from, watch a stronger selector take over and learn when later source order breaks a tie.

Activity 1 of 5 in Intermediate CSS

All courses

CSS Basics / 04.01

Which CSS rule wins?

Not started

Watch CSS rules compete in an interactive demo, then practice inheritance, selector specificity and source order in six guided steps.

More options
Rob traces p, .notice and #urgent rules to the same paragraph; the ID rule wins among these ordinary declarations.
Rob’s comic strip · Intermediate CSS

Watch one paragraph change, one rule at a time

Use Next to reveal one rule, or Play demo to move through the sequence with a five-second pause. The changed declaration is outlined, matching weaker declarations are crossed out, and the browser applies the real CSS to the paragraph.

The body rule still styles body even after the paragraph gets its own color. It is not crossed out as a losing selector on the paragraph: inherited color simply stops supplying the paragraph’s color.

Ordinary color declarations in one stylesheet and cascade layer. No inline styles or !important.

index.htmlhtml · example
<p class="notice" id="urgent">Bring a question.</p>
styles.csscss · example
body {
  color: navy;
}

YOUR PAGE PREVIEW

Inherited from body

Stage 1 of 5

Start with inherited color

The body is navy. This paragraph has no direct color declaration, so it inherits navy from the body.

Use Next to explore at your pace, or Play demo for a five-second pause between stages.

Your turn: find the winning declaration

Work on the same paragraph in six guided steps. Start with inherited navy, add element, class and ID rules, break a tie with a later rule and repair the declaration that actually controls the color. Predict the color before running each check.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

6 steps · Saved work · Instant feedback

A useful order for troubleshooting

Check that the selector matches the element and the stylesheet is linked. Ask whether the property is inherited or directly styled. For competing ordinary declarations in the same cascade layer, compare IDs, then classes/attributes/pseudo-classes, then element selectors. Only tied specificity is resolved by later source order.

In this example an ID beats a class; it does not mean an ID always wins every CSS conflict. Inline styles, !important, different origins and cascade layers add rules beyond this first lesson. We will handle those separately.

The browser’s developer tools show which declarations are overridden. Edit the declaration that wins instead of repeatedly adding weaker rules or reaching for !important.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

First ask whether the color is inherited or directly specified. For competing ordinary declarations in the same layer, compare specificity before using source order.