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.
"Specificity is the algorithm used by browsers to determine the CSS declaration that is the
most
relevant to an element, which in turn, determines the property value to apply to the element.
The
specificity algorithm calculates the weight of a CSS selector to determine which rule from
competing CSS declarations gets applied to an element" - MDN
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 get used to the Elements panel in the browser's DevTools. 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.
There are many special cases in CSS specificity. Here are two to consider:
- The universal selector
*
has no specificity. - properties with
!important
are applied even if the selector is less specific.
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 Devotional 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). - The last two declarations made were
color: #fff
and before that,color: var(--primaryTextColor)
. - Why is the color #fff applied? Note the specificity values for each of those selectors.
var(--primaryTextColor) is a CSS variable that is defined in the :root selector of the page. This is a common practice to define variables for use throughout the page. This allows for easy changes to the variable value and, therefore, the value is applied throughout the page.You can toggle the 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
- Review this basic example of specificity in action: Specificity | w3schools.com
Submission
- There is nothing to submit for this learning activity.
Additional Resources
- Reference Only: ๐ CSS Stats - Enter a URL and see the CSS stats for the page including the average specificty.