Skip to main content

Web Development and Multimedia

This document provides in-depth coverage of web development (HTML, CSS, JavaScript), web hosting, client-side vs server-side processing, multimedia applications, and user interface design. A brief introduction to HTML, CSS, and JavaScript is available in internet-and-data-communications.md.


HTML -- Structure and Semantics

Document Structure

Every HTML document follows this fundamental structure:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Page description for search engines" />
<title>Page Title</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<!-- Page content here -->
<script src="script.js"></script>
</body>
</html>
ComponentPurpose
<!DOCTYPE html>Declares the document type as HTML5
<html lang="en">Root element; lang specifies the language for accessibility
<head>Metadata, links to external resources, not displayed
<meta charset>Character encoding (should be UTF-8)
<meta viewport>Responsive design settings for mobile devices
<title>Browser tab title and search engine result title
<link>Links to external CSS stylesheets
<body>Contains all visible page content
<script>Links to or embeds JavaScript code

Semantic HTML

Semantic elements clearly describe their meaning to both the browser and the developer. Using semantic HTML improves accessibility, SEO, and code maintainability.

Semantic ElementPurposeNon-Semantic Equivalent
<header>Introductory content or navigational links<div>
<nav>Navigation links<div>
<main>Main content of the page (only one per page)<div>
<article>Self-contained content (blog post, news article)<div>
<section>Thematic grouping of content<div>
<aside>Sidebar, related content<div>
<footer>Footer information (copyright, links)<div>
<figure>Self-contained content with optional caption<div>
<figcaption>Caption for a <figure><span> or ``
<time>Date/time<span>
<mark>Highlighted text<span>

HTML Forms

Forms collect user input and submit it to a server for processing.

<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required />

<label for="email">Email:</label>
<input type="email" id="email" name="email" required />

<label for="age">Age:</label>
<input type="number" id="age" name="age" min="1" max="120" />

<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday" />

<label>Gender:</label>
<input type="radio" id="male" name="gender" value="male" />
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female" />
<label for="female">Female</label>

<label for="city">City:</label>
<select id="city" name="city">
<option value="hong_kong">Hong Kong</option>
<option value="kowloon">Kowloon</option>
<option value="nt">New Territories</option>
</select>

<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></textarea>

<input type="checkbox" id="agree" name="agree" value="yes" />
<label for="agree">I agree to the terms</label>

<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Input TypePurposeValidation
textSingle-line textmaxlength, pattern, required
emailEmail addressBrowser validates email format
passwordMasked text (shows dots)minlength, maxlength
numberNumeric inputmin, max, step
dateDate pickermin, max
radioSingle selection from mutually exclusive optionsSame name groups them
checkboxMultiple selectionschecked attribute
selectDropdown list<option> elements
textareaMulti-line textrows, cols
fileFile uploadaccept for file types
hiddenHidden data not visible to userSent with form submission
submitSubmits the formTriggers action URL
resetResets all form fields to defaults

Form attributes:

AttributeDescription
actionURL where form data is sent
methodHTTP method: GET (data in URL) or POST (data in body)
enctypeHow form data is encoded (for file uploads: multipart/form-data)
nameName of the form field (used as key when submitted)
idUnique identifier (used by <label for> and JavaScript)
valueDefault value of the field
requiredField must be filled before submission
placeholderHint text shown inside the field before user types
autofocusField receives focus when page loads

HTML Tables

<table>
<caption>
Student Scores
</caption>
<thead>
<tr>
<th>Name</th>
<th>Maths</th>
<th>English</th>
<th>ICT</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>85</td>
<td>92</td>
<td>78</td>
</tr>
<tr>
<td>Bob</td>
<td>72</td>
<td>65</td>
<td>88</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Average</td>
<td>78.5</td>
<td>78.5</td>
<td>83</td>
</tr>
</tfoot>
</table>
ElementPurpose
<table>Container for the table
<caption>Table title/description
<thead>Table header section
<tbody>Table body (main data)
<tfoot>Table footer section
<tr>Table row
<th>Header cell (bold, centred by default)
<td>Data cell

CSS -- Styling and Layout

Selectors

CSS selectors determine which HTML elements are targeted by style rules.

