Cascading Style Sheets (CSS) Fundamentals
From the 123 curriculum
Cascading Style Sheets (CSS) Fundamentals
TL;DR
CSS lets you control how your web content looks, like colors, fonts, and layout. It works by linking styles to HTML elements using selectors and properties. Understanding how styles "cascade" and are inherited is key to predictable design.
1. The Mental Model
Think of CSS as the "paint and blueprint" for your HTML "building blocks." HTML provides the structure (like walls and rooms), and CSS applies the decorations (colors, wallpaper, furniture arrangement) and structural rules (room sizes).
2. The Core Material
What is CSS?

Photo by RealToughCandy.com on Pexels
CSS stands for Cascading Style Sheets. It's a language used to describe the presentation of a web page written in HTML. This separation of content (HTML) from presentation (CSS) makes web development more efficient and maintainable.
How CSS Connects to HTML

Photo by César Gaviria on Pexels
There are three main ways to connect CSS to HTML:
-
Inline Styles: Applied directly to an HTML element using the
styleattribute. Good for quick, isolated changes, but generally not recommended for larger projects as it mixes content and presentation.html <p style="color: blue; font-size: 16px;">This text is blue and 16px.</p> -
Internal (Embedded) Styles: Placed within a
<style>tag in the<head>section of an HTML document. Useful for single pages with unique styles.html <!DOCTYPE html> <html> <head> <title>My Page</title> <style> h1 { color: green; text-align: center; } </style> </head> <body> <h1>Welcome!</h1> </body> </html> -
External Stylesheets: The most common and recommended method. Styles are written in a separate
.cssfile and linked to the HTML document using a<link>tag in the<head>. This allows you to apply the same styles to multiple HTML pages.html <!-- In your HTML file (e.g., index.html) --> <head> <link rel="stylesheet" href="styles.css"> </head>
css /* In your CSS file (e.g., styles.css) */ body { font-family: Arial, sans-serif; background-color: lightgray; }
CSS Syntax: Rulesets

Photo by Pixabay on Pexels
A CSS ruleset consists of a selector and a declaration block. The declaration block contains one or more declarations, each with a property and a value.
/* This is a CSS ruleset */
selector { /* This is the selector */
property: value; /* This is a declaration */
another-property: another-value; /* Another declaration */
}
- Selector: Points to the HTML element(s) you want to style.
- Property: The specific visual characteristic you want to change (e.g.,
color,font-size,margin). - Value: The setting for that property (e.g.,
blue,16px,20px).
Common Selectors

