Web Design & Development · JavaScript

WDD13 - JavaScript: changing page layout

Tue 8 Dec 2026 · P2 (single)
Content scoped for a single period · matches available time
Learning intentions
Success criteria
Warm up - show/hide and toggle recap

WDD13 generalises WDD12's single-property style changes (display) to several properties at once.

WU1
Which value should element.style.display be set to, to hide an element completely?
 
WU2
Which JavaScript statement is used to check a condition, such as an element's current display value, before deciding what to do next?
 
WU3
Before any JavaScript runs, what is the value of element.style.display for an element with no inline style and no matching CSS rule setting display?
  • It is not automatically 'block'.
  • It is not 'none' unless that has actually been set.
  • Correct - nothing has set it directly yet.
  • Reading it does not cause an error.

Key vocabulary

camelCase
A naming style where each word after the first starts with a capital letter and there are no hyphens, e.g. backgroundColor.
Hyphen-case (CSS)
CSS's own naming style for multi-word properties, using hyphens, e.g. background-color, margin-left.
element.style.X
The general pattern for reading or changing any single CSS property of an element from JavaScript.
Layout-changing function
A function that combines several style property changes together to visibly rearrange an element on the page.

Combining several style changes in one function

One function, several properties

WDD12 used element.style.display to show and hide a single element by changing one property. The same element.style.X pattern works for any CSS property, and a single function is not limited to changing only one of them. A function that genuinely rearranges a page's layout - moving an element, resizing it, and recolouring it all at once - typically needs several element.style.X assignments inside the same function, one line per property.

function changeLeft() {
  var box = document.getElementById('box');
  box.style.float = 'left';
  box.style.width = '120px';
  box.style.background = '#E1F5EE';
}

camelCase: JavaScript's naming rule for hyphenated CSS properties

Many CSS properties are two or more words joined by a hyphen: background-color, margin-left, font-size. JavaScript identifiers cannot contain a hyphen - a hyphen is the subtraction operator - so every hyphenated CSS property becomes a single JavaScript word in camelCase: the hyphen is removed, and the letter that followed it becomes a capital. background-color becomes backgroundColor; margin-left becomes marginLeft; font-size becomes fontSize. Single-word CSS properties such as width, height, float or color need no conversion at all, since there is no hyphen to remove.

background-color

backgroundColor

margin-left

marginLeft

font-size

fontSize

width

width (no change - single word)

Same property, written two ways setting the background colour
element.style.background-color = 'red'; JavaScript error
element.style.backgroundColor = 'red'; Works correctly

Worked examples

Example 1 - Converting CSS property names to camelCase
/* CSS */              /* JavaScript */
background-color   →   backgroundColor
margin-left         →   marginLeft
font-size            →   fontSize
1
Find the hyphen in the CSS property name.
2
Remove the hyphen and capitalise the letter that came straight after it.
background-color becomes backgroundColor - one JavaScript word, no hyphen, correctly capitalised.
Example 2 - changeLeft(), changeRight() and changeBack()
function changeLeft() {
  var box = document.getElementById('box');
  box.style.float = 'left';
  box.style.width = '120px';
}

function changeRight() {
  var box = document.getElementById('box');
  box.style.float = 'right';
  box.style.width = '200px';
}

function changeBack() {
  var box = document.getElementById('box');
  box.style.float = 'none';
  box.style.width = '300px';
}
1
changeLeft() and changeRight() each combine two style properties (float and width) to move and resize the box differently.
2
changeBack() restores both properties to the box's original values.
Three buttons, each calling one of these functions from onclick, let a pupil rearrange and then restore the layout on demand.
Try the three buttons above - I move and resize.
Example 3 - What goes wrong without camelCase
box.style.background-color = 'red';

JavaScript reads this as background minus color - an invalid, meaningless expression. This causes a JavaScript error, and nothing on the page changes.

box.style.backgroundColor = 'red';

A single valid JavaScript property name. The background colour changes exactly as intended.

Always convert hyphenated CSS property names to camelCase before using them with element.style.
Now you try

Write the correct JavaScript property name for the CSS property border-color.

Remove the hyphen between "border" and "color", and capitalise the letter that followed it: c → C.

border-color becomes borderColor.

Practical: changeLeft/changeRight/changeBack, WebStorm

Download the starter file below - three buttons with empty onclick attributes, and a box to rearrange.

Download WDD13_starter.html
  1. Add a <script> block and write changeLeft(), changeRight() and changeBack(), each combining at least two element.style.X assignments.
  2. Wire each button's onclick attribute to the matching function.
  3. Double-check every property name is correctly converted to camelCase.
  4. Save, open in a browser, and click all three buttons to check the box moves, resizes, and can always be restored to its original layout.
