🎨 Web Development

Introduction to CSS

⏱ 2 hr2 topicsInteractive
🎯 By the end: You can write CSS rules, apply them three ways, and style text, backgrounds, borders and spacing.

CSS (Cascading Style Sheets) controls how a web page looks — colours, fonts, spacing — separately from the HTML, which holds the content. Edit the live examples and press Run.

1Why CSS, the three ways & syntax

Keeping style in CSS (not in the HTML) means you can restyle a whole site by editing one place. A CSS rule has a selector and property: value; pairs:

p {
  color: navy;
  font-size: 18px;
}
Way to apply CSSWhere
InlineA style attribute on one tag
InternalA <style> block in the page's head
ExternalA separate .css file linked to the page

Try a <style> block (internal CSS):

CSS syntax▶ live preview
index.html
Preview
Key points
  • CSS separates how a page LOOKS from the HTML CONTENT, so one change restyles a whole site.
  • A rule = selector { property: value; ... }.
  • Apply CSS three ways: inline (style attribute), internal (<style> block), external (.css file).

2Styling text, backgrounds, borders & margins

Common CSS properties:

PropertySets
colorText colour
font-size / font-familyText size / typeface
background-colorBackground colour
borderA border (width, style, colour)
marginSpace outside the element
text-alignleft, center, right

Edit the card's colours, border and spacing, then Run:

Style a card▶ live preview
index.html
Preview
Key points
  • color sets text colour; background-color sets the background; font-size/font-family set the text.
  • border draws a border (width, style, colour); margin adds space outside the element.
  • text-align positions text (left, center, right).

★ Practical: style a page

Using an internal &lt;style&gt; block:

  1. Set the page text colour and a background colour.
  2. Style a heading: centre it and change its font-family and font-size.
  3. Make a box with a border, margin and padding.
  4. Use one class selector applied to two different elements.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.