Skip to content

Styling with CSS

1 min read3 lessons in topic

Learning objective

By the end of this lesson you will be able to add CSS to an HTML page to control colours, fonts and spacing.

HTML gives a page structure; CSS (Cascading Style Sheets) gives it style — colours, fonts, spacing and layout.

Three ways to add CSS

  • Inline — a style attribute on a single element
  • Internal — a <style> block in the page's <head>
  • External — a separate .css file linked with <link>

Selectors

<style>
  h1 { color: #3559ad; }
  .highlight { background-color: #fdf0f4; }
  #intro { font-size: 1.2rem; }
</style>
<h1 id="intro" class="highlight">Welcome</h1>

Selectors target elements: element names (h1), classes (.highlight) and ids (#intro). Classes are reusable; an id should appear once.

Common properties

  • color — text colour
  • background-color — element background
  • font-size — text size
  • margin — space outside the element
  • padding — space inside the element
<style>
  p { color: #0d2358; margin: 8px 0; padding: 4px; }
  .card { border: 1px solid #d9e1f4; border-radius: 8px; padding: 16px; }
</style>

Practice exercise

Take your About Me page and give the heading a colour, add a bordered card around the paragraph, and increase the font size.

Check your understanding

1. Which CSS property changes the text colour?

2. How do you select an element with class="highlight"?

Self-graded — answer then check below.