Selector TypeSyntaxDescriptionSpecificity
Universal*Selects all elements0-0-0
ElementpAll `` elements0-0-1
Class.highlightElements with class="highlight"0-1-0
ID#headerElement with id="header"0-1-0-0
Descendantdiv p``inside any<div>Additive
Childdiv > p``that is a direct child of<div>Additive
Adjacent siblingh1 + p``immediately after<h1>Additive
Attribute[type="text"]Elements with matching attribute0-1-0
Pseudo-classa:hoverElement in a specific state0-1-0
Pseudo-elementp::first-lineSpecific part of an element0-0-1

Specificity ordering (low to high): Universal < Element < Class/Attribute/Pseudo-class < ID < Inline style < !important

CSS Properties -- Core Reference

Text and Font Properties

PropertyDescriptionExample
font-familySets the typefaceArial, sans-serif
font-sizeSets text size16px, 1.2em, 120%
font-weightSets text thicknessnormal, bold, 700
font-styleSets italic/obliquenormal, italic
text-alignHorizontal alignmentleft, center, right
text-decorationUnderline, overline, line-throughunderline, none
line-heightSpace between lines1.5, 24px
colorText colour#333333, rgb(51,51,51)
letter-spacingSpace between characters1px, 0.05em
text-transformCapitalisationuppercase, lowercase, capitalize

Box Model Properties

The CSS box model describes how every HTML element is rendered as a rectangular box.

+--------------------------------------------------+
| margin |
| +--------------------------------------------+ |
| | border | |
| | +--------------------------------------+ | |
| | | padding | | |
| | | +--------------------------------+ | | |
| | | | content | | | |
| | | +--------------------------------+ | | |
| | +--------------------------------------+ | |
| +--------------------------------------------+ |
+--------------------------------------------------+
PropertyDescription
width, heightDimensions of the content area
paddingSpace between content and border
borderBorder around the padding
marginSpace outside the border
box-sizing: border-boxIncludes padding and border in the width/height

Total element width (default content-box):

TotalWidth=marginleft+borderleft+paddingleft+width+paddingright+borderright+marginright\mathrm{Total Width} = \mathrm{margin-left} + \mathrm{border-left} + \mathrm{padding-left} + \mathrm{width} + \mathrm{padding-right} + \mathrm{border-right} + \mathrm{margin-right}

With box-sizing: border-box, the width property includes content, padding, and border:

TotalWidth=marginleft+width+marginright\mathrm{Total Width} = \mathrm{margin-left} + \mathrm{width} + \mathrm{margin-right}

Layout Properties

PropertyDescription
displayblock, inline, inline-block, flex, grid, none
positionstatic, relative, absolute, fixed, sticky
floatLeft/right floating (legacy layout method)
clearClear float (left, right, both)
overflowvisible, hidden, scroll, auto
z-indexStacking order for positioned elements

Display types:

Display ValueBehaviour
blockTakes full width; starts on new line (e.g., <div>, ``)
inlineTakes only content width; flows within text (e.g., <span>, <a>)
inline-blockInline flow but respects width/height
flexFlexbox container for flexible 1D layouts
gridCSS Grid container for 2D layouts
noneElement is hidden and removed from layout flow

Flexbox Layout

Flexbox is a one-dimensional layout system for arranging items in rows or columns.

.container {
display: flex;
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: center; /* main axis alignment */
align-items: center; /* cross axis alignment */
gap: 10px; /* space between items */
flex-wrap: wrap; /* allow wrapping */
}

.item {
flex: 1; /* grow equally */
flex-basis: 200px; /* ideal size */
}
PropertyValuesDescription
flex-directionrow, column, row-reverse, column-reverseDirection of main axis
justify-contentflex-start, center, flex-end, space-between, space-around, space-evenlyAlignment on main axis
align-itemsstretch, flex-start, center, flex-end, baselineAlignment on cross axis
flex-wrapnowrap, wrap, wrap-reverseWhether items wrap
gapLength valueSpace between flex items
flex-growNumberHow much the item grows
flex-shrinkNumberHow much the item shrinks
flex-basisLength valueInitial size before grow/shrink

CSS Grid Layout

CSS Grid is a two-dimensional layout system for creating complex page layouts.

.grid-container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto 1fr auto;
gap: 20px;
}

.header {
grid-column: 1 / -1;
} /* spans all columns */
.sidebar {
grid-column: 1;
}
.main {
grid-column: 2 / 4;
} /* spans columns 2 and 3 */
.footer {
grid-column: 1 / -1;
}
PropertyDescription
grid-template-columnsDefines column widths
grid-template-rowsDefines row heights
grid-columnColumn start and end positions
grid-rowRow start and end positions
gapSpace between rows and columns
grid-areaShorthand for row/column start/end

