CSS Introduction
CSS (Cascading Style Sheets) is the language that controls how web pages look: colours, fonts, spacing, layout and even animation. HTML decides what is on the page; CSS decides how it is presented. Every modern website you use, from your bank's app to a matatu booking site, relies on it.
What CSS does
A browser can display plain HTML on its own, but the result looks like a document from the early 1990s: black Times New Roman text on a white background, blue underlined links and everything stacked down the left edge. CSS lets you change all of that. With it you can:
- choose colours and backgrounds, including gradients and images;
- set fonts, text sizes, line spacing and alignment;
- add space around and inside elements, plus borders and rounded corners;
- arrange elements into columns, cards, navigation bars and full page layouts;
- adapt the layout to phones, tablets and large screens;
- animate changes smoothly, for example when someone hovers over a button.
A first look
Here is a tiny HTML page. The <style> element in the head holds CSS that changes the background, the heading and the paragraph. Press Try it, then change some values and watch the preview update.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My first styles</title>
<style>
body {
background-color: #fdf6e3;
font-family: system-ui, sans-serif;
padding: 2rem;
}
h1 {
color: #1e3a8a;
font-size: 2.5rem;
}
p {
color: #444;
line-height: 1.6;
max-width: 40ch;
}
</style>
</head>
<body>
<h1>Karibu!</h1>
<p>This page is styled with CSS. Change the colours above and see what happens.</p>
</body>
</html>
The browser shows a cream-coloured page with comfortable space around the edges. The heading "Karibu!" is large and dark blue, and the paragraph is dark grey, set in the device's own clean font, with generous line spacing and a line length of about 40 characters.
Why "cascading"?
Several rules can try to style the same element. One rule might say all paragraphs are grey, another that a particular paragraph is red. The cascade is the set of rules the browser uses to decide which declaration wins: it looks at where the style came from, how specific the selector is and which rule came last. You will study it properly in Specificity & the Cascade; for now, remember that later rules usually override earlier ones when everything else is equal.
Some properties are also inherited. If you set color or font-family on body, every paragraph, list and heading inside it picks up that value unless something more specific overrides it. Layout properties such as padding and border are not inherited.
Separation of content and style
Keeping HTML for structure and CSS for appearance has real benefits:
| Benefit | What it means in practice |
|---|---|
| Consistency | One stylesheet can style hundreds of pages, so every page shares the same look. |
| Easy redesigns | Change one file and the whole site updates, without touching the HTML. |
| Faster sites | The browser downloads an external stylesheet once and reuses it on every page. |
| Accessibility | Clean, semantic HTML works well with screen readers, and users can apply their own styles. |
A short history
Håkon Wium Lie proposed CSS in 1994 while working at CERN, and the first official version, CSS level 1, became a W3C Recommendation in 1996. After CSS 2, the language was split into separate modules (colour, selectors, Flexbox, Grid and so on) that develop at their own pace. That is why there is no single "CSS4": browsers add new features module by module, and the CSS you learn today includes powerful tools such as Grid, custom properties and container queries that are supported in every major browser.
Every browser has built-in developer tools. Right-click any element on a web page and choose Inspect to see exactly which CSS rules apply to it. You can even edit values live, which is one of the fastest ways to learn.
Key points
- CSS controls presentation: colour, typography, spacing, layout and animation.
- HTML provides the structure; CSS styles that structure.
- The cascade decides which rule wins when several target the same element.
- Some properties, such as
colorandfont-family, are inherited by child elements. - Next, learn the three ways to add CSS to HTML.
Tip: use the ← and → keys to move between pages.
