Styling pages with CSS

Foundation

CSS form: selector { property: value; }. Tag p{}, class .highlight{}, ID #title{}. The box model inside out: content → padding → border → margin. Responsive via @media (max-width: 600px) to restyle on phone screens.

For:Secondary 3 Information & Communication Technology · Unit:Web design · Lesson 9 of 13 in the learning path

After this lesson, you can:
  • CSS styles the look, HTML carries the content; one external .css file restyles the whole site.
  • The form: selector { property: value; }; every declaration ends with a semicolon.
  • Three selectors: tag p, class .highlight (reusable), ID #title (unique per page).
文體:編程教學課有聲朗讀

文體小知識:編程教學課每個概念都配一段可以親手試的代碼例子,照住輸入、改一改,最快學會。

資訊及通訊科技Information & Communication Technology

§Text Passage

1HTML carries the content; CSS (Cascading Style Sheets) carries the look — colour, fonts, spacing and layout are all its business. Three ways to attach it: the style attribute on a tag (inline), a <style> block on the page (internal), or a separate .css file pulled in with <link> (external) — external wins, because dozens of pages share one stylesheet and one edit restyles the whole site.

2The basic CSS form is 'selector { property: value; }'. Three everyday selectors: the tag selector p { } hits every paragraph; the class selector .highlight { } hits elements marked class="highlight"; the ID selector #title { } hits the single element with id="title". For example p { color: #23634f; font-size: 18px; } turns every paragraph dark green at 18 pixels.

3Every element is really a box — the Box Model: innermost the content, then padding (space between content and border), the border itself, then margin (space to other elements). For example .card { padding: 16px; border: 2px solid black; margin: 10px; } — roomy inside, framed, and gaps between boxes.

