Lesson 1
10 min
Understand what CSS is and how it applies to an HTML page.
CSS (Cascading Style Sheets) is the language that defines the appearance of an HTML page: colors, fonts, spacing, layout, animations. Without CSS, a web page is just a stack of plain text from top to bottom, with no formatting.
The word "cascade" comes from the fact that several rules can apply to the same element, and that CSS follows precise priority rules to decide which one wins. You will learn these rules in detail in the lesson on selectors.
You have learned that HTML describes the structure and the meaning of the content: this is a heading, this is a paragraph, this is a menu. CSS takes over to decide the appearance of each of these elements. This separation of roles is one of the great principles of the web, and it brings several concrete benefits:
In the past, appearance was put directly in the HTML with tags like <font> or <center>. These tags are now obsolete: CSS has replaced them all.
CSS was proposed in 1994 by Håkon Wium Lie, then published in its first official version (CSS1) in 1996. Later versions added new capabilities: positioning (CSS2), then animations, rounded corners, shadows, grids and many more with CSS3. Today, we no longer talk about big numbered versions: the language is split into "modules" that each evolve at their own pace. Just remember that the CSS you are learning is modern and supportedby all recent browsers.
There are three ways to apply CSS to an HTML page, each with its own use:
<!-- In the <head> of the HTML file -->
<link rel="stylesheet" href="/styles.css" /><!-- In the <head> of the HTML file -->
<style>
p {
color: navy;
}
</style><p style="color: navy; font-weight: bold;">Styled text</p>.css file, reusable across several pages. This is the method to prefer in 95% of cases.Why is external CSS the best choice? Because it applies the separation principle seen above: a single style file, linked from all your pages. The browser downloads it only once and then keeps it in memory (the "cache"), which also speeds up navigation from one page to another. Conversely, inline CSS forces you to repeat the same style everywhere and makes changes tedious.
The <link> tag goes in the <head> of the page. Like <img>, it is a void element, with no closing tag. It uses two attributes:
rel="stylesheet" — indicates the nature of the link: here, a stylesheet.href — the path to the CSS file. It follows the same rules as for links and images: absolute path from the site root, relative path, or full address.<!-- Absolute path: from the site root -->
<link rel="stylesheet" href="/styles.css" />
<!-- Relative path: file in the same folder as the page -->
<link rel="stylesheet" href="styles.css" />
<!-- Relative path: file in a subfolder -->
<link rel="stylesheet" href="css/styles.css" />
<!-- Several stylesheets are possible -->
<link rel="stylesheet" href="/base.css" />
<link rel="stylesheet" href="/home.css" />When several stylesheets are linked, they apply in the order in which they appear: if two rules contradict each other, the one from the stylesheet linked last wins. If your style isn't being applied, first check the path in href: it is the most frequent cause.
A CSS rule associates a selector (which element to target) with a block of declarations (which properties to apply).

