CSS (Cascading Style Sheets) determines the look: colours, fonts, spacing and layout. The same HTML can look completely different with a different CSS file. This lesson covers rules, selectors (element, class, id), colours, fonts and how the browser decides which rule wins.
Selectors
<style>
p { color: #333; } /* all paragraphs */
.important { background: #fff3cd; } /* all elements with class="important" */
#title { color: #2b7bb9; } /* the one element with id="title" */
li a { color: green; } /* links INSIDE an li */
h2, h3 { font-family: Georgia, serif; } /* several selectors at once */
a:hover { color: crimson; } /* when the mouse is over the link */
</style>
<h2 id="title">Selectors</h2>
<p>An ordinary paragraph.</p>
<p class="important">A paragraph with the class "important".</p>
<ul><li><a href="#">a link in a list</a></li></ul>
<a href="#">a link outside the list (hover over it)</a>
Result:
| Selector | Selects | Note |
p | all elements of that type | |
.name | all elements with class="name" | a class is used on several elements; an element can have several classes: class="card important" |
#name | the element with id="name" | an id is unique on the page |
A B | a B that is anywhere inside A | descendant |
A > B | a B that is a direct child of A | |
:hover, :first-child, :nth-child(2n) | a state or a position | pseudo-classes |
Classes are used most often for styling – they are flexible and easy to reuse.
Colours and units
| Colour notation | Example |
| a name | red, navy, white |
| hexadecimal #RRGGBB | #2b7bb9 – two hex digits each for red, green and blue (00–ff) |
| rgb() / rgba() | rgb(43, 123, 185), rgba(0, 0, 0, 0.5) – the last number is the opacity |
| hsl() | hsl(205, 62%, 45%) – hue, saturation, lightness |
| Unit | Meaning |
px | pixel – a fixed size |
% | a percentage of the parent's size |
rem | relative to the base font size (usually 16px): 1.5rem = 24px – recommended for text |
em | relative to the parent's font size |
vw, vh | a percentage of the window's width / height |
A link with programming: in hexadecimal notation ff = 255, so #ff0000 is the same as rgb(255, 0, 0). The contrast between text and background must be high enough for the text to be easy to read.
Fonts and text
<style>
body { font-family: "Segoe UI", Arial, sans-serif; line-height: 1.6; }
h1 { font-size: 2rem; text-align: center; color: #1f5f91; }
.subtitle { font-style: italic; text-align: center; color: #666; }
.notice { font-weight: bold; text-transform: uppercase; letter-spacing: 2px; font-size: 0.8rem; }
a { text-decoration: none; border-bottom: 2px solid #2b7bb9; }
</style>
<h1>Computer club</h1>
<p class="subtitle">Every Tuesday at 2 pm</p>
<p class="notice">Sign-up is open</p>
<p>More on the <a href="#">club's page</a>.</p>
Result:
| Property | Example values |
font-family | a list of fonts, the last one is generic: Arial, sans-serif |
font-size | 16px, 1.2rem |
font-weight / font-style | bold, 400, 700 / italic |
text-align | left, center, right, justify |
line-height | the space between lines, e.g. 1.6 |
text-decoration | none (no underline), underline |
Special fonts can be included for free from Google Fonts – the site gives a ready-made <link> for the head.
The cascade: which rule wins
When several rules apply to the same element, the browser chooses in this order:
- Specificity – the more precise selector wins:
style="" > #id > .class > element.
- Order – with the same specificity, the rule written later wins.
- Inheritance – some properties (colour, font) pass from the parent to the children; that is why it is enough to set the font for the
body.
<style>
p { color: gray; }
.blue { color: blue; }
p { color: green; } /* later than the first p rule → it beats it */
#special { color: red; } /* an id is more specific than a class */
</style>
<p>An ordinary paragraph – green (the later rule).</p>
<p class="blue">A paragraph with a class – blue (a class beats an element).</p>
<p class="blue" id="special">A paragraph with a class and an id – red.</p>
Result:
Tip: right-click any element on a page → Inspect (or F12) shows its HTML and CSS. You can change the values live – the change lasts until you refresh the page.