Core Concepts of the Cascade
This excerpt offers a thorough overview of the CSS cascade and its importance in resolving conflicts between rules. Here's a breakdown of the key concepts covered:
h1 {
font-family: serif;
}
#page-title {
font-family: sans-serif;
}
.title {
font-family: monospace;
}
In this scenario, the ID selector #page-title wins due to its higher specificity.
Criteria for Resolving Conflicts
When conflicts arise, the browser uses the following order of criteria:
User-Agent Styles: Default browser styles (e.g., <h1> has a large font size).
Author Styles: CSS styles defined by the developer.
User Styles: Custom user-defined styles, less common but higher precedence than user-agent styles.
2. Inline Styles: Inline styles added directly to an element via the style attribute have higher precedence.
3. Layer Priority: Newer CSS feature allowing styles to be layered with different priorities.
4. Selector Specificity: A hierarchy determines which selector takes precedence:Inline styles (style="...") > ID selectors (#id) > Class selectors (.class) > Tag selectors (h1).
5. Scope Proximity: Styles scoped closer to an element (e.g., via shadow DOM) override global styles.
6. Source Order: When selectors have equal specificity, the last declaration in the source code wins.
User-Agent Styles and Overriding Them
By default, browsers apply user-agent styles for basic page rendering. These can be overridden by author styles:
h1 {
color: #2f4f4f; /* Override user-agent default color */
margin-bottom: 10px;
}
#main-nav {
list-style: none;
padding-left: 0; /* Remove default padding */
}
Special Considerations
Order of precedence for !important: