Adding style to a page

Lesson 1 of 14 11 min read Free lesson

What CSS is, how a rule is written, and the three ways to add CSS to a page.

What is CSS?

CSS (Cascading Style Sheets) describes how HTML elements look: colours, fonts, spacing, layout and even animation. HTML says *what* something is; CSS says *how it looks*.

A CSS rule

style.css Try it in CodeLab
h1 {
  color: #000080;
  font-size: 2rem;
}
PartExampleMeaning
Selectorh1Which elements to style
PropertycolorWhat to change
Value#000080What to change it to
Declarationcolor: #000080;One property and its value, ending with ;

Three ways to add CSS

HTML Try it in CodeLab
<!-- 1. External file (best): one file styles every page -->
<link rel="stylesheet" href="style.css">

<!-- 2. Internal: a <style> block in the <head> -->
<style>
  p { line-height: 1.6; }
</style>

<!-- 3. Inline: on one element only (avoid) -->
<p style="color: red;">Warning</p>
CodeLab challenge

Style a welcome card

Give the card a background colour, rounded corners and some padding. Try changing the values and press Run.

Check your understanding

Quick check

In "p { color: red; }", what is p?

Quick check

What is the best way to add CSS to a whole website?

Quick check

What ends each declaration?

Hi there! đź‘‹ Have a question about the courses or joining the class? Ask me.