CSS Core Concepts
Table of Contents
CSS Core Concepts
The "C" in CSS stands for Cascade, and along with Specificity and Inheritance, these three core concepts define how CSS rules are applied to HTML elements and how conflicts are resolved. Understanding them is fundamental to predicting and controlling your web page's styling.
Cascade
The Cascade is the algorithm that determines which CSS declaration (a property: value; pair) applies to an element when multiple rules try to style the same property on the same element. It's a hierarchical process that combines rules from various sources and resolves conflicts based on a defined order:
Origin: The source of the CSS rule. Rules from more "powerful" origins take precedence.
- Transition Animations: Styles applied during CSS transitions.
!importantUser Agent Stylesheets: Styles set by the browser, marked!important.!importantAuthor Stylesheets: Your CSS rules marked with!important.!importantUser Stylesheets: Styles set by the user (e.g., accessibility), marked!important.- Author Stylesheets: Your regular CSS rules.
- User Stylesheets: Regular styles set by the user.
- Browser (User Agent) Stylesheets: Default styles provided by the browser (e.g., default
<h1>font size).
Note on
!important: While!importantcan override normal rules regardless of specificity, it breaks the natural flow of the cascade and should be used sparingly, primarily for debugging or overriding third-party styles as a last resort. An!importantrule from an author stylesheet generally beats a normal rule from any origin.- Specificity: If rules come from the same origin (e.g., all from your author stylesheet), their specificity is compared.
- Order of Appearance: If origin and specificity are equal, the rule that appears last in the stylesheet (or later in the parsed order) wins. This means if you define
p { color: red; }and laterp { color: blue; }, the text will be blue.
Specificity
Specificity is a calculated score that determines which CSS rule's declaration takes precedence when multiple selectors target the same element for the same property. A higher specificity score wins.
Specificity is calculated by counting different types of selectors:
- Inline Styles: Styles applied directly to an HTML element using the
styleattribute.- Adds 1 to the "inline" category (highest).
- Example:
<p style"color: blue;">= -> (1,0,0,0)
- IDs: Selectors using the
idattribute.- Adds 1 to the "ID" category.
- Example:
#my-unique-element-> (0,1,0,0)
- Classes, Attributes, Pseudo-classes: Selectors using class names, attribute selectors (e.g.,
[type"text"]=), and pseudo-classes (e.g.,:hover,:nth-child).- Adds 1 to the "class/attribute/pseudo-class" category.
- Example:
.my-class,[href],:first-child-> (0,0,1,0)
- Elements, Pseudo-elements: Selectors targeting HTML element names (type selectors) and pseudo-elements (e.g.,
::before,::first-line).- Adds 1 to the "element/pseudo-element" category.
- Example:
p,::after-> (0,0,0,1)
Calculation Example:
/* Specificity: (0,0,0,1) */ p { color: red; } /* Specificity: (0,0,1,1) */ div p.intro { color: blue; } /* Specificity: (0,1,0,1) */ #header p { color: green; } /* Specificity: (0,0,2,0) */ .card.featured { color: purple; }
In div p.intro, the score is (0 classes, 1 element div, 1 element p, 1 class intro). Wait, no, it's (0,0,1,1). div is an element, p is an element,.intro is a class. So (0,0,1,2). This is where counting can get tricky.
Let's simplify:
p- (0, 0, 0, 1)p.intro- (0, 0, 1, 1) (1 class, 1 element)div p.intro- (0, 0, 1, 2) (1 class, 2 elements)#header p- (0, 1, 0, 1) (1 ID, 1 element).card.featured- (0, 0, 2, 0) (2 classes)
Tie-breaking: If two rules have the exact same specificity score, the one that appears later in the stylesheet (or is loaded last) wins.
Inheritance
Inheritance refers to the mechanism where certain CSS properties applied to a parent element are automatically passed down (inherited) by its child elements. This helps to prevent repetitive styling and maintain consistency.
- Inherited Properties (Common Examples): color, font-family, font-size, line-height, text-align, list-style, visibility, cursor.
- If you set font-family: Arial; on the
<body>tag, all text within the body will generally inherit this font, unless a more specific rule overrides it on a child element.
- If you set font-family: Arial; on the
- Non-Inherited Properties (Common Examples): border, margin, padding, background-color, width, height, display, position.
- If you set a margin on a
<div>, its child elements will not automatically get that margin. They will have their own default margins or whatever you set for them.
- If you set a margin on a
- Controlling Inheritance:
- inherit keyword: You can explicitly force a property to inherit its value from its parent, even if it's normally non-inheritable, or to override a more specific rule. .child-element { margin-left: inherit; * Takes margin-left from its parent * }
- initial keyword: Resets a property to its initial (default browser) value.
- unset keyword: Behaves like inherit if the property is normally inherited; otherwise, it behaves like initial.
- revert keyword: Resets a property to the value it would have had if no changes had been made to the element's style by any author or user styles, effectively reverting to user agent/browser default. By understanding how the cascade weighs origins and specificities, and which properties are inherited, you gain precise control over how your CSS styles are applied and rendered on the web page.