Lesson 1 / 128%

Lesson 1

Introduction

10 min

Understand what CSS is and how it applies to an HTML page.

What is CSS?

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.

Why separate content and style?

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:

  • Consistency — a single rule can apply to hundreds of pages: changing the color of the site's headings means editing one line, not a hundred files.
  • Readability — your HTML stays clean, with no tags cluttered by appearance details.
  • Adaptability — the same HTML can be displayed differently on a phone, a tablet, a desktop screen or in print, simply by changing the CSS.
  • Teamwork — one person can take care of the content while another takes care of the design.

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.

A bit of history

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.

The three ways to add CSS

There are three ways to apply CSS to an HTML page, each with its own use:

1. External CSS (recommended)
<!-- In the <head> of the HTML file -->
<link rel="stylesheet" href="/styles.css" />
2. Internal CSS
<!-- In the <head> of the HTML file -->
<style>
  p {
    color: navy;
  }
</style>
3. Inline CSS (avoid except in one-off cases)
<p style="color: navy; font-weight: bold;">Styled text</p>
  • External CSS — a separate .css file, reusable across several pages. This is the method to prefer in 95% of cases.
  • Internal CSS — useful for a quick prototype or a single page, but can't be shared between several HTML files.
  • Inline CSS — the highest priority in the cascade, but also the hardest to maintain: keep it for very occasional cases (e.g. generated dynamically by JavaScript).

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.

Linking a stylesheet properly

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.
css-paths.html
<!-- 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.

Anatomy of a CSS rule

A CSS rule associates a selector (which element to target) with a block of declarations (which properties to apply).

Anatomy of a CSS rule

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.

vocabulary.css
h1 {
  color: purple;      /* property: color, value: purple */
  font-size: 2rem;    /* property: font-size, value: 2rem */
}
  • Selector — designates the HTML elements to style (here, all theh1 elements of the page).
  • Property — the aspect you want to change (color, size, margin...).
  • Value — the new value of this property, separated from its name by a colon.
  • Declaration — a 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:

grouping.css
/* The three heading levels share the same color */
h1,
h2,
h3 {
  color: #6d28d9;
  font-family: Georgia, serif;
}

Values you will often encounter

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:

values.css
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;
}
  • Colors — by name (navy), in hexadecimal (#6d28d9), or with the rgb()and hsl() functions.
  • Sizes — in pixels (px, fixed size), inrem (relative to the page's base text size), or as a percentage (%, relative to the parent).
  • Keywords — predefined values like center, bold or none.

A first complete example

index.html
<!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>
styles.css
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
}

h1 {
  color: #6d28d9;
}

p {
  color: #334155;
  line-height: 1.6;
}
Example of a web page with CSS

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).

The cascade in action

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:

cascade.css
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.

Inheritance

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.

inheritance.css
/* 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;
}

CSS comments

As in HTML, you can leave notes in your CSS code, between /* and */.

comments.css
/* 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.

Writing readable CSS

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}
  • One declaration per line, indented by two spaces.
  • A blank line between two rules.
  • A space after the colon and the opening brace on the selector's line.
  • A semicolon at the end of every declaration, including the last one, to avoid mistakes when adding another.

Inspecting CSS with browser tools

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:

  • Struck-through rules are those that have been overridden by a higher-priority rule: this is the cascade live.
  • You can edit a value live or check and uncheck a declaration to see the effect immediately, without touching your file.
  • The file and line number where each rule comes from are shown, which lets you easily find 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.

Common beginner mistakes

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:

  • Forgetting the ; at the end of a declaration: the next line is then ignored or misinterpreted.
  • Using the = sign instead of a colon: you write color: red, not color = red.
  • Getting a property name wrong (colour, background_color): CSS names are in English, lowercase, with hyphens.
  • Forgetting the unit of a size: font-size: 16 is invalid, you need to write 16px.
  • Forgetting to close a brace: all the rules that follow may stop working.
  • Getting the path of the <link> wrong, or forgetting to save the CSS file before refreshing the page.
  • Putting the <style> or <link> tags in the body instead of the head.

Key takeaways

  • HTML describes the structure, CSS describes the appearance: keep them separate.
  • Three ways to add CSS: external (preferred), internal and inline (one-off cases).
  • A rule = a selector + declarations in curly braces, each in the form property: value;
  • In case of conflict, the rule written last wins at equal priority; some text properties are inherited from parents.
  • CSS comments are written between /* */.
  • The developer tools (F12) let you see and test CSS live.

Exercise

Create a 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

Add a rule that styles all the 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

In your stylesheet, write two contradictory rules for paragraphs (for example a blue color and then a red color) and check which one wins. Then define a font and a text color only on the 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.