1. Methods of Integrating CSS
There are three primary ways to link CSS rules to an HTML document. Understanding all three is important for proper debugging, though one method is strongly preferred for professional work.
A. External Stylesheet (Recommended)
This is the standard, best practice method. Styles are kept in a separate .css file and linked in the <head> section of the HTML.
- Pros: Clean separation of concerns (structure vs. presentation), improved caching (browser loads CSS once for many pages), and reusability across multiple HTML pages.
- Syntax:
<link rel="stylesheet" href="css/styles.css">
B. Internal Styles (Embedded)
Styles are defined directly within the HTML document using the <style> tag, typically inside the <head>.
- Pros: Useful for single-page applications or when only one page requires unique styles.
- Cons: Mixes presentation with structure, cannot be reused across pages.
- Syntax:
<head>
<style>
h1
{
color: green;
}
</style>
</head>
C. Inline Styles
Styles are applied directly to a single HTML element using the style attribute.
Pros: Used rarely, perhaps for very specific, dynamic, one-off overrides often generated by JavaScript.
Cons: Worst practice for general styling; non-reusable; makes code hard to read and maintain.
Syntax:
<p style="color: red; font-size: 16px;">This text is styled inline.</p>
2. The Cascade
The Cascade is the primary algorithm CSS uses to resolve conflicts. When multiple CSS rules try to style the same element, the browser uses three main factors, in order, to determine which rule wins:
- Origin: Where the style came from (User Agent, Author, or User).
- Specificity: Which selector is more precise.
- Source Order: Which rule appeared last in the stylesheet.
3. Specificity (The Tie-Breaker)
Specificity is the set of rules used to determine which rule applies when two or more selectors point to the same element. It is calculated by assigning a point value to each part of the selector.
The scoring system is often represented by three columns: (A, B, C).
| Selector Type | Column A (IDs) | Column B (Classes, Attributes, Pseudo-Classes) | Column C (Elements, Pseudo-Elements) | Example Score |
| Inline Style | 1 | 0 | 0 | (1, 0, 0) |
ID Selector (#logo) | 1 | 0 | 0 | (1, 0, 0) |
Class Selector (.card) | 0 | 1 | 0 | (0, 1, 0) |
Element Selector (p) | 0 | 0 | 1 | (0, 0, 1) |
Combined (#nav .link p) | 1 | 1 | 1 | (1, 1, 1) |
How Scoring Works:
The scores are compared column by column, from left to right.
- Example 1:
.nav-bar a(Score: 0, 1, 1) loses to#nav a(Score: 1, 0, 1). The ID wins the comparison in column A. - Example 2:
ul li.item(Score: 0, 1, 2) loses toul.nav-list li.item(Score: 0, 2, 2). The rule with two classes wins the comparison in column B.
4. Source Order (The Final Tie-Breaker)
If two competing rules have the exact same specificity score, the one that appears last in the stylesheet (or in the sequence of linked styles) is applied.
5. The !important Override
The keyword !important can be added after any property value to override all normal specificity and cascade rules.
p {
color: red !important; /* This rule will almost always win */
color: blue; /* This is ignored */
}
Warning: Use
!importantonly as a last resort (e.g., for utility classes or overriding third-party styles). Its overuse makes your CSS maintenance extremely difficult.
