Key Takeaway: Prioritize loading resets and external resources before custom CSS resources for optimal performance.
In-Depth Explanation
The issue you're encountering is related to specificity in your reset CSS code.
When conflicting CSS rules are present, the browser will prioritize the rule with the most specific selector (or if specificity is equal, the last loaded rule).
The Mozilla Developer's Network describes "Specificity" as:
Specificity assigns weight to a CSS declaration based on the count of each selector type. In cases of equal specificity, the latest declaration found is applied. Specificity matters when targeting the same element; directly targeted CSS rules take precedence over inherited rules from ancestors.
Using Meyer's CSS Reset introduces conflicts with your own CSS due to properties like font: inherit
conflicting with your font-family
rule. This highlights the importance of the specificity principle—targeted elements override inherited styles from ancestors like body
.
To address this, ensure your CSS loads after the reset file. Alternatively, enhance the specificity of your selectors compared to the reset’s selectors. The third option, though not encouraged, involves using !important
to emphasize select styles over others but risks unintended side effects.
Illustrative Examples
Non-Functional Example: Demonstrates issues arising from sequential loading of CSS sections versus HTML sections in Stack Snippets.
body {
font-family: 'Lato', sans-serif;
}
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<p>Hello, world</p>
Operational Example: Reorder CSS Resources: By loading custom CSS post-reset inclusion, we resolve conflict issues successfully.
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<style type="text/css">
body {
font-family: 'Lato', sans-serif;
}
</style>
<p>Hello, world</p>
Operational Example: Use a More Targeted Selector: A viable workaround if modifying resource loading sequence isn’t feasible.
body * {
font-family: 'Lato', sans-serif;
}
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<p>Hello, world</p>
Operational Example: Emphasize with !important
: Caution advised while utilizing !important
, deemed against best practices.
body {
font-family: 'Lato', sans-serif !important;
}
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<p>Hello, world</p>