Nishaglobal Education logo

CSS basics: colours, fonts and your first stylesheet

Write your first CSS rules, link an external stylesheet to your HTML, and change text colour, background colour, fonts, font sizes, line spacing and alignment.

Lesson 6 of 1335 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Read and write a CSS rule: selector, property and value
  • Link an external stylesheet to an HTML page
  • Style text with colours, fonts, sizes, line height and alignment
Outline
  1. What you'll learn and why it matters
  2. Your first CSS rule
  3. Link a stylesheet to your page
  4. Colours
  5. Fonts and text
  6. Comments in CSS
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

What you'll learn and why it matters

Your pages so far use the browser's default look: black Times New Roman text on white. CSS lets you choose colours, fonts, spacing and layout. One stylesheet can style every page of a website, so changing a colour in one place changes it everywhere.

Your first CSS rule

A CSS rule has a selector that picks elements, and a declaration block in curly braces with one or more declarations: a property, a colon, a value and a semicolon.

selectordeclaration blockh1 { color: navy; }propertywhat to setvalueset it toone declarationThe selector chooses which elements to style.A block can hold many declarations, each ending in ;
The rule h1 { color: navy; }: h1 is the selector, the curly braces hold the declaration block, color is the property, navy is the value, and color: navy; together is one declaration.
CSS
h1 {
  color: navy;
  font-size: 2rem;
}
Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>

Browser preview

The selector h1 picks every <h1> on the page. The two paragraphs are not affected, because the rule only selects h1 elements.

Link a stylesheet to your page

The best place for CSS is its own file, usually called styles.css, linked from the <head> of every page. Let's set it up:

  1. In VS Code, create a new file called styles.css in your my-website folder, next to index.html.
  2. Add the <link> line below inside the <head> of index.html.
  3. Put your CSS rules in styles.css, save both files and refresh the browser.
index.html: the head with the stylesheet linked
<head>
  <meta charset="utf-8">
  <title>Green Leaf Café</title>
  <link rel="stylesheet" href="styles.css">
</head>
Where CSS can goExampleWhen to use it
External file (best)<link rel="stylesheet" href="styles.css">Almost always: one file styles every page
<style> in the head<style> h1 { color: navy; } </style>Quick tests, or a single-page example
Inline style attribute<p style="color: red;">Avoid: hard to find and change later

Colours

color sets the text colour and background-color the background. You can write colours in several ways:

WayExampleNotes
Named colournavy, white, tomatoAbout 140 names; easy to read
Hex code#1d4ed8Red, green and blue as two hex digits each; the most common in design tools
rgb()rgb(29 78 216)Red, green, blue from 0 to 255
CSS
body {
  background-color: #f0fdf4;
  color: #14532d;
}

h1 {
  color: #166534;
}
Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>

Browser preview

Fonts and text

font-family chooses the typeface. Give a list: the browser uses the first one installed on the visitor's device, and the last item should be a general family such as sans-serif (no little feet on the letters) or serif.

CSS
body {
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 1.125rem;
  line-height: 1.6;
}

h1 {
  font-size: 2.5rem;
  font-weight: 700;
  text-align: center;
}

p {
  max-width: 40ch;
}
Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>

Browser preview

PropertyExampleWhat it does
font-familyArial, sans-serifThe typeface, with backups
font-size1.125remText size. 1rem is the browser's base size (usually 16px)
font-weight700 or boldThickness: 400 is normal, 700 is bold
line-height1.6Space between lines, as a multiple of the font size. 1.5 to 1.7 is comfortable for paragraphs
text-aligncenterleft, center, right or justify
max-width40chLimits line length. ch is about one character wide

Some properties are inherited: set font-family or color on body and every element inside uses it too, unless a more specific rule says otherwise. That is why we style body first.

Comments in CSS

styles.css
/* Main colours for the whole site */
body {
  color: #14532d; /* dark green text */
}

CSS comments go between /* and */. They are for people reading the code; the browser ignores them.

Practice

Guided exercise

Style a profile

Style the profile page: a light blue background for the page, dark blue text, a sans-serif font, a centred heading in a larger size, and comfortable line spacing.

  1. Write a body rule with background-color, color, font-family and line-height.
  2. Write an h1 rule with font-size and text-align: center.
  3. Check the preview: every paragraph should inherit the font and colour from body.
Show the solution
Solution
body {
  background-color: #eff6ff;
  color: #1e3a8a;
  font-family: Arial, sans-serif;
  line-height: 1.6;
}

h1 {
  font-size: 2.25rem;
  text-align: center;
}
Show the HTML this CSS styles
<h1>Meera Iyer</h1>
<p>Student, artist and future web developer.</p>
<p>I love painting, cricket and building websites.</p>

Browser preview

How to get there: Colours and fonts set on body are inherited by everything inside it, so you only write them once. The h1 rule then changes just the heading.

Your turn

Two colours for two elements

On the café page, make the heading white on a dark green background (#166534), and make the paragraphs dark grey (#374151).

Show the solution
Solution
h1 {
  color: white;
  background-color: #166534;
}

p {
  color: #374151;
}
Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>

Browser preview

How to get there: Each selector picks different elements. The heading's background fills its whole width because headings are block elements; you will control that width in lesson 8.

Common mistakes

Missing semicolon between declarations

You see: The declaration after it is ignored

Fix: End every declaration with ;.

Wrong path in the link's href, or styles.css saved in another folder

You see: None of your CSS works

Fix: Put styles.css next to index.html and check the spelling. Press F12 and look at the Console for a file-not-found message.

Writing colour (British spelling)

You see: The colour does not change

Fix: CSS uses American spelling: color and background-color.

Putting a space in a value like 1.5 rem

You see: The size is ignored

Fix: Numbers and units go together: 1.5rem, 16px.

Forgetting the closing }

You see: Every rule after it stops working

Fix: Check each rule has { and }. VS Code highlights the matching brace when you click next to one.

Recap

  • A rule is a selector plus declarations: selector { property: value; }.
  • Put CSS in styles.css and link it with <link rel="stylesheet" href="styles.css">.
  • Colours can be names, hex codes or rgb(); keep strong contrast between text and background.
  • Style body first: fonts and colours are inherited. Use rem for font sizes and 1.5 to 1.7 line height.

Key terms

Selector
The part of a CSS rule that chooses which elements to style.
Declaration
One property: value; pair inside a rule.
Inheritance
Some properties, such as color and font-family, pass from a parent element to its children.
rem
A unit equal to the root font size, usually 16px, that follows the user's text-size setting.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. In `p { color: green; }`, what is `color`?

  2. 2. Which line links an external stylesheet called styles.css?

  3. 3. You set `font-family` on body. Why do the paragraphs use that font too?

  4. 4. Why is `rem` a good unit for font sizes?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.