Common mistakes
Exam tip

If a question shows JavaScript code setting a style property and asks whether it will work, check first for a hyphen in the property name - a hyphen there is invalid JavaScript and the correct camelCase form should be given instead. Remember that a single function is allowed to contain more than one element.style.X line; combining several is exactly how a genuine layout change is built.

Task Set A - Core questions

Task Set A - Core questions
Complete all questions. Written and code answers reveal a model answer for self-assessment.
A1
What is the correct JavaScript camelCase property name for the CSS property background-color?
 
A2
What is the correct JavaScript camelCase property name for the CSS property margin-left?
 
A3
What is the correct JavaScript camelCase property name for the CSS property font-size?
 
A4
What happens if a pupil writes box.style.background-color = 'red'; in JavaScript?
  • The hyphen makes this invalid JavaScript.
  • Correct. background-color is read as "background minus color".
  • This is not what an invalid property expression does - it causes an error.
  • These are not equivalent - one is valid JavaScript, the other is not.
A5
Does the single-word CSS property "width" need any conversion to be used as element.style.width in JavaScript? Answer yes or no.
 
A6
A function needs to float a box left and also make it narrower, both at once. How many element.style.X assignments does this function need, at minimum?
  • Nothing happens without an explicit style assignment for each property.
  • Each property needs its own assignment - one line sets one property.
  • Correct.
  • Multiple style assignments can sit inside the same function.
A7
Explain why a changeBack() function is needed after changeLeft() and changeRight() have been used, and what would happen without one.
Model answer
A8
What is the correct JavaScript camelCase property name for the CSS property border-color?
 
A9
This function is meant to move a box to the right and shrink it, but running it causes a JavaScript error and nothing changes. Find the bug and write the corrected function.
function changeRight() {
  var box = document.getElementById('box');
  box.style.float = 'right';
  box.style.background-color = '#E1F5EE';
}
function changeRight() {
  var box = document.getElementById('box');
  box.style.float = 'right';
  box.style.backgroundColor = '#E1F5EE';
}

background-color is not valid JavaScript - the hyphen is read as subtraction. Converting it to camelCase, backgroundColor, fixes the error.

Task Set B - Extension

Task Set B - Extension
Beyond the core specification - written and practical answers are self-assessed against a model answer.
B1
Compare WDD12's toggle function approach with WDD13's changeLeft/changeRight/changeBack approach. What is the key structural difference?
Model answer
B2
Explain, in terms of how JavaScript reads code, exactly why element.style.background-color is invalid, rather than simply being an alternative way of writing backgroundColor.
Model answer
B3 - Practical (WebStorm, extending the main practical)
Add a fourth function to your changeLeft/changeRight/changeBack file that combines at least three style properties at once.
  1. In your file from the main practical, add a fourth button and a function such as changeHighlight().
  2. Inside it, set at least three element.style.X properties together - for example float, width and a border colour, all in the same function.
  3. Make sure every property name is correctly converted to camelCase where needed.
  4. Save, open in a browser, and check the new button applies all three changes at once, and that changeBack() still correctly restores the original layout afterwards.
function changeHighlight() {
  var box = document.getElementById('box');
  box.style.float = 'left';
  box.style.width = '150px';
  box.style.borderColor = '#D85A30';
}

Compare your own file against this rather than copying it - the exact properties and values do not matter, but at least three element.style.X lines should be present in a single function, and changeBack() should still work afterwards.

File this in OneNote under:
Higher Computing Science > Web Design & Development > WDD13

Teacher notes - Shift+T to toggle

Timing: content is scoped for a single period and lands on one (Tue 8 Dec 2026, per WDD.md §2a/§4 table) - an "OK" match, no compression or extra-time adjustment needed. Suggested pace: 6 min warm up, 12 min combining-properties + camelCase notes + Example 1, 12 min changeLeft/Right/Back notes + context box + Example 2 (including the live demo), 8 min Example 3 + common mistakes, remaining time on Task Set A, then Task Set B and the WebStorm practical.

Scope control: deliberately does not introduce arrays, loops, or storing multiple elements' worth of state - each function operates on one fixed element id, matching Chris's own L13 changeLeft()/changeRight()/changeBack() pattern and the WDD scope note excluding loops and arrays entirely.

The camelCase conversion is the highest-value fact in this lesson, matching the role played by margin collapse (WDD8), percentage height (WDD9) and the empty-string starting state (WDD12) - it is a small, easy-to-miss syntax rule with a very specific, testable failure mode (background-color parsed as subtraction). A1-A4/A8/A9/B2 all test it from different angles.

Starter file provided (WDD13_starter.html) rather than a from-scratch build, restoring variation after WDD11 and WDD12 were both built from scratch in a row.