Photo by Black ice on Pexels
- Element Selector: Selects all instances of an HTML element (e.g.,
p,h1,div). -
Class Selector: Selects elements with a specific
classattribute. Preceded by a dot (.). You can apply the same class to multiple elements.html <p class="highlight">Important text</p> <span class="highlight">Also important</span>
css .highlight { background-color: yellow; }
* ID Selector: Selects a single element with a specificidattribute. Preceded by a hash (#). IDs should be unique per page.html <div id="main-header"></div>
```cssmain-header {
border-bottom: 1px solid black;}
```
The Cascade
The "C" in CSS stands for Cascading. This means that if multiple styles apply to the same element, CSS has rules to decide which style takes precedence.
Here's the general order of precedence (from lowest to highest):
- Browser's default styles
- User-defined styles (if any)
- External stylesheets
- Internal (embedded) stylesheets
- Inline styles
!important(overrides almost everything, use sparingly)
When multiple rules apply to an element and have the same origin (e.g., all from an external stylesheet), specificity and order determine the winner. More specific selectors (e.g., ID selectors are more specific than class selectors) win. If specificity is the same, the last declared rule wins.
graph TD
A["Browser Default Styles"] --> B["User Defined Styles"]
B --> C["External Stylesheet"]
C --> D["Internal (Embedded) Styles"]
D --> E["Inline Styles"]
E --> F["!important Declaration"]
F --> G["Final Applied Style"]
Inheritance
Some CSS properties are inherited by child elements from their parent elements. For example, font-family and color are often inherited. If you set font-family: Arial; on the body element, all text inside the body (unless overridden) will use Arial. Properties like border and margin are generally not inherited.
3. Worked Example
Let's say you have this HTML:
<!DOCTYPE html>
<html>
<head>
<title>CSS Example</title>
<link rel="stylesheet" href="my-styles.css">
<style>
.card {
background-color: #f0f0f0;
padding: 10px;
}
</style>
</head>
<body>
<header id="main-header">
<h1>My Awesome Site</h1>
</header>
<main>
<p>Welcome to my site. Here's some content.</p>
<div class="card">
<h2>Product A</h2>
<p style="color: purple;">This is product A's description.</p>
</div>
<div class="card special-card">
<h2>Product B</h2>
<p>This is product B's description.</p>
</div>
</main>
</body>
</html>
And this my-styles.css file:
/* my-styles.css */
body {
font-family: Verdana, sans-serif;
color: #333;
margin: 20px;
}
#main-header {
background-color: #007bff;
color: white;
padding: 15px;
text-align: center;
}
h1 {
font-size: 2.5em; /* 2.5 times the base font size */
}
h2 {
color: #0056b3;
font-size: 1.8em;
}
.special-card {
border: 2px dashed red;
box-shadow: 3px 3px 5px rgba(0,0,0,0.3);
}
p {
line-height: 1.5; /* Spacing between lines */
}
Here's what happens:
bodystyles: ThebodygetsVerdanafont, dark gray text, and a 20px margin frommy-styles.css. All its children, includingheader,main,h1,h2,p, will inheritfont-familyandcolorunless overridden.#main-headerstyles: The header gets a blue background, white text, padding, and centered text frommy-styles.css.h1styles: Theh1in the header gets afont-sizeof2.5emfrommy-styles.css. Its color is inherited white from#main-header..cardstyles: Bothdivelements with classcardget a light gray background and 10px padding from the internal style block. Since internal styles have higher precedence than external styles, ifmy-styles.cssalso had a.cardrule, the internal one would win.h2styles: Bothh2elements get a dark blue color and1.8emfont size frommy-styles.css.- Inline
pstyle: The firstpinside.cardexplicitly sets itscolortopurplevia an inline style. This takes precedence over thebody's inherited#333color. .special-cardstyles: The seconddivgets a red dashed border and a shadow frommy-styles.css. This rule is additive to the.cardstyles because it affects different properties. If both.cardand.special-cardtried to setbackground-color, the.special-card(due to being defined later in this scenario) would win if it was in the same stylesheet and had equal specificity.
4. Key Takeaways
- CSS defines the visual presentation of your HTML content, separating structure from style.
- You can connect CSS to HTML using inline, internal, or external methods, with external being best practice for larger projects.
- CSS rulesets use selectors to target HTML elements and declarations (property: value;) to style them.
- Understanding specificity and order of declaration is crucial for predicting which styles will apply when multiple rules conflict.
- The "cascade" means styles are applied in a specific order, with more specific or later-defined rules usually winning.
- Some properties, like
font-familyandcolor, are inherited by child elements from their parents. - Use class selectors for reusable styles and ID selectors for unique elements on a page.
Common mistakes you should avoid:
- Don't use inline styles for extensive styling; it makes your code messy and hard to maintain.
- Avoid overusing !important as it breaks the natural cascade and makes debugging difficult.
- Forgetting to link your external stylesheet in the HTML <head> is a common oversight.
- Not understanding selector specificity can lead to styles not applying as you expect.
- Using IDs for styling elements that appear multiple times; use classes instead.
5. Now Try It
Create a new HTML file (index.html) and
Frequently asked about Cascading Style Sheets (CSS) Fundamentals
More from 123
Get the full 123 curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account