Published by Patrick Mutisya · 8 days ago
Know and understand the characteristics of Cascading Style Sheets (CSS), including the difference between attached stylesheets and inline style attributes, and the hierarchy of multiple attached stylesheets and inline styles within a web page.
Cascading Style Sheets (CSS) are a language used to describe the presentation of a web document written in HTML or XML. CSS separates content (HTML) from presentation (layout, colours, fonts), making pages easier to maintain and faster to load.
.css file linked to the HTML document with a <link> element.<style> element in the <head> of the HTML page.style attribute.| Method | Typical Use | Advantages | Disadvantages |
|---|---|---|---|
| External (attached) stylesheet | Site‑wide styling; shared by many pages |
|
|
| Internal stylesheet | Styling that applies only to a single page |
|
|
| Inline style attribute | One‑off styling for a specific element |
|
|
The term “cascade” describes how the browser decides which style rule to apply when several rules could affect the same element. The order of precedence is:
!important (override all other rules except another !important with higher specificity)When more than one external stylesheet is linked, the cascade follows the order of the <link> elements in the HTML document. Later sheets can override earlier ones if they contain rules with equal or higher specificity.
<head>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="layout.css">
<link rel="stylesheet" href="theme.css">
</head>
In the example above, a rule in theme.css that sets p { color: blue; } will override a conflicting rule in layout.css because theme.css is linked later.
When two rules have the same origin (e.g., both from external stylesheets), the browser uses specificity to decide which rule wins. Specificity is calculated based on selectors:
If two selectors have the same specificity, the later one in the cascade wins.
Consider the following HTML snippet:
<head>
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="override.css">
<style>
h1 { color: green; }
</style>
</head>
<body>
<h1 style="color: red;">Welcome</h1>
</body>
Which colour will the <h1> text appear?
h1 { color: blue; }h1 { color: orange; }h1 { color: green; }style="color: red;"According to the cascade hierarchy, the inline style has the highest precedence (unless an !important rule is present elsewhere). Therefore the heading will be displayed in red.
<link rel="stylesheet"> and are ideal for site‑wide consistency.<style> block in the page’s <head>.style attribute on individual elements.!important.!important declaration affect the cascade?.menu.