Responsive Design

Responsive design ensures a website displays well on all screen sizes (desktop, tablet, mobile).

Media queries apply CSS rules conditionally based on device characteristics.

/* Mobile first (default) */
.container {
width: 100%;
padding: 10px;
}

/* Tablet and above */
@media (min-width: 768px) {
.container {
width: 750px;
padding: 20px;
}
}

/* Desktop and above */
@media (min-width: 1024px) {
.container {
width: 960px;
margin: 0 auto;
}
}

Responsive units:

UnitDescription
pxAbsolute pixels
%Percentage of parent element
emRelative to the font-size of the element
remRelative to the font-size of the root element
vwPercentage of viewport width
vhPercentage of viewport height
vminPercentage of the smaller viewport dimension
vmaxPercentage of the larger viewport dimension

The Three Ways to Apply CSS

MethodSyntaxSpecificityReusability
Inline style<p style="color: red;">HighestNone
Internal style<style> block in <head>MediumPage-level
External style<link rel="stylesheet" href="style.css" />MediumSite-wide

External stylesheets are the recommended approach for maintainability, caching, and consistency.


JavaScript -- Programming for the Web

Variables

var oldStyle = 'function-scoped'; // Legacy, avoid in modern code
let counter = 0; // Block-scoped, reassignable
const PI = 3.14159; // Block-scoped, cannot be reassigned
KeywordScopeReassignableHoistedUse case
varFunctionYesYesLegacy code (avoid)
letBlockYesNoVariables that change
constBlockNoNoConstants, fixed references

Data Types

TypeDescriptionExamples
NumberAll numbers (integer and floating point)42, 3.14, -7, Infinity
StringText in single, double, or backtick quotes'hello', "world", `template`
BooleanLogical valuestrue, false
nullIntentional absence of valuenull
undefinedVariable declared but not assignedundefined
ObjectKey-value pairs{ name: "Alice", age: 20 }
ArrayOrdered collection[1, 2, 3, "four"]

Functions

// Function declaration
function add(a, b) {
return a + b;
}

// Function expression
var multiply = function (a, b) {
return a * b;
};

// Arrow function (ES6)
var divide = (a, b) => a / b;

// Function with default parameter
function greet(name, greeting) {
greeting = greeting || 'Hello';
return greeting + ', ' + name + '!';
}

Control Structures

// If-else
var score = 75;
if (score >= 90) {
grade = 'A';
} else if (score >= 70) {
grade = 'B';
} else {
grade = 'C';
}

// For loop
for (var i = 0; i < 10; i++) {
console.log(i);
}

// While loop
var count = 5;
while (count > 0) {
console.log(count);
count--;
}

DOM Manipulation

The Document Object Model (DOM) represents the HTML document as a tree of objects. JavaScript can traverse and modify this tree to change page content, structure, and style dynamically.

Selecting elements:

var element = document.getElementById('myId');
var elements = document.getElementsByClassName('myClass');
var elements = document.getElementsByTagName('p');
var element = document.querySelector('.myClass'); // first match
var elements = document.querySelectorAll('div.item'); // all matches

Modifying content and style:

// Change text content
document.getElementById('demo').textContent = 'New text';
document.getElementById('demo').innerHTML = '<strong>Bold text</strong>';

// Change style
document.getElementById('demo').style.color = 'red';
document.getElementById('demo').style.fontSize = '20px';

// Add/remove CSS classes
document.getElementById('demo').classList.add('highlight');
document.getElementById('demo').classList.remove('highlight');
document.getElementById('demo').classList.toggle('active');

// Create and append elements
var newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
document.getElementById('container').appendChild(newParagraph);

Event Handling

Events are actions or occurrences (click, hover, key press, form submission) that JavaScript can respond to.

// Using addEventListener (recommended)
document.getElementById('myButton').addEventListener('click', function () {
alert('Button clicked!');
});

// Event object
document.getElementById('myInput').addEventListener('keyup', function (event) {
console.log('Key pressed: ' + event.key);
});

