topskill.dev
CoursesTracks
0
All courses/CSSSpecificity Resolve a Style Conflict
CSS Foundations
7 / 10
← CSS
○CSS Rule Style an Exhibit Card○Write Your First CSS Rule○CSS Selectors Target Exhibit Details○Target Exhibit Details○Apply CSS Three Sources○Apply CSS Three Ways●Specificity Resolve a Style Conflict○Resolve Specificity○Inheritance and the cascade✦Module Quiz
○Color Palette○Color Palette○Background Layer○Background Layer○Type Rhythm○Type Rhythm○Borders and Shadows○Borders and Shadows✦Module Quiz
○Box Model Seed Packet Card○Space a Seed Packet○Display Swap Table Labels○Arrange Swap Table Labels○Positioning Pickup Pin○Pin the Pickup Location○Sizing and Overflow○Size a Seed-Swap Notice○Sticky scroll headings✦Module Quiz
○Advanced Selectors Book Cart Sign○Target a Book Cart Sign○Pseudo-Classes Night Cart States○Style Night Cart States○Pseudo-Elements○Add a Book-Quote Mark✦Module Quiz
○Lantern Market navigation○Lantern Market navigation✦Module Quiz
○Community garden request card○Community garden request card✦Module Quiz
○Flex Direction○Flex Direction○Main-Axis Spacing○Main-Axis Spacing○Cross-Axis Alignment○Cross-Axis Alignment○Wrapping and Flex Items○Wrapping and Flex Items○Flexbox Navigation○Flexbox Navigation✦Module Quiz
○Grid Tracks○Grid Tracks○Grid Gaps○Grid Gaps○Grid Placement○Grid Placement○Responsive Grid○Responsive Grid✦Module Quiz
○River radio schedule○Harbor radio schedule✦Module Quiz
○Field note color system○Field note color system○Transforms○Rotate a Field Note○Resilient styles✦Module Quiz
○Kite festival signal○Garden festival signal✦Module Quiz
○Garden forecast○Harbor forecast○Fluid sizing○Fluid weather bulletin○Images that fit✦Module Quiz
○Seed inventory table○Tool checkout table✦Module Quiz
○Community maker profile○Maker profile sheet✦Module Quiz

Specificity Resolve a Style Conflict

When matching rules disagree

The museum note is a paragraph with both class="highlight" and id="intro". Three selectors match it, but its text can only have one final color. The cascade is the browser's process for choosing that value.

css
p { color: gray; }
.highlight { color: orange; }
#intro { color: green; }

These are ordinary rules in the same stylesheet, with no special priority settings. Here specificity, the weight of a selector, decides: an ID selector outranks a class selector, and a class selector outranks an element selector. The note is green because #intro wins. This does not mean the last line always wins.

If two otherwise equal rules use the same selector, the later declaration wins. For example, p { color: gray; } followed by p { color: blue; } gives paragraphs blue text unless a higher-priority rule overrides it. CSS has other priority mechanisms, but this exercise does not need them.

Test the conflict

Run, then change .highlight to blue and Run again. The note stays green. Move the entire #intro rule above the others: it still wins. Restore the original order and orange class color. In the exercise, add rules one at a time to watch the winner change.

Hint 1
1 / 1
HINT 1

This completed example is for reading and previewing before the exercise.

Files
  • ⋄index.html
  • ⋄style.css
Loading editor…