Computer ScienceFoundation20 min read

Web Development with HTML, CSS and JavaScript

Structure, appearance and behaviour — three languages, three jobs

This topic appears in:

01

Three languages, and why they are separate

A web page is built from three technologies, and each does exactly one thing. Keeping them separate is not a style preference — it is what lets one stylesheet restyle a thousand pages, and what lets a page still be readable when scripting is switched off.

LanguageJobAnalogy
HTMLstructure and contentthe skeleton
CSSpresentation — colour, layout, spacingthe clothes
JavaScriptbehaviour — what happens when the user actsthe muscles

Switch to CSS and then JavaScript. CSS styles whole groups of nodes at once; JavaScript changes them after the page has loaded. Only JavaScript can respond to a click.

02

HTML: elements, tags and nesting

An HTML element is written with an opening tag, some content, and a closing tag: <p>Hello</p>. A few elements have no content and so no closing tag, such as <br> and <img>.

Elements sit inside other elements, forming a tree. The nesting must not cross over: <b><i>text</b></i> is wrong, because the <i> opened last and must therefore close first.

Attributes go inside the opening tag and give extra information — <img src="cat.jpg" alt="a cat">. The alt attribute is required on images: it is what a screen reader announces to a blind user and what appears if the image fails to load.

TagPurpose
<h1> … <h6>headings, h1 most important
<p>a paragraph
<a href="…">a hyperlink
<img src="…" alt="…">an image, with text for when it cannot be seen
<ul> <ol> <li>unordered list, ordered list, list item
<table> <tr> <td>table, row, cell
<div> <span>generic containers, block and inline
<!DOCTYPE html><html> <head> … title, links … </head><body> … visible content … </body> </html>head holds information about the page; body holds what the visitor actually sees
03

CSS: selecting and styling

A CSS rule has a selector saying which elements it applies to, and a block of declarations, each a property and a value. Styling can be written inline in an element, inside a <style> block in the head, or — best — in a separate .css file linked from every page.

The external file is the one the paper wants you to recommend. Change one line in it and every page on the site updates, the file is cached by the browser so it downloads once, and the HTML stays readable.

selector { property: value; }p { color: navy; font-size: 16px; }.warning { color: red; }/* class — reusable */#header { height: 80px; }/* id — one element only */a class starts with a dot and may be used many times; an id starts with a hash and must be unique on the page

Class or id?

A class may be applied to any number of elements — .warning on every alert box. An id must identify exactly one element on the page, such as #main-navigation. Using an id for something that appears twice is invalid, and exam questions test the distinction directly.

04

JavaScript: making the page respond

HTML and CSS produce a page that looks right but does nothing. JavaScript runs in the browser, after the page has loaded, and can change any part of the tree in response to what the user does.

The pattern is nearly always the same: find an element, attach a function to an event, and change something when that event fires.

Worked example

A page has <button id="go">Click me</button> and <p id="out"></p>. Write JavaScript that puts "Hello" into the paragraph when the button is clicked.

  1. Find the button: const btn = document.getElementById("go");The id is how JavaScript locates one specific element in the tree.
  2. Attach a handler: btn.addEventListener("click", function () { … });The function is stored, not run — it waits until a click actually happens.
  3. Inside it, change the paragraph: document.getElementById("out").textContent = "Hello";Setting textContent replaces what the element displays.
  4. Place the script at the end of the body, or use defer.A script that runs before the elements exist finds nothing — this is the single most common cause of "it does not work".

Locate the element by id, add a click listener, and set textContent inside the handler.

Client-side and server-side

JavaScript in a page runs on the client — the visitor's own browser — so it is fast and needs no round trip, but the user can read and alter it. Anything that must be trusted, such as checking a password or a price, has to be done on the server. Client-side validation is for the user's convenience; it is never security.

05

Making a page usable by everyone

Two ideas from the syllabus decide whether a page works for real visitors.

Accessibility means the page can be used by people with impairments: alt text on every image, sufficient colour contrast, headings used in order rather than chosen for their size, and every function reachable from the keyboard.

Responsive design means the page adapts to the screen it is on. Most web traffic in Pakistan is from phones, so a layout that only works at desktop width fails most of its audience. Relative units, flexible layouts and media queries are how it is done.

Before you leave this chapter

  1. HTML = structure, CSS = presentation, JavaScript = behaviour.
  2. Tags must nest without crossing; the last opened is the first closed.
  3. An external stylesheet styles a whole site from one file and is cached once.
  4. A class can repeat; an id must be unique on the page.
  5. Client-side validation helps the user; only server-side checking is secure.
06

Getting a page from a server to a browser

Typing an address does more than most students realise, and the sequence is examinable. The browser looks up the domain name through DNS to find the server's IP address, opens a connection, and sends an HTTP request. The server replies with an HTTP response containing the HTML, and the browser then requests each stylesheet, image and script the page refers to.

