CSS Specificity
Overview
CSS stands for Cascading Style Sheets. The "cascade" term represents the prioritization stack of the CSS declarations that are applied to a specific element. This order is determined by the CSS specificity value.
Prepare
- Read: ๐ Specificity - MDN
- Reference Only: ๐CSS Specificity Calculator - enter a selector and the specificity calculation is provided.
Calculating specificity during design can be difficult. It is recommended that you use the Elements panel in the browser's DevTools application. As you inspect an element by clicking on the element name in the panel or by selecting it from the displayed screen, the CSS rules and declarations are then shown for you to analyze and toggle on and off as needed to test.
Here are some specificity considerations:
- The universal selector
*
has no specificity. - properties with
!important
are applied even if the selector is less specific. (This should be avoided) - Inline styles have higher specificity than embedded and external stylesheets.
Activity Instructions
- Navigate in your browser to the byui.edu home page.
- Open up the browser DevTools.
- In the footer of the page, select the Devotionals link by either, 1) right mouse clicking on the link and selecting inspect, or 2) using the Select tool in the DevTools panel bar and then selecting the link.
- You should have the following anchor <a> tag selected in the
Elements
panel:
<a class="FooterNavigationItem-text-link" href="https://www.byui.edu/devotionals" target="_blank">Devotionals</a>
- In the DevTools Styles panel, note the CSS stack for this anchor element.
This panel reveals which CSS rules are applied and the declaration stack. Find the color
declaration property.
- How many
color
declarations are made for this element? You should find at least four (4).Besides the Styles tab, developers can used the Computed tab to shown the style application per property.
- Note that the last two declarations made in the cascade were:
color: #fff
andcolor: var(--primaryTextColor)
. - What is the "user agent stylesheet" notation shown in the CSS stack in the Styles tab?
- Why is the color
#fff
applied? Note the specificity values for each of those selectors.
color: #fff
by clicking on the checkbox to the left of the declaration in the Styles panel within DevTools.What are the actual CSS specificity values?
.GlobalFooter .FooterNavigationItem-text-link
is 020
.FooterNavigationItem-text-link
is 010
a
is 001
๐Resource: CSS Calculator - How many
Optional Resources
- Reference: ๐ CSS Stats - Enter a URL and see the CSS stats for the page including the average specificity.
- Specificity Try It | w3schools.com