BeginnerLesson 1 of 305 min read

What is CSS and how to add it to a web page

Understand what CSS does, write your first rule and learn the three ways to add styles to a page.

In this lesson you will

  • Explain what CSS does
  • Write a rule with a selector, property and value
  • Add CSS in three ways
  • Debug a stylesheet that does nothing

About 13 minutes

Why plain HTML looks plain

Open one of your HTML pages. Black text, white background, blue underlined links, everything stacked in a single column. It works, and it is also exactly the way web pages looked in 1995. Now think about a website you love. It has colours, spacing, nice fonts, buttons that react when you hover over them. Both are made of HTML. The difference is CSS.

CSS stands for Cascading Style Sheets. If HTML is the skeleton of a page, CSS is the clothing, the paint and the furniture arrangement. HTML says “this is a heading”. CSS says “make that heading large, purple and centred”. The two jobs are kept separate on purpose, so you can redecorate a whole site without touching its content.

Your first rule

CSS is written as rules. Each rule picks something on the page and says how it should look.

h1 {
  color: purple;
  font-size: 40px;
}
  • h1 is the selector. It says which elements the rule applies to.
  • Everything inside the curly braces is the declaration block.
  • color: purple; is a declaration. color is the property and purple is the value.
  • Each declaration ends with a semicolon. Forget one, and the next declaration may stop working.

Press Run to see the plain heading, then change the colour to tomato and the font-size to 48px.

<style>
  h1 {
    color: purple;
    font-size: 32px;
  }
</style>
<h1>Hello, CSS</h1>
<p>This paragraph is not styled yet.</p>

Edit the two values inside the curly braces. Keep the colon and semicolon.

<style>
  h1 {
    color: tomato;
    font-size: 48px;
  }
</style>
<h1>Hello, CSS</h1>
<p>This paragraph is not styled yet.</p>

Three ways to add CSS

There are three places you can put CSS, and learning when to use each saves a lot of confusion later.

1. Inline: on one element

<p style="color: green;">This paragraph is green.</p>

Quick for a test, but it styles only that single element and mixes looks into your content. Avoid it for real work.

2. Internal: in a style element

<head>
  <style>
    p { color: green; }
  </style>
</head>

Good for a one-page experiment, and it is what the practice editors in this course use.

3. External: in its own file

<head>
  <link rel="stylesheet" href="style.css">
</head>

This is the professional method. You write all your rules in a file called style.css, and every page of your site links to it. Change one file and the whole site updates.

Good to know

When a page links to an external stylesheet, always use rel="stylesheet" and put the link in the head. Check the file path carefully. A wrong path is the number one reason beginners say that their CSS does nothing.

Your first external stylesheet

  1. In your project folder, create a new file named style.css next to your HTML file.
  2. Write a rule, for example body { background: #FAF8FF; }.
  3. Add <link rel="stylesheet" href="style.css"> to the head of your HTML.
  4. Save both files and refresh the browser.
  5. Change a value, save and refresh again. This save-and-refresh loop is how all front-end developers work.

Comments

Notes for yourself that the browser ignores. Start with a slash and a star, and end with a star and a slash.

/* Page background */
body { background: #FAF8FF; }

/* body { color: red; }  <- switched off for now */

Comments are also a handy way to switch a rule off while testing, without deleting it.

Reading CSS errors

CSS does not shout when something is wrong. A mistake simply makes that declaration, or sometimes the next rule, quietly stop working. When your styles do not appear, check these in order:

  • Is the stylesheet linked correctly, and is the file name spelled exactly right?
  • Is there a missing semicolon, colon or closing brace?
  • Is a property misspelled? colour will not work. CSS uses the American color.
  • Does the selector actually match the element? Check the spelling of class names.
  • Open the browser’s developer tools with F12. Invalid or crossed-out declarations show in the Styles panel.
Common mistake

Writing color = red or color: red without a semicolon at the end of a line, then adding another declaration below. The browser reads both lines as one broken line, and neither works.

This rule has three mistakes: a missing semicolon, a wrong spelling of a property, and an equals sign. Fix them so the heading turns blue and centred.

<style>
  h1 {
    color = blue
    text-align: centre;
  }
</style>
<h1>Fix my CSS</h1>

Use a colon after color, finish the line with a semicolon, and use the American spelling center.

<style>
  h1 {
    color: blue;
    text-align: center;
  }
</style>
<h1>Fix my CSS</h1>

What you will learn in this course

Over the next thirty lessons you will move from your first colour to full responsive layouts. The beginner lessons cover selectors, colours, text, the box model and the cascade. The intermediate lessons add Flexbox, Grid, responsive design and positioning. The advanced lessons cover animations, custom properties, modern CSS features and a professional workflow. Everything builds on what you did in the HTML course, so keep that portfolio handy. You will style it by the end.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does CSS control?

  1. The structure of a page
  2. How a page looks
  3. Where files are stored
  4. How servers work

HTML provides structure and CSS controls appearance.

In color: red;, what is color?

  1. The selector
  2. The property
  3. The value
  4. The comment

color is the property. red is the value.

Which is the professional way to add CSS to many pages?

  1. Inline style attributes
  2. An external stylesheet linked in the head
  3. Writing it in the body
  4. Using the font tag

One external file styles the whole site.

Which tag links an external stylesheet?

  1. <style src>
  2. <link rel="stylesheet" href="style.css">
  3. <css>
  4. <script>

A link element with rel=stylesheet loads the file.

Your styles do nothing. What should you check first?

  1. The file path and spelling
  2. Your internet speed
  3. The monitor
  4. The page title

Wrong paths and typos are the most common causes.

Summary

  • CSS controls how HTML looks.
  • A rule is a selector plus declarations.
  • Use an external stylesheet for real sites.
  • Check paths, spelling and semicolons when styles fail.

Next lesson

Next: selectors, the way you choose what to style.

Questions

Do I need to know HTML first?

A little. You should know what tags, classes and ids are. The HTML course on this site covers all of it.

Is CSS a programming language?

No. It is a style language. It describes how things should look rather than running logic.

Which editor should I use?

Any code editor works. VS Code is free and popular, and it has helpful colour previews for CSS.

Progress is saved in this browser.