// Form submission
document.getElementById('myForm').addEventListener('submit', function (event) {
event.preventDefault(); // prevent page reload
var name = document.getElementById('nameInput').value;
if (name === '') {
alert('Name is required!');
} else {
alert('Form submitted: ' + name);
}
});
Common EventsTrigger
clickMouse click on an element
dblclickDouble click
mouseoverMouse enters the element
mouseoutMouse leaves the element
keydownA key is pressed down
keyupA key is released
submitA form is submitted
changeForm field value changes
loadPage or resource finishes loading
focusElement receives focus
blurElement loses focus

Arrays

var scores = [85, 92, 78, 95, 88];

// Access
console.log(scores[0]); // 85
console.log(scores.length); // 5

// Modify
scores[1] = 96;

// Iterate
for (var i = 0; i < scores.length; i++) {
console.log(scores[i]);
}

// Common array methods
scores.push(100); // add to end
scores.pop(); // remove from end
scores.unshift(70); // add to beginning
scores.shift(); // remove from beginning
scores.splice(2, 1); // remove 1 element at index 2

// Searching
var index = scores.indexOf(95); // returns index or -1

// Sorting
scores.sort(function (a, b) {
return a - b;
}); // ascending numeric sort

Web Hosting and Domain Names

Web Hosting Types

Hosting TypeDescriptionCostPerformanceControlSuitable For
Shared hostingMultiple websites share one server's resourcesLowLow-MediumLimitedSmall sites, beginners
VPSVirtual Private Server -- partitioned dedicated resourcesMediumMediumHighGrowing sites, custom config
Dedicated serverEntire physical server for one clientHighHighFullHigh-traffic, enterprise
Cloud hostingResources distributed across multiple servers (scalable)VariableScalableMedium-HighVariable traffic, apps
Free hostingLimited resources, often with advertisementsFreeLowVery limitedPersonal projects, testing

Domain Name Registration Process

  1. Choose a domain name: Select a name that is memorable, relevant, and available.
  2. Check availability: Use a domain registrar's search tool to verify the name is not already registered.
  3. Choose a TLD: Select the appropriate top-level domain (.com, .org, .edu, .hk, etc.).
  4. Register with a registrar: Use an accredited registrar (e.g., GoDaddy, Namecheap, HKDNR for .hk domains).
  5. Provide registrant information: Name, contact details, administrative and technical contacts.
  6. Pay registration fee: Annual fee varies by TLD and registrar.
  7. Configure DNS records: Point the domain to your web hosting server by setting A records (for IPv4) or CNAME records at your DNS provider.
  8. Wait for propagation: DNS changes can take up to 48 hours to propagate globally (typically much less).

Uploading Files to a Web Server

MethodDescription
FTP/SFTPFile Transfer Protocol (FTP) or Secure FTP (SFTP) using a client like FileZilla
Web-based uploadControl panel provided by the hosting company (cPanel, Plesk)
SSH/SCPSecure copy via command line
Git deploymentPush to a repository that triggers automatic deployment (e.g., GitHub Pages, Netlify)
CI/CDContinuous Integration/Continuous Deployment pipelines

Client-Side vs Server-Side Processing

Comparison