4Colour takes names (red) or hex codes (#f07f2e orange); fonts use font-family, size font-size, line spacing line-height. There is also the basic responsive idea: @media (max-width: 600px) { body { font-size: 14px; } } means 'when the screen is narrower than 600 pixels (a phone), drop the font to 14px' — one page reads well on desktop and mobile alike.

1Summary 重點摘要

CSS form: selector { property: value; }. Tag p{}, class .highlight{}, ID #title{}. The box model inside out: content → padding → border → margin. Responsive via @media (max-width: 600px) to restyle on phone screens.

2Vocabulary cards Vocabulary

C
CSS層疊樣式表

The language describing how pages look — colour, fonts, spacing and layout.

S
Selector選擇器

The opening part naming which elements a style hits, e.g. p, .highlight, #title.

C
Class selector類別選擇器

Starts with a dot (.highlight) and hits every element carrying that class — reusable anywhere.

I
ID selectorID 選擇器

Starts with a hash (#title) and hits the single element with that id on the page.

P
Property and value屬性與值

The two halves of a declaration: the property names what changes (color), the value what it becomes (red), ending with a semicolon.

B
Box model盒子模型

Every element is a box: content, padding, border, margin — four layers inside out.

P
Padding / Margin內邊距/外邊距

padding is the space inside, between content and border; margin is the space outside, to other elements.

H
Hex colour十六進制顏色碼

A six-digit colour code after #, e.g. #f07f2e orange, #ffffff white.

M
Media query媒體查詢

@media (max-width: 600px) { ... } applies styles only under a screen width — the basis of responsive design.

3Step by step Steps

  1. Your first CSS and the basic formatCSS handles looks while HTML handles content. First example: inside a <style> block write p { color: red; } and every paragraph on the page turns red at once. The basic shape is selector { property: value; } — p is the selector naming the targets, color: red is one style rule, and the trailing semicolon is mandatory, separating rules when several sit together; adding font-size: 18px; restyles the size too. This one-place, whole-page effect is the root of CSS's efficiency.
  2. Three kinds of selectorThree everyday selectors: the tag selector p { } acts on every paragraph; the class selector .highlight { } acts on elements marked class='highlight' — one class may sit on many elements at once, ideal for repeated styling such as highlighted paragraphs; the ID selector #title { } acts on the single element with id='title', unique per page, naming the main heading. The choosing principle: tag for everything, class for the shared few, ID for the one — keep the layers straight and confusion stays away.
  3. The box modelEvery element is a box with four layers from inside out: the content (text and images), padding between content and border, the border itself, and margin between the element and its neighbours. Example: .card { padding: 16px; border: 2px solid black; margin: 10px; } — the text sits 16px clear of the frame, the frame is a 2px solid black line, and the card keeps 10px away from surrounding elements. Any layer, once changed, shows instantly; fine-tuning a layout is really the sizing of these four layers.
  4. Fonts, line spacing and colourTypography shapes the reading experience: body { font-family: Arial, sans-serif; font-size: 16px; line-height: 1.8; } sets the site-wide typeface, 16-pixel size and 1.8 line spacing in one go, making long text instantly readable — 1.5 to 1.8 is the accepted comfort range. Colour comes in two notations: color: red and color: #ff0000 look identical; the three hex pairs control the amounts of red, green and blue — #000000 is black, #ffffff white, and #f07f2e is orange. A site should keep one palette, starting from a single theme colour and extending it.
  5. An introduction to responsive designResponsive design keeps one page looking good on both phone and desktop. The entry tool is the media query: @media (max-width: 600px) { body { font-size: 14px; } } means 'when the screen is narrower than 600 pixels (a phone), drop the font to 14px', while desktops keep 16px. One line of code and both devices are comfortable; further steps narrow the margins or hide sidebars on the phone version. With more than half of web traffic now on phones, responsiveness is a baseline requirement, not a bonus.
  6. Three ways to attach CSSCSS joins HTML in three ways: inline styles sit in a tag's style attribute, touching that one element — highest priority but hard to reuse; internal styles live inside a <style> block and style the whole page; external stylesheets are separate .css files pulled in with <link rel='stylesheet' href='css/style.css'>, shared by dozens of pages so one edit restyles the entire site. Formal projects always use external stylesheets, keeping inline styles for temporary tweaks — the industry-standard arrangement.
  7. Practical work: a card styleA combined exercise: design a 'card' for an intro paragraph. The style: .card { background-color: #ffffff; border: 2px solid #000000; border-radius: 14px; padding: 16px; margin: 10px; } — white background, black frame, 14px rounded corners, 16px padding. On the HTML side, mark the paragraph with class='card': <p class='card'>This text sits inside a card.</p>. Once applied, the page shows a rounded white card with comfortable space between text and frame. Define styles as classes, attach them in HTML — the daily rhythm of CSS work.

4Diagrams Diagrams

CSS:網頁嘅化妝師 Stylingh1 {color: teal; font-size: 40px;}選擇器 selectorh1 → 揀邊個元素屬性 propertycolor → 改乜特徵值 valueteal → 改成乜樣一句 CSS =「揀邊個 → 改乜 → 改成點」三段式HTML 係素顏骨架,CSS 幫佢著衫化妝——同一個 HTML 可以百變造型
Fig. 1: CSS styling

5Worked example Example

Example: design a 'card' style for an intro paragraph — white background, black border, rounded corners, roomy padding — and apply it in HTML.

  1. Write the selector: .card { } — a class, because several paragraphs will share the same card look.
  2. Set background and border: background-color: #ffffff; border: 2px solid #000000; — white with a 2px solid black frame.
  3. Round the corners and pad: border-radius: 14px; padding: 16px; — smooth corners and 16px between text and frame.
  4. Apply in HTML: <p class="card">This text sits inside a card.</p> — the page now shows a white, black-framed, rounded card.
  5. Answer: .card { background-color: #ffffff; border: 2px solid #000000; border-radius: 14px; padding: 16px; } used with <p class="card">...</p>.

6Common mistakes Mistakes

✗ Dropping the semicolon in p { color: red } and hoping.
✓ One line may survive, but with several lines a missing semicolon kills the next declaration; end every property: value pair with one.
✗ Writing the class selector as highlight { }.
✓ Without the dot, highlight reads as a tag selector and matches nothing; classes must start with a dot: .highlight.
✗ Treating padding and margin as the same 'space'.
✓ padding is inside the border (pushes content from the frame); margin is outside (pushes other elements away) — the visual results differ.
✗ Mixing up color and background-color.
✓ color paints the text itself; background-color paints behind it — swap them wrongly and text melts into the background.
✗ Hard-coding styles inline on every page for convenience.
✓ Inline styles repeat across pages and resist change: one colour edit means hunting tag by tag; an external .css file changes once and restyles the site.

7Quick check Quick check

Q1In .highlight { background-color: yellow; }, what is .highlight?

Q2What is the box model's order from the inside out?

Q3Which property controls the space between text and the border? A. margin B. padding C. border

Q4What colours are #ffffff and #000000?

Q5What does @media (max-width: 600px) { body { font-size: 14px; } } do?

8Recap Recap

  • CSS styles the look, HTML carries the content; one external .css file restyles the whole site.
  • The form: selector { property: value; }; every declaration ends with a semicolon.
  • Three selectors: tag p, class .highlight (reusable), ID #title (unique per page).
  • The box model: content → padding (inside) → border → margin (outside).
  • @media (max-width: 600px) keeps one page good on phone and desktop — responsive step one.

Other subjects in this grade

Same subject, other grades