Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 6 of 14 25 min

Start styling. Learn how a CSS rule is built, the three ways to add CSS to a page, how to target elements by type, class and id, and how to set colours, fonts, sizes and backgrounds.

By the end of today you can

  • Read and write a CSS rule with a selector, properties and values
  • Add CSS with a style element, a linked stylesheet or a style attribute
  • Select elements by type, class and id
  • Set colours, backgrounds, fonts and text sizes
  • Write CSS comments and group selectors

Your pages have structure. Now let's make them look good. CSS describes how HTML elements should be displayed: colours, fonts, spacing, layout. The same HTML can look completely different with different CSS.

Mind map · click a dot to fold, drag to pan

Your first CSS

index.html · edit it, the preview updates
Page preview

Try color: tomato; or font-size: 30px;.

Sketch
The parts of a CSS rule: h1 { color: teal; font-size: 32px; }. The selector h1 picks which elements to style. Curly braces hold the declarations. Each declaration is a property, such as color, a colon, a value, such as teal, and a semicolon. A rule can have many declarations.

A rule has:

  • a selector: which elements (h1);
  • declarations inside { }: what to change, each as property: value;.

Forget a semicolon or a brace and the rule (or the next one) may be ignored, with no error message, so check your punctuation when something doesn't work.

Three ways to add CSS

<!-- 1. A linked stylesheet: the normal way for real sites -->
<head>
  <link rel="stylesheet" href="style.css">
</head>
 
<!-- 2. A <style> element in the page: fine for one page (and for these lessons) -->
<style>
  p { color: navy; }
</style>
 
<!-- 3. A style attribute on one element: avoid, except for quick tests -->
<p style="color: red;">Just this paragraph</p>

With a linked stylesheet, every page shares one style.css: change it once and the whole site updates. In these lessons we'll use <style> so everything fits in one editor.

Selectors: type, class and id

index.html · edit it, the preview updates
Page preview
SelectorMatchesExample
pEvery element of that typep { }
.noteEvery element with that class<p class="note">
#introThe element with that id<p id="intro">
h1, h2Several selectors (a group)
*Every element* { }

An element can have several classes (class="note warning") and gets the styles of each. Classes are the workhorse of CSS: reusable and flexible. Use ids sparingly.

/* … */ is a CSS comment.

Colours and backgrounds

index.html · edit it, the preview updates
Page preview

Colours can be names (teal, tomato), hex (#0f766e), rgb() or hsl(). Day 9 covers them properly.

Fonts and text

index.html · edit it, the preview updates
Page preview
  • font-family lists fonts in order of preference; the browser uses the first one it has. End with a generic family: serif, sans-serif, monospace or system-ui (the device's own font).
  • font-size, font-weight (400 normal, 700 bold), font-style (italic).
  • text-align: left, center, right.
  • text-decoration: underline or none, often for links.

Recap

  • A rule = selector { property: value; }.
  • Add CSS with a linked .css file (best), a <style> element, or a style attribute.
  • Select by type (p), class (.note), id (#intro); group with commas.
  • Colours, backgrounds and fonts: color, background-color, font-family, font-size, font-weight, text-align.

Tomorrow: more powerful selectors, and what happens when two rules disagree.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

    In p { color: teal; }, what is "color"?

  2. 2

    How do you select every element with class="card"?

  3. 3

    What's the usual way to add CSS to a real website with many pages?

  4. 4

    What does h1, h2 { color: navy; } do?

  5. 5

    Which is NOT a valid way to write a colour in CSS?

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going