Code and data

Find which CSS declaration wins for a notice

·

A print studio has an active notice inside a panel and wants to know which rule sets its text color. Use the stylesheet example to compare specificity and importance on the same element.

CSS Specificity Visualizer

Compare the four matching color rules

Load the example and analyze the stylesheet. Assume a notice with the classes notice and active inside an element with the ID panel. Select the first four scores to compare color. The fifth rule sits inside a media query.

Read the three counts in order. :where(#panel) .notice scores 0, 1, 0 because :where() contributes zero. .notice.active scores 0, 2, 0. :is(.notice, #panel) scores 1, 0, 0 because the ID is the strongest argument, even when the notice matches the class argument.

The leading declaration sets teal on line 4. Its !important takes precedence over the normal purple declaration, which has the highest score among the first four selectors.

W3C, Selectors Level 4 specificityW3C, CSS Cascading and Inheritance

Remove importance and compare again

Remove !important from the teal declaration. Analyze the stylesheet and choose the same four selectors. The purple declaration on line 3 now leads.

Copy or download css-specificity.json. The selected indexes refer to rows in the export. Check that the comparison names :is(.notice, #panel) and shows important as false for its declaration.

W3C, Selectors Level 4 specificityW3C, CSS Cascading and Inheritance

Verify conditional rules on the element

The last selector, #panel .notice, scores 1, 1, 0. Its orange declaration applies only when the media query matches. Selecting it displays a scope notice so you can inspect the rule in browser developer tools.

With !important removed, resize a test page containing the notice. At 600 CSS pixels or below, the orange rule applies. Above 600 CSS pixels, the purple rule leads among these same-origin rules.

W3C, CSS Cascading and Inheritance

Common questions

Do selector branches share one score?

A plain comma-separated list has one score per branch. Functional pseudo-classes such as :is() have their own strongest-argument rule.

Why does the comparison ask me to choose matches?

The source alone does not identify the element or active state. Use the browser's matched rules to select the branches that apply.

Sources

Put it into practice.

Related guides