The browser parses the HTML into the tree, applies the CSS, and runs the JavaScript. Only then does anything appear — which is why a page with twenty large images feels slow even on a fast connection: each one is a separate request.

  • HTTP is the protocol carrying requests and responses. HTTPS is the same thing encrypted, so nobody between you and the server can read or alter it.
  • DNS translates a human-readable name into the numeric address the network actually routes to.
  • Status codes report the outcome: 200 means success, 404 means the page does not exist, 500 means the server itself failed.
  • Caching stores files locally so a returning visitor downloads them once rather than every time.

Why HTTPS matters on every page, not just login pages

Without encryption, anyone on the same Wi-Fi can read the traffic and — worse — modify it in transit, injecting content into a page the visitor trusts. That is why browsers now mark plain HTTP sites as "not secure" regardless of whether they ask for a password. Any site handling user input at all should use HTTPS.

Practice questions

6 questions · 20 marks · full working on every one

Try each one on paper first, then open the working. The marks are shown where they are actually awarded, because that is where they are actually lost.

Short questions

3 · 6 marks

Two marks each, in the style of the short-question section of the paper. Answer in two or three lines.

SQ1[2 marks]
State the purpose of HTML, CSS and JavaScript in a web page.
Model answer

HTML defines the structure and content. CSS controls the presentation — colour, layout and spacing. JavaScript adds behaviour, responding to user actions after the page has loaded.

Examiner tip. One clause each. The word "behaviour" for JavaScript is what the mark scheme looks for, rather than "makes it interactive".

SQ2[2 marks]
Why should the alt attribute be included on every image?
Model answer

It provides a text description that a screen reader announces to a visually impaired user, and it is displayed if the image fails to load. It also gives search engines something to index.

Examiner tip. Accessibility is the primary reason and should come first. The fallback and SEO benefits are worth adding as the second mark.

SQ3[2 marks]
Differentiate between a CSS class and an id.
Model answer

A class is written with a leading dot and may be applied to many elements on a page. An id is written with a leading hash and must identify exactly one element.

Examiner tip. The symbols and the one-versus-many rule are both credited. Mentioning only the symbols usually scores one.

Solved numericals

2 · 8 marks

Full working, one step per line, with the marks shown where they are awarded.

N1[4 marks]
Give two advantages of using an external stylesheet rather than inline styles, and state how it is linked to a page.
Full working
  1. One change in the stylesheet updates every page that links to it, instead of editing each page individuallyconsistency and maintenance[1]
  2. The browser caches the file, so it is downloaded once rather than repeated inside every page — pages load faster[1]
  3. The HTML stays shorter and easier to read, with structure and presentation kept separateany third valid advantage accepted for this mark[1]
  4. Linked with <link rel="stylesheet" href="style.css"> inside the <head>the tag must be in the head[1]

Site-wide changes from one file, caching, and cleaner HTML; linked with a tag in the head.

Examiner tip. The link tag goes in the head, not the body. Placing it in the body is a common slip and the mark scheme checks for it.

N2[4 marks]
Write the HTML for an unordered list of three subjects, where the first item links to physics.html.
Full working
  1. Opens with <ul> and closes with </ul>ul for unordered; ol would be numbered[1]
  2. Three <li> … </li> items, each properly closed[1]
  3. First item contains <a href="physics.html">Physics</a>the href attribute is required[1]
  4. The anchor is nested inside the li, not wrapped around itcorrect nesting[1]

<ul><li><a href="physics.html">Physics</a></li><li>Maths</li><li>Computer</li></ul>

Examiner tip. Write the closing tags as soon as you write the opening ones, then fill in the middle. Unclosed tags are the commonest reason this question loses marks.

Long questions

1 · 6 marks

Theory and numerical together, as they appear in the long-question section.

LQ1[6 marks]
A student builds a school website. It works on their laptop but users complain that it is unusable on a phone, and that a form accepts invalid email addresses.
  1. Explain what responsive design is and why it matters here.
  2. Explain how JavaScript could check the email field before the form is submitted.
  3. Explain why that check alone is not enough.
Mark scheme
  1. Responsive design means the layout adapts to the size of the screen it is being viewed on[1]
  2. Achieved with relative units, flexible layouts and media queries rather than fixed pixel widths[1]
  3. Most visitors browse on phones, so a fixed desktop layout fails the majority of the audiencethe reason must connect to the users[1]
  4. JavaScript can listen for the form's submit event, read the value of the email field and test its format[1]
  5. If it is invalid, the script cancels the submission and displays a message — with no trip to the server, so the response is immediate[1]
  6. Client-side validation can be bypassed: a user can disable JavaScript or send the request directly, so the data must also be validated on the serverthe security point is the mark[1]

(a) layout adapts to screen size, and most visitors are on phones (b) listen for submit, test the value, cancel if invalid (c) client-side checks can be bypassed, so the server must validate too

Examiner tip. Part (c) is the understanding mark and the one most often left blank. Client-side validation is a convenience for the honest user; it stops nobody who is trying.