The web is built on three core technologies that work together: HTML for structure, CSS for presentation, and JavaScript for interactivity.
HTML (Hypertext Markup Language)
•
<!DOCTYPE html> - Declares document type•
<html> - Root element, contains entire page•
<head> - Contains metadata (title, stylesheets, scripts)•
<body> - Contains visible contentCommon HTML Tags
<h1> to <h6>• Purpose: Headings (h1 largest)
• Example:
<h1>Main Title</h1><p>• Purpose: Paragraph
• Example:
<p>Some text</p><a>• Purpose: Hyperlink
• Example:
<a href="url">Link text</a><img>• Purpose: Image
• Example:
<img src="photo.jpg" alt="description"><ul>, <ol>• Purpose: Lists (unordered/ordered)
• Example:
<ul><li>Item</li></ul><div>• Purpose: Division/container
• Example:
<div class="section">...</div><span>• Purpose: Inline container
• Example:
<span>text</span><table>• Purpose: Table
• Example:
<table><tr><td>Cell</td></tr></table><form>• Purpose: Form for user input
• Example:
<form action="/submit">...</form><input>• Purpose: Form input field
• Example:
<input type="text" name="username"><!DOCTYPE html>
<html>
<head>
<title>My First Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a paragraph of text.</p>
<h2>My Favourite Things</h2>
<ul>
<li>Programming</li>
<li>Music</li>
<li>Gaming</li>
</ul>
<a href="https://example.com">Visit Example</a>
<img src="photo.jpg" alt="A photo" width="300">
</body>
</html>
CSS (Cascading Style Sheets)
1) Inline CSS
<p style="color: red; font-size: 16px;">Red text</p>
<head>
<style>
p { color: red; }
</style>
</head>
<head>
<link rel="stylesheet" href="styles.css">
</head>
CSS Selectors and Properties
selector {
property: value;
property: value;
}
/* Select all paragraphs */
p {
color: blue;
font-size: 14px;
}
/* Select by class (.) */
.highlight {
background-color: yellow;
}
/* Select by ID (#) */
#header {
font-weight: bold;
}
/* Select by element and class */
p.intro {
font-style: italic;
}
• Purpose: Text colour
• Example Values: red, #FF0000, rgb(255,0,0)
background-color
• Purpose: Background colour
• Example Values: blue, #0000FF
font-size
• Purpose: Text size
• Example Values: 16px, 1.2em, 120%
font-family
• Purpose: Font type
• Example Values: Arial, serif, sans-serif
margin
• Purpose: Space outside element
• Example Values: 10px, 5%
padding
• Purpose: Space inside element
• Example Values: 10px, 5%
border
• Purpose: Element border
• Example Values: 1px solid black
width, height
• Purpose: Dimensions
• Example Values: 200px, 50%
JavaScript
• Respond to user events (clicks, key presses, form submissions)
• Modify HTML content dynamically
• Validate form input before submission
• Create animations and visual effects
• Communicate with servers (AJAX)
• Store data locally in the browser
// Change text content
document.getElementById("demo").innerHTML = "Hello World!";
// Respond to button click
document.getElementById("btn").onclick = function() {
alert("Button clicked!");
};
// Validate form input
function validateForm() {
var email = document.getElementById("email").value;
if (email == "") {
alert("Email is required");
return false;
}
return true;
}
// Change CSS style
document.getElementById("box").style.backgroundColor = "red";
Adding JavaScript to a Web Page
JavaScript is written between <script> tags inside the HTML, or kept in a separate file that the page loads with <script src="code.js"></script>. The browser interprets it on the user's own computer: it is not compiled first, so the same script runs on any device that has a browser. It will not run if the user has disabled JavaScript.
<html>
<body>
<form>
<input type="text" id="firstName" name="firstName">
<input type="submit" value="Send" onclick="return checkName()">
</form>
<p id="message"></p>
<script>
function checkName() {
var name = document.getElementById("firstName").value;
if (name == "") {
document.getElementById("message").innerHTML = "Enter your name";
return false;
}
alert("Thank you, " + name);
return true;
}
</script>
</body>
</html>
• Clicking the button calls the function
checkName•
document.getElementById("firstName").value reads what was typed into the text box• If the box is empty,
innerHTML writes an error message into the paragraph that has the id message, and returning false stops the form being sent• Otherwise
alert shows a pop-up box and the form is sent•
document.write("text") is another way to produce output: it writes straight into the page