CSS 4 🎨 How To Cascade, Specificity, and Inheritance
4. How-To Cascade, Specificity, and Inheritance
How to Check CSS Rule Priority Using Specificity
Steps:
- Count selectors by type (inline, ID, class/attr/pseudo-class, element/pseudo-element)
- Assign points: inline(1000), ID(100), class/attr/pseudo-class(10), element/pseudo-element(1)
- Compare total points to determine which rule wins
Example:
/* Specificity: 0,0,0,1 (1 point) */
p { color: blue; }
/* Specificity: 0,0,1,0 (10 points) */
.highlight { color: red; }
/* Specificity: 0,1,0,0 (100 points) */
#main { color: green; }
/* Specificity: 1,0,0,0 (1000 points) */
p { color: purple; } /* Inline style wins */
How to Create a CSS Cascade with Multiple Sources
Steps:
- Create browser default styles
- Add user styles (browser preferences)
- Add author styles (your CSS)
- Apply inline styles for highest priority
Example:
/* Browser default - paragraph margin */
p { color: black; }
/* User style (browser theme) */
/* Override with user preferences */
/* Author style - your CSS file */
.container p { font-size: 16px; }
/* Inline style - highest priority */
<p style="color: red;">This wins</p>
How to Use Inheritance to Style Child Elements
Steps:
- Set properties on parent element
- Verify child elements inherit those properties
- Override inheritance only when needed
Example:
/* Parent styles that are inherited */
.parent {
color: blue;
font-family: Arial, sans-serif;
}
/* Children inherit these properties */
.child {
/* Inherits color: blue and font-family: Arial */
font-size: 14px; /* Can override */
}
How to Resolve Conflicting CSS Rules
Steps:
- Identify which rules target the same element
- Calculate specificity for each rule
- Apply the rule with highest specificity
- Use !important as last resort
Example:
/* Rule 1 - Specificity: 0,0,0,3 */
h1.title { color: blue; }
/* Rule 2 - Specificity: 0,0,1,0 */
.title { color: red; }
/* Rule 3 - Specificity: 0,1,0,0 */
#header h1 { color: green; }
/* Result: #header h1 wins with highest specificity */
How to Apply Inheritance Selectively
Steps:
- Identify which properties are inheritable (color, font-family, etc.)
- Set parent styles for inherited properties
- Test child elements to confirm inheritance
- Use non-inheritable properties as needed
Example:
/* Parent with inherited properties */
.text-container {
color: #333;
font-family: Georgia, serif;
border: 1px solid black; /* Not inherited */
}
/* Children inherit color and font-family but not border */
.text-content {
/* Inherits color and font-family from .text-container */
/* Does NOT inherit border property */
}
How to Create a Specificity Hierarchy in Your CSS
Steps:
- Use inline styles for emergency overrides
- Implement ID selectors for unique elements
- Use class selectors for reusable components
- Apply element selectors as fallback
Example:
/* Inline style (highest priority) */
<p style="color: orange;">Inline</p>
/* ID selector (100 points) */
#hero h1 { color: purple; }
/* Class selector (10 points) */
.hero-title { color: blue; }
/* Element selector (1 point) */
h1 { color: green; }
/* Specificity order: inline > ID > class > element */
How to Debug CSS Cascade Issues
Steps:
- Use browser developer tools
- Check computed styles
- Identify which rules are being applied
- Verify specificity of active rules
Example:
/* Debugging example */
body {
background-color: white; /* Browser default */
}
.container {
background-color: #f0f0f0; /* Author rule */
}
.highlight {
background-color: yellow; /* Class rule */
}
/* In browser dev tools, check computed styles */
/* See which rule actually applies to element */
How to Use Inheritance to Reduce CSS Code
Steps:
- Set base font properties on body
- Apply to all text elements through inheritance
- Only override when necessary
Example:
/* Set base styles (inheritable) */
body {
color: #333;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
/* Children inherit these properties */
h1, h2, p, span {
/* No need to redeclare color, font-family */
margin-bottom: 1em;
}
How to Handle Browser Default Styles
Steps:
- Reset browser defaults with CSS reset
- Understand default styles for elements
- Override defaults with your own rules
Example:
/* Reset browser defaults */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Browser defaults are applied first */
p {
line-height: 1.5; /* Browser default */
}
/* Then your custom styles override */
p {
color: blue; /* Your rule overrides browser default */
}
How to Test Inheritance in Practice
Steps:
- Create parent element with styled properties
- Add child elements that should inherit
- Verify inheritance works correctly
- Check which properties are inherited vs not
Example:
/* Parent with styles */
.header {
color: #ff6b6b;
font-family: 'Roboto', sans-serif;
text-transform: uppercase;
}
/* Children inherit color and font-family */
.subheader {
/* Inherits color and font-family from .header */
/* Does NOT inherit text-transform in some cases */
font-size: 18px;
}