30/10/2024
CSS (Cascading Style Sheets) lets you style your web pages, but sometimes multiple styles can target the same element, leading to confusion about which style gets applied. This is where CSS specificity comes into play. It is a set of rules that the browser uses to determine which CSS rule is the most specific and should therefore take precedence when styling an element.
Specificity is essentially a scoring system. Each CSS selector is assigned a specificity value based on its type. This value is calculated in four categories:
Inline styles: These are styles defined directly on an HTML element using the style
attribute. Inline styles have the highest specificity value and are marked with a score of 1000.
Example:
<p style="color: red;">This is a red paragraph.</p>
IDs: Selectors that use an ID (using the #
symbol) come next in the specificity hierarchy, earning 100 for each ID used.
Example:
#header { color: blue; }
Classes, attributes, and pseudo-classes: Any class selector (using the .
symbol), attribute selectors, and pseudo-classes (like :hover
) all share the same specificity score of 10.
Example:
.button { color: green; }
Elements and pseudo-elements: Finally, the basic element selectors (like div
, p
, h1
, etc.) and pseudo-elements (like ::before
, ::after
) have the lowest specificity, earning 1 point each.
Example:
p { color: black; }
To calculate a selector's specificity, you represent its values as a four-part number (a-b-c-d), where:
For example, if you have the following selector:
#main .nav a:hover { color: purple; }
The specificity calculation would be:
#main
).nav
)a
)So, the specificity score is 0-1-1-1.
When multiple selectors apply to the same element, the one with the highest specificity wins. If two selectors have the same specificity, the latest one in the stylesheet (or in your stylesheets if you're using multiple files) will take precedence.
For instance:
p { color: black; /* Specificity: 0-0-0-1 */ } #header p { color: blue; /* Specificity: 0-1-0-1 */ }
In this example, <p>
elements within an element with the ID header
will be styled with blue text due to the higher specificity of the second rule.
Avoid Inline Styles: Although inline styles carry the highest specificity, they can make your CSS harder to maintain. Instead, use external stylesheets where possible.
Plan Your Selectors: Be deliberate in how you structure your CSS selectors. Using too many IDs or classes for specificity can lead to overly complex styles.
Use the Cascade Effectively: Remember that CSS stands for Cascading Style Sheets. This means that the order of your styles matters, so organize your CSS with specific styles towards the end of your files when necessary.
CSS specificity may seem complicated at first, but with practice, it becomes easier to grasp. It’s essential for avoiding conflicts and ensuring that your web pages look just as you intend!
30/10/2024 | CSS
30/10/2024 | CSS
30/10/2024 | CSS
30/10/2024 | CSS
30/10/2024 | CSS
30/10/2024 | CSS