|
2 | 2 | title: What is CSS selector specificity and how does it work?
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -The browser determines what styles to show on an element depending on the specificity of CSS rules. We assume that the browser has already determined the rules that match a particular element. Among the matching rules, the specificity, four comma-separate values, `a, b, c, d` are calculated for each rule based on the following: |
| 5 | +When multiple CSS rules could apply to the same HTML element, the browser needs a way to decide which rule takes precedence. This is determined by the **CSS cascade**, which considers importance, inline styles, selector specificity, and source order. **Selector specificity** is a key part of this process, calculating a weight for each selector. |
6 | 6 |
|
7 |
| -1. `a` is whether inline styles are being used. If the property declaration is an inline style on the element, `a` is 1, else 0. |
8 |
| -1. `b` is the number of ID selectors. |
9 |
| -1. `c` is the number of classes, attributes and pseudo-classes selectors. |
10 |
| -1. `d` is the number of tags and pseudo-elements selectors. |
| 7 | +The browser determines what styles to show on an element depending on the **specificity** of the CSS rules that match it. Specificity is calculated for each rule to decide which one takes precedence. |
11 | 8 |
|
12 |
| -The resulting specificity is not a single numerical score, but an array of values that can be compared column by column. When comparing selectors to determine which has the highest specificity, look from left to right, and compare the highest value in each column. So a value in column `b` will override values in columns `c` and `d`, no matter what they might be. As such, specificity of `0, 1, 0, 0` would be greater than one of `0, 0, 10, 10`. |
| 9 | +## How is specificity computed? |
13 | 10 |
|
14 |
| -In the cases of equal specificity: the latest rule is the one that counts. If you have written the same rule into your stylesheet (regardless of internal or external) twice, then the lower rule in your stylesheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied. |
| 11 | +The specificity algorithm is basically a three-column value of three categories or weights - ID, CLASS, and TYPE - corresponding to the three types of selectors. The value represents the count of selector components in each weight category and is written as `ID - CLASS - TYPE`. The three columns are created by counting the number of selector components for each selector weight category in the selectors that match the element. |
15 | 12 |
|
16 |
| -It's a better practice to write CSS rules with low specificity so that they can be easily overridden if necessary. When writing CSS UI component library code, it is important that they have low specificities so that users of the library can override them without using too complicated CSS rules just for the sake of increasing specificity or resorting to `!important`. |
| 13 | +1. **ID**: This is the count of ID selectors (e.g., `#example`). |
| 14 | +2. **CLASS**: This is the count of class selectors (e.g., `.my-class`), attribute selectors (e.g., `[type="radio"]`), and pseudo-classes (e.g., `:hover`). |
| 15 | +3. **TYPE**: This is the count of type selectors (element names, e.g., `h1`, `div`) and pseudo-elements (e.g., `::before`). |
| 16 | + |
| 17 | +When comparing selectors to determine which has the highest specificity, look from left to right (ID, then CLASS, then TYPE), and compare the highest value in each column. A value in the ID column will override values in the CLASS and TYPE columns, no matter how large they are. Similarly, a value in the CLASS column overrides any value in the TYPE column. For example, a specificity of `1,0,0` (one ID) is greater than `0,10,10` (ten classes and ten types). |
| 18 | + |
| 19 | +It's important to remember that specificity is part of the broader **CSS cascade**. Declarations marked `!important` have the highest precedence, followed by inline styles (using the `style` attribute). Selector specificity comes next. |
| 20 | + |
| 21 | +In cases of **equal specificity** among competing rules (that aren't inline or `!important`), the rule that appears **last** in the CSS source order is the one that counts and will be applied. |
| 22 | + |
| 23 | +It's a better practice to write CSS rules with low specificity so that they can be easily overridden if necessary. When writing CSS for UI component libraries, it is important that styles have low specificities so that users of the library can customize them without needing overly complex selectors or resorting to `!important`. |
| 24 | + |
| 25 | +## References |
| 26 | + |
| 27 | +- [Specificity | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_cascade/Specificity) |
| 28 | +- [Specificity | web.dev](https://web.dev/learn/css/specificity) |
0 commit comments