Each declaration ends with a semicolon ;, and all the declarations for a selector are wrapped in curly braces { }.
Let's review the vocabulary with an example. In the rule below, h1 is the selector, color and font-size are properties, and purple and 2rem are their values. Each property and value pair forms a declaration.
h1 {
color: purple; /* property: color, value: purple */
font-size: 2rem; /* property: font-size, value: 2rem */
}h1 elements of the page).property: value; pair.A rule can contain as many declarations as needed, and you can apply the same declarations to several selectors by separating them with a comma:
/* The three heading levels share the same color */
h1,
h2,
h3 {
color: #6d28d9;
font-family: Georgia, serif;
}A CSS value is not always a simple word. Here are the most common families of values, which you will explore further in the following lessons:
p {
/* Colors: name, hexadecimal, rgb or hsl */
color: navy;
background-color: #f5f5f5;
border-color: rgb(109, 40, 217);
/* Sizes: pixels, rem, percentage */
font-size: 18px;
margin-bottom: 1.5rem;
width: 80%;
/* Keywords */
text-align: center;
font-weight: bold;
}navy), in hexadecimal (#6d28d9), or with the rgb()and hsl() functions.px, fixed size), inrem (relative to the page's base text size), or as a percentage (%, relative to the parent).center, bold or none.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Example Page</title>
<link rel="stylesheet" href="/styles.css" />
</head>
<body>
<h1>Welcome</h1>
<p>Here is a paragraph styled with external CSS.</p>
</body>
</html>body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
}
h1 {
color: #6d28d9;
}
p {
color: #334155;
line-height: 1.6;
}
Look closely at what is happening: the HTML file contains no appearance information, only the structure and the link to the stylesheet. The body receives a font and a background color, which are inheritedby its children; the heading receives its own color; and the paragraphs receive a text color and a more spacious line spacing (the line-height property).
What happens when two rules target the same element and contradict each other? This is where the "cascade" comes into play. Here is the basic idea, with an example:
p {
color: blue;
}
p {
color: red;
}Here, the paragraphs will be red: at equal priority, the rule written last wins. Other criteria come into play, such as the precision of the selector ("specificity") or the origin of the rule (inline, internal, external CSS). We will look at them in detail later; for now, remember that the order of writing matters.
Some properties, notably those related to text (color,font-family, line-height), areinherited: if you define them on a parent element, all its descendants receive them automatically. This is why it is enough to define the font on the body for it to apply to the whole page. Other properties, like borders or margins, are not inherited.
/* All the text on the page will use this font */
body {
font-family: Arial, sans-serif;
color: #334155;
}
/* Headings keep the font but change color */
h1 {
color: #6d28d9;
}As in HTML, you can leave notes in your CSS code, between /* and */.
/* This is a CSS comment */
p {
color: red; /* you can also comment at the end of a line */
}
/*
Comment
over several lines
*/Be careful, the syntax is not the same as in HTML: CSS comments are not written with <!-- -->. As soon as your file grows, use comments to split it into main parts ("Headings", "Menu", "Footer"...) and easily find each piece. They are also used to temporarily disable a rule to test the effect of its absence.
The browser doesn't care about the formatting of your CSS, but you do, and so do the people who will read your code. A few habits to pick up from the start:
Readable
h1 {
color: #6d28d9;
font-size: 2rem;
}
p {
color: #334155;
}Hard to read
h1{color:#6d28d9;font-size:2rem}p{
color:#334155}The browser's developer tools (the F12 key, or right-click then "Inspect") are your best ally for learning CSS. By selecting an element, you see all the rules that apply to it:
Changes made in the tools disappear when the page is reloaded: it is a risk-free sandbox. When an experiment works for you, copy it into your CSS file.
Good habit
Always link your CSS stylesheet at the very start, even if it is empty at first, before you build the rest of your HTML. This keeps you from forgetting the link later and lets you immediately see the effect of each rule you add.
CSS is very forgiving: when a declaration is invalid, the browser ignores it without displaying an error. This is convenient for compatibility, but it makes mistakes hard to spot. Here are the most frequent pitfalls:
; at the end of a declaration: the next line is then ignored or misinterpreted.= sign instead of a colon: you write color: red, not color = red.colour, background_color): CSS names are in English, lowercase, with hyphens.font-size: 16 is invalid, you need to write 16px.<link> wrong, or forgetting to save the CSS file before refreshing the page.<style> or <link> tags in the body instead of the head.property: value;/* */.Exercise
styles.css file and link it to an existing HTML page. Change the background color of the bodyand the color of an h1 heading.Going further
p paragraphs of your page (color, font size, line height), then compare the result by commenting and uncommenting the <link> tag in your HTML.Challenge: cascade, inheritance and developer tools
body to see that they are inherited by all the elements. Finally, open the developer tools (F12), spot the struck-through rule in the styles panel, then uncheck a declaration to observe the effect live. Organize your whole file with section comments.