OCR H446, section 1.3.4 (a)
Web pages are built from three languages that each do a different job.
| Language | Job | Analogy |
|---|---|---|
| HTML (HyperText Markup Language) | The structure and content of the page | The skeleton |
| CSS (Cascading Style Sheets) | The appearance: colours, fonts, sizes, layout | The clothes |
| JavaScript | The behaviour: reacting to the user, changing the page | The muscles |
The browser downloads the files, and renders the page, running the JavaScript.
HTML
HTML uses tags to mark up content. Most come in pairs, an opening tag and a closing tag (<p> ... </p>). Tags can have attributes: name="value".
OCR expects you to recognise these tags (any others will be explained in the question):
| Tag | Purpose |
|---|---|
<html> |
The whole document |
<head> |
Information about the page (not shown in the main window) |
<title> |
The page's title (shown in the browser tab) |
<link> |
Links to a CSS file |
<body> |
The visible content |
<h1>, <h2>, <h3> |
Headings, from biggest to smaller |
<p> |
A paragraph |
<img> |
An image, with the attributes src (the file), alt (text description), height and width |
<a> |
A hyperlink, with the attribute href (where it goes) |
<div> |
A block that groups content, for styling or layout |
<ul>, <ol>, <li> |
An unordered (bullet) list, an ordered (numbered) list, and a list item |
<form> |
A form for user input |
<input> |
An input control: type="text" is a text box (with a name to identify it), and type="submit" is a submit button |
<script> |
JavaScript code |
A basic page
<html>
<head>
<title>My page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Welcome</h1>
<p>This is a <a href="more.html">link</a>.</p>
<img src="cat.png" alt="A cat" width="200" height="150">
<ul>
<li>First</li>
<li>Second</li>
</ul>
</body>
</html>
CSS
CSS describes how elements look. A rule has a selector (which elements) and properties with values in curly brackets.
h1 {
color: blue;
font-family: Arial;
font-size: 24px;
}
Three ways of applying CSS
- Inline, with the
styleattribute directly on an element:
<h1 style="color:blue;">Welcome</h1>
- Internal, in a
<style>block in the page. - External style sheet: a separate
.cssfile, linked with<link>. This is best, as one file styles many pages, so the whole site can be changed in one place.
Selectors
| Selector | Matches | Example |
|---|---|---|
| Element | All elements of that tag | h1 { color: blue; } |
Class (.) |
Elements with class="..." |
.infoBox { background-color: green; } |
ID (#) |
The one element with id="..." |
#menu { background-color: #A2441B; } |
<div class="infoBox">Reusable style</div>
<div id="menu">Only one of these</div>
Properties you should know
| Property | What it sets |
|---|---|
background-color |
The background colour |
color |
The text colour (as a named colour such as blue, or a hex code such as #A2441B) |
border-color, border-style, border-width |
The border |
font-family |
The typeface |
font-size |
The size of the text |
height, width |
The size of an element |
JavaScript
JavaScript is a programming language that runs in the browser. It can respond to events, read form input, change the page and make calculations. Its structures match the OCR pseudocode (selection, iteration, functions), with a different syntax.
let x = 3; // variable
let name = "Bob";
if (x > 2) { // selection
name = name + "!";
}
for (let i = 0; i < 3; i++) { // iteration
x = x + i;
}
function triple(n) { // function
return n * 3;
}
Output
OCR describes three ways for JavaScript to produce output:
// 1. Change the contents of an HTML element
chosenElement = document.getElementById("example");
chosenElement.innerHTML = "Hello World";
// 2. Write straight into the document
document.write("Hello World");
// 3. Show an alert box
alert("Hello World");
Input
Input is read from a form. You won't need to memorise the exact method, as the exam focuses on what you do with the input.
A complete example
A page with a form, a button and some JavaScript that shows a greeting:
<html>
<head>
<title>Greeting</title>
<style>
#result { color: green; font-size: 20px; }
</style>
</head>
<body>
<h1>Say hello</h1>
<form>
<input type="text" name="username" id="username">
<input type="button" value="Greet" onclick="greet()">
</form>
<p id="result"></p>
<script>
function greet() {
let n = document.getElementById("username").value;
document.getElementById("result").innerHTML = "Hello " + n;
}
</script>
</body>
</html>
- HTML gives the heading, form, button and an empty paragraph.
- CSS (in the
<style>block) makes the result green and 20px. - JavaScript reads what was typed, and puts a greeting into the paragraph (
innerHTML) when the button is clicked.
onclick and type="button" aren't on OCR's list; if a question uses them, it will explain them.
Worked example: CSS
Task: write a CSS rule that makes every <h2> have white text on a dark blue background, with a 2px solid border, and say which language does which job.
h2 {
color: white;
background-color: #00008B;
border-width: 2px;
border-style: solid;
border-color: white;
}
- HTML supplies the
<h2>element, which holds the text. It decides what is on the page. - CSS (the rule above) decides how it looks.
- JavaScript is not needed here, since nothing changes in response to the user. It would be used to, for example, change this colour when the user clicks the heading.
Worked example: JavaScript
Task: a shop's page has an element <p id="charge"></p>. Complete the function showCharge(total, express) so that it works out the delivery charge and shows it in that element. Delivery is free on orders over £50, and costs £3.99 otherwise. Express delivery (when express is true) adds £5.
function showCharge(total, express) {
let charge = 0;
if (total <= 50) {
charge = 3.99;
}
if (express == true) {
charge = charge + 5;
}
document.getElementById("charge").innerHTML = "Delivery: £" + charge;
}
chargestarts at 0, which covers free delivery.- The first
ifadds the standard charge only when the order is £50 or less. "Over £50" means more than 50, so an order of exactly £50 still pays. - The second
ifis separate, not anelse, because express can apply whatever the total. - The last line finds the element by its id and replaces its contents with the message.
| Call | Shows |
|---|---|
showCharge(30, false) |
Delivery: £3.99 |
showCharge(30, true) |
Delivery: £8.99 |
showCharge(60, false) |
Delivery: £0 |
showCharge(60, true) |
Delivery: £5 |
In the exam, small syntax slips (a missing semicolon, say) aren't penalised: the logic and the output to the right element are what earn the marks.
Exam tip: state the job of each: HTML = structure and content, CSS = appearance, JavaScript = behaviour. Know the selector types (element, .class, #id) and the properties in the list. Use an external style sheet to style many pages consistently.
Check yourself
What is the job of HTML, of CSS and of JavaScript?
HTML gives the structure and content of the page, CSS controls its appearance, and JavaScript adds behaviour such as responding to the user.
Write the HTML for a link to about.html with the text "About"
<a href="about.html">About</a>
What is the difference between a CSS class selector and an ID selector?
A class selector (starting with a full stop) applies to every element with that class. An ID selector (starting with #) applies to the one element with that ID.
Why is an external style sheet better than inline styles?
One style sheet can style many pages, so the look of the whole site can be changed in one place, and the HTML stays simpler.