Skip to content
SPM Tuition
Computer Science · Web and project development

Separating structure, presentation and behaviour

Your page works, but colours, text and click actions are tangled together and one change breaks another.

A web page has three jobs: holding content, looking right, and responding to the user. Structure is what is on the page, presentation is how it looks, and behaviour is what it does.

This lesson is part of web and project development. Keeping the three jobs apart makes each one easier to change, test and explain.

Which layer does what?

Layer Language Question it answers Example
Structure HTML What is on the page? A heading, a button, a paragraph
Presentation CSS How does it look? Text colour, spacing, font size
Behaviour JavaScript What happens when the user acts? A click changes the status text

A quick test: if you removed this line, would the content disappear, the look change, or the response stop? The answer names the layer.

Worked example: a homework reminder card

An original small page shows a homework task and a button to mark it done.

Structure (HTML)

<h2>Homework reminder</h2>
<p id="status">Pending</p>
<button id="done">Mark done</button>

Presentation (CSS)

#status { color: orange; }
.finished { color: green; }

Behaviour (JavaScript)

document.getElementById("done").addEventListener("click", function () {
  var s = document.getElementById("status");
  s.textContent = "Completed";
  s.classList.add("finished");
});

Trace one click. JavaScript finds the paragraph, changes its text to “Completed”, and adds the class finished. CSS then turns the colour green because the class now matches a rule.

Notice that JavaScript does not set a colour itself. It only switches a label, and the colour stays in CSS.

The mistake: mixing the layers

A typical tangled version looks like this.

<p id="status" style="color:orange">Pending</p>
<button onclick="document.getElementById('status').innerHTML='<font color=green>Completed</font>'">Mark done</button>

It works once, but the colour appears in two places inside the HTML, and the button carries behaviour and styling in one line. Changing the green to blue means editing the button code.

Change wanted Tangled version Separated version
Change finished colour Edit inside the button attribute Edit one CSS rule
Change the wording Edit inside JavaScript text Edit HTML or one JS string
Find a bug in the colour Search the whole page Open the CSS

How do I decide where a change belongs?

Ask three questions in order.

  1. Is it content? Put it in HTML.
  2. Is it appearance? Put it in CSS.
  3. Is it a reaction to the user? Put it in JavaScript.

When behaviour needs a new look, let JavaScript switch a class name and let CSS define what the class looks like. That link keeps both layers clean.

Check yourself

Sort these into structure, presentation or behaviour: (a) a level 2 heading reading “Class notice”, (b) the heading turns red after 5 seconds, (c) the notice box has a blue border.

Answer

(a) Structure, because it is content that belongs in HTML. (b) Behaviour, because the page reacts over time, and the red colour itself would be defined in CSS as a class that JavaScript adds. (c) Presentation, because a border is appearance and belongs in CSS.

Item (b) is the tricky one. It involves two layers: JavaScript decides when, and CSS decides how it looks.

What to study next

Plan a whole project with these layers in planning an original assessed project, then test the behaviour layer in testing interaction and data validation.

For help tracing your own page, see online one-to-one Computer Science tuition.

Common questions

What is the difference between HTML, CSS and JavaScript?

HTML describes the content and its structure, such as headings, buttons and paragraphs. CSS controls how that content looks. JavaScript controls what happens when the user does something. One page usually uses all three together.

Why not put everything in one file?

You can for a very small page, but changes become harder. If colours sit inside HTML tags, you must hunt through the whole page to change one colour. Separate layers let you change one thing in one place.

Is a button's text structure or presentation?

The text is content, so it belongs to the structure in HTML. The font size and colour of that text belong to presentation in CSS. Changing the text when the button is clicked is behaviour in JavaScript.

Which layer should I check first when something is wrong?

Ask what kind of wrong it is. Wrong or missing content points to HTML, wrong appearance points to CSS, and wrong response to a click or input points to JavaScript.

If you are unsure which layer a bug belongs to, one-to-one Computer Science lessons let a teacher ask you to locate it on your own page before anything is changed.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.