AspectClient-SideServer-Side
Execution locationUser's browserWeb server
LanguagesHTML, CSS, JavaScriptPHP, Python, Ruby, Java, C#, Node.js
Response speedImmediate (no server round-trip)Requires network request/response cycle
Server loadNone (processing on user's device)Server handles all processing
SecurityCode is visible to the user (not secure)Code runs on server, hidden from user
Data accessCannot directly access databasesCan connect to databases and file systems
Offline capabilityCan work offline (if cached)Requires internet connection
Browser dependencyDepends on browser compatibilityIndependent of browser

When to Use Each

ScenarioClient-Side or Server-SideReason
Form input validation (format checking)Client-sideImmediate feedback, reduces server load
Sensitive data validation (password check)Server-sideDatabase access needed; client-side is insecure
User interface animationsClient-sideJavaScript/CSS in browser
Database queriesServer-sideDirect database access required
Search engine indexingServer-sideSearch engines need server-rendered content
Image manipulation (resize, filter)Client-side (preview)For immediate feedback; final processing server-side
Payment processingServer-sideSecurity: API keys and transaction logic hidden

Server-Side Processing Example

When a user submits a login form:

  1. Client-side: JavaScript validates that fields are not empty and email format is correct.
  2. Form data is sent to the server via HTTP POST.
  3. Server-side: PHP/Python code checks credentials against the database.
  4. Server sends back a response (success: redirect to dashboard; failure: error message).

Multimedia Applications

Image Editing

FeatureDescriptionPurpose
CroppingRemove outer parts of an imageFocus on subject, remove distractions
ResizingChange image dimensionsFit specific requirements
Colour adjustmentBrightness, contrast, saturation, hueEnhance appearance
LayersStack multiple elements on separate editable layersNon-destructive editing
Selection toolsSelect specific areas (lasso, magic wand, marquee)Edit parts of an image
Filters and effectsApply artistic or corrective effects (blur, sharpen, distort)Creative and corrective editing
Text toolAdd text overlaysCaptions, watermarks, titles
Clone/stamp toolCopy pixels from one area to anotherRemove blemishes, duplicate elements
Colour modesRGB for screen, CMYK for printOutput-specific preparation

Video Editing

FeatureDescriptionPurpose
Timeline editingArrange clips in sequential order on a timelineAssemble the video narrative
Cutting and trimmingRemove unwanted segments from clipsTighten the edit
TransitionsEffects between clips (dissolve, wipe, fade)Smooth scene changes
Audio editingAdjust volume, add background music, voiceoverBalance audio levels
Text overlaysTitles, subtitles, lower thirdsConvey information
Colour gradingAdjust colour, contrast, and toneConsistent visual style
Rendering/exportConvert the edited project into a distributable video fileFinal output in appropriate format

Common video formats:

FormatContainerVideo CodecAudio CodecTypical Use
MP4MP4H.264 / H.265AACWeb, streaming, mobile
AVIAVIVariousVariousLegacy Windows
MKVMKVH.264 / VP9AAC / FLACHigh quality, flexibility
MOVMOVH.264 / ProResAACApple ecosystem
WebMWebMVP8 / VP9VorbisWeb (open source)

Animation

Animation TypeDescriptionTools
Frame-by-frameIndividual frames drawn/edited sequentiallyFlash, Toon Boom
TweeningSoftware generates intermediate frames between keyframesFlash, After Effects
CSS animationAnimated using CSS @keyframes and transition propertiesText editor
JavaScriptAnimated via DOM manipulation and requestAnimationFrameText editor
SVG animationAnimated using SMIL or CSSText editor, Inkscape
3D animationThree-dimensional animated modelsBlender, Maya

User Interface Design Principles

Nielsen's 10 Usability Heuristics

These heuristics, developed by Jakob Nielsen, are widely used principles for evaluating user interface design.

HeuristicPrincipleDescription
1Visibility of system statusThe system should always inform users about what is going on through appropriate feedback within reasonable time
2Match between system and the real worldThe system should speak the users' language with familiar words, phrases, and concepts rather than system-oriented terms
3User control and freedomUsers need a clearly marked "emergency exit" to leave the unwanted state without having to go through an extended dialogue (e.g., Undo, Redo)
4Consistency and standardsUsers should not have to wonder whether different words, situations, or actions mean the same thing; follow platform conventions
5Error preventionCareful design prevents problems from occurring in the first place; prefer error prevention over error messages
6Recognition rather than recallMinimise the user's memory load by making objects, actions, and options visible; the user should not have to remember information from one part to another
7Flexibility and efficiency of useAccelerators (shortcuts, expert modes) speed up interaction for experienced users while remaining accessible to novices
8Aesthetic and minimalist designDialogues should not contain irrelevant or rarely needed information; every extra unit of information competes with relevant information
9Help users recognise, diagnose, and recover from errorsError messages should be expressed in plain language, precisely indicate the problem, and constructively suggest a solution
10Help and documentationAlthough better if the system can be used without documentation, it may be necessary to provide help and documentation searchable and focused on the user's task

Key Design Principles

PrincipleDescriptionApplication Example
ConsistencySame visual style, terminology, and behaviour throughoutSame button style on all pages
FeedbackAcknowledge user actions with visual, auditory, or haptic cuesButton colour change on hover/click
AffordanceDesign elements should suggest their functionalityA raised button looks clickable
VisibilityImportant elements should be visible and not hiddenClear navigation menu
AccessibilityDesign for users with disabilities (colour blindness, motor, visual)Alt text for images, keyboard navigation
HierarchyOrganise information by importance using size, colour, positionLarge heading, smaller body text
SimplicityKeep the interface clean and unclutteredMinimal elements, clear calls to action
NavigationUsers should always know where they are and how to get elsewhereBreadcrumbs, navigation menu, sitemap

Colour and Accessibility

ConsiderationDescription
Colour contrastText and background must have sufficient contrast ratio (WCAG AA: 4.5:1 for normal text)
Colour blindnessDo not rely solely on colour to convey information; use labels/icons additionally
Red-green deficiencyMost common form of colour blindness (affects ~8% of males)
High contrast modeSupport system-level high contrast settings

Mobile Interface Considerations

ConsiderationDescription
Touch targetsMinimum 44x44 pixels for tap targets (Apple HIG)
Screen sizeDesign for variable screen sizes; use responsive layout
OrientationSupport both portrait and landscape modes
Gesture supportSupport swipe, pinch, and long-press where appropriate
PerformanceMinimise data transfer and processing for mobile networks

Common Pitfalls

  1. Forgetting the DOCTYPE: Without <!DOCTYPE html>, browsers render in quirks mode, which can cause inconsistent layout across browsers. Always include it as the first line.

  2. Block vs inline elements: Block elements (like <div>, ``) take full width and start on a new line. Inline elements (like <span>, <a>) take only content width and flow within text. You cannot set width/height on inline elements without changing their display type.

  3. CSS specificity conflicts: When multiple rules target the same element, the most specific rule wins. !important overrides everything but should be used sparingly. Understand the specificity hierarchy: inline > ID > class > element.

  4. Box model confusion: By default, width and height apply only to the content area, not including padding and border. Use box-sizing: border-box to include padding and border in the specified dimensions.

  5. JavaScript in the wrong place: If a <script> in the <head> references DOM elements that have not yet loaded, it will fail. Either place scripts at the end of <body> or use the defer attribute.

  6. Event listener attachment before element exists: If you try to attach an event listener to an element that has not been rendered yet, the listener will not be attached. Wrap code in DOMContentLoaded event or place the script after the HTML element.

  7. Client-side validation is not security: Client-side validation improves user experience but can be bypassed. Always validate on the server side for security-critical operations (passwords, payment, data integrity).

  8. Ignoring accessibility: Missing alt text on images, poor colour contrast, and lack of keyboard navigation exclude users with disabilities and may violate accessibility legislation.

  9. Responsive design afterthought: Designing for desktop first and then trying to make it responsive is harder than designing mobile-first. Use media queries to progressively enhance layouts for larger screens.

  10. Overusing animations: Excessive animations distract users, slow down perceived performance, and can cause motion sickness for some users. Use animations purposefully and respect the prefers-reduced-motion media query.


Practice Problems

Question 1: HTML and Forms

(a) Write HTML code for a registration form with the following fields: username (text, required), email (email, required), password (password, required, minimum 8 characters), and a submit button.

(b) Explain the difference between the GET and POST form methods.

(c) What is the purpose of the <label> element, and why is it important?

Answer:

(a)

<form action="/register" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required />

<label for="email">Email:</label>
<input type="email" id="email" name="email" required />

<label for="password">Password:</label>
<input type="password" id="password" name="password" minlength="8" required />

<input type="submit" value="Register" />
</form>

(b) GET sends form data as URL parameters (appended to the action URL after ?). It is visible in the browser address bar, has a length limit (approximately 2048 characters), and should be used for idempotent requests (search, filter). POST sends form data in the HTTP request body, is not visible in the URL, has no practical length limit, and should be used for data that changes state on the server (login, registration, payment).

(c) The <label> element associates a text description with a form control via the for attribute (matching the input's id). It is important because: (1) clicking the label focuses the associated input, increasing the clickable area for mouse/touch users. (2) Screen readers announce the label text when the input receives focus, improving accessibility for visually impaired users. (3) It creates a semantic association between the description and the input field.

Question 2: CSS Layout

(a) Write CSS to create a flexbox layout with a sidebar (250px wide) on the left and a main content area that fills the remaining space.

(b) Write a media query that changes the layout to a single column (sidebar on top, content below) when the screen width is less than 768 pixels.

(c) Explain what box-sizing: border-box does and why it is commonly recommended.

Answer:

(a)

.container {
display: flex;
}

.sidebar {
width: 250px;
flex-shrink: 0;
}

.main-content {
flex: 1;
}

(b)

@media (max-width: 767px) {
.container {
flex-direction: column;
}

.sidebar {
width: 100%;
}
}

(c) box-sizing: border-box makes the width and height properties include the content area, padding, and border. Without it (the default content-box), width applies only to the content, and padding and border are added on top, making the total element wider than specified. This commonly causes layout issues when elements exceed their container. With border-box, if you set width: 250px, the total rendered width including padding and border will be exactly 250px, making layout calculations predictable. Many developers apply * { box-sizing: border-box; } globally.

Question 3: JavaScript DOM and Events

(a) Write JavaScript code that: (i) finds an element with id counter, (ii) adds a click event listener that increments a counter variable and updates the element's text content to show the current count.

(b) Write JavaScript to validate a form with id loginForm. If the password field (id pass) has fewer than 8 characters, prevent form submission and display an alert.

(c) Explain the difference between textContent and innerHTML.

Answer:

(a)

var count = 0;
var counterElement = document.getElementById('counter');

counterElement.addEventListener('click', function () {
count++;
counterElement.textContent = 'Count: ' + count;
});

(b)

document.getElementById('loginForm').addEventListener('submit', function (event) {
var password = document.getElementById('pass').value;
if (password.length < 8) {
event.preventDefault();
alert('Password must be at least 8 characters long.');
}
});

(c) textContent sets or returns the plain text content of an element. It does not parse HTML tags -- any HTML tags in the string are treated as literal text. innerHTML sets or returns the HTML content of an element, including any child elements. HTML tags in the string are parsed and rendered as DOM elements. textContent is safer (no XSS risk) and faster. innerHTML is needed when you intentionally want to insert HTML elements.

Question 4: Client-Side vs Server-Side

An e-commerce website needs to implement the following features. For each, state whether the processing should be client-side, server-side, or both, and justify your answer.

(a) Displaying a product image gallery with thumbnail navigation.

(b) Checking whether a credit card number has the correct format (16 digits, valid Luhn check).

(c) Processing the payment and deducting the amount from the customer's account.

(d) Showing a "items in cart" counter that updates when the user adds a product.

(e) Searching the product database for items matching a search query.

Answer:

(a) Client-side. Image gallery navigation (switching between images, zoom, pan) requires immediate responsiveness and does not need server involvement. JavaScript handles this in the browser for the best user experience.

(b) Both. Client-side validation provides immediate feedback (format check, Luhn algorithm) before the form is submitted, improving UX. Server-side validation is essential for security -- client-side checks can be bypassed, and the server must never trust client data.

(c) Server-side. Payment processing involves accessing the payment gateway API, verifying account balances, and modifying financial records. This must happen on the server for security (API keys must not be exposed to the client) and reliability (client-side processing would be inherently insecure and unreliable).

(d) Client-side. Updating the cart counter is a UI operation that should happen instantly when the user clicks "Add to cart." JavaScript can update the DOM without a server round-trip. The actual cart data should be synced with the server (via an AJAX request), but the visual update is client-side.

(e) Server-side. Searching a database requires server-side processing (SQL query). The client cannot directly access the database. The client sends the search query, and the server returns the results. Client-side filtering (narrowing already-loaded results) could supplement this.

Question 5: UI Design Evaluation

A school's library website has the following characteristics:

  • The search bar is located at the bottom of the page.
  • The navigation menu uses only icons with no text labels.
  • Error messages say "Error 404" with no additional explanation.
  • The page uses light grey text on a white background.
  • Clicking "Back" after submitting a search shows a warning about resubmitting the form.

Evaluate each characteristic against usability principles and suggest improvements.

Answer:

Search bar at bottom: Violates the visibility principle. Users expect search at the top of the page (standard convention). Move the search bar to the header area where it is immediately visible.

Icon-only navigation: Violates the recognition rather than recall and accessibility principles. Icons alone are ambiguous -- users must guess what each icon means. Text labels provide clarity and help screen readers. Add text labels below or beside each icon.

"Error 404" messages: Violates the "help users recognise, diagnose, and recover from errors" heuristic. Technical error codes are meaningless to most users. Replace with plain language: "The page you requested could not be found. Here are some suggestions..." with links to common pages.

Light grey text on white background: Violates visibility and accessibility principles. The contrast ratio is insufficient, making text hard to read, especially for users with visual impairments. Use dark text (e.g., #333333) on a white background to meet WCAG AA contrast requirements (4.5:1).

Form resubmission warning: Violates user control and freedom. After form submission, the browser shows a warning about resubmission when the user navigates back. This indicates the site uses POST without the Post/Redirect/Get (PRG) pattern. Fix by redirecting (HTTP 302/303) to a results page after POST processing, so the Back button returns to the form, not the POST request.