Course content

HTML

Learn how web pages are built. Understand structure, elements, and how the browser reads your code.

📚 Beginner ⏱ 45 lessons · ~8h

What is HTML?

The Simple Version

Every website you have ever visited is made with HTML. Google, YouTube, Wikipedia - all of them. HTML is the language that tells your browser what to show on the screen.

Think of it like this: imagine you are writing a letter to send home. You have a title at the top, paragraphs in the middle, and maybe a picture. You know what is a title and what is a paragraph because of how they look. But a computer does not have eyes. It cannot just look at your letter and figure it out.

So HTML is how you tell the computer: "This is a title. This is a paragraph. This is a picture." You put special labels around your words so the computer knows what everything is.

These labels are called tags. A tag looks like this:

<p>This is a paragraph.</p>

The <p> at the start says "a paragraph starts here." The </p> at the end says "the paragraph ends here." That is all HTML is - words with labels around them.

Think of HTML like the bones of a camel. You cannot see the bones from outside, but without them, the camel cannot stand up or walk. HTML gives a web page its shape and structure - it holds everything together.

What's Really Happening

When you type a website address and press Enter, here is what actually happens:

  1. Your browser asks for the page. It sends a message to a computer somewhere in the world (called a server) and says: "Send me the page for this address."
  2. The server sends back an HTML file. This file is just a text file. It contains all the words on the page, wrapped in HTML tags.
  3. Your browser reads the HTML. It goes through the file from top to bottom. Every time it sees a tag, it understands what that piece of content is.
  4. Your browser draws the page. It takes all the tagged content and turns it into what you see on screen - titles are big, paragraphs are normal text, images show up in the right place.

The important thing to understand is: HTML is not a programming language. It does not do calculations or make decisions. It is a markup language - it marks up content so the browser knows what it is.

HTML stands for HyperText Markup Language:

  • HyperText means text that can link to other text (that is how the web works - pages link to other pages)
  • Markup means you are labeling content with tags
  • Language means it has rules that both you and the browser understand

Good to know: The browser does not show your tags on the page. It reads them, understands them, and then hides them. The user only sees the result.

Try It Yourself

Below is a small piece of HTML. On the left side you see the code. On the right side you see what the browser shows. Try changing the text and watch the right side update.

HTML Code
Result

Things to try:

  • Change the text between the tags. What happens?
  • Change <h1> to <h2>. What changes?
  • Add a new line: <p>This is a new paragraph.</p>
  • What happens if you remove the </p> from one of the lines?

Try More Tags

HTML has many different tags. Here are a few. Try changing them and see what each one does.

HTML Code
Result

Notice:

  • <strong> makes text bold (it tells the browser this word is important)
  • <em> makes text italic (it adds emphasis, like stressing a word when speaking)
  • <h1> is the biggest heading, <h2> is smaller

Challenge

Go back to the first editor above and try to write HTML that looks like this:

  • A big title with your name
  • A smaller title that says "About Me"
  • A paragraph about where you come from
  • Another paragraph about your favourite food with one bold word in it

There is no wrong answer. If it shows up on the right side and looks the way you wanted, you did it right!

Remember: Every tag that opens must close. <p> needs </p>. <h1> needs </h1>. The closing tag always has a / in it.

What You Learned

  • HTML is the language browsers use to understand web pages
  • It uses tags to label content (titles, paragraphs, bold text)
  • Tags come in pairs: an opening tag and a closing tag
  • The browser reads your HTML and turns it into what you see on screen
  • HTML is not a programming language - it is a markup language that gives structure to content
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

How to Write HTML

The Simple Version

To write HTML, you do not need anything special or expensive. You just need something that lets you type text and save it as a file. That is it.

Think of it like writing a letter. You could write it on fancy paper with a special pen, or you could write it on a plain piece of paper with a pencil. The letter is the same - what matters is the words, not the tool.

HTML works the same way. You type your HTML into a file, save it with the name ending in .html, and then open that file in your browser. The browser reads your file and shows you the page.

For example, if you save a file called mypage.html and open it in Chrome or Firefox, you will see your web page. It is that simple.

An HTML file is just a text file with a different name ending. It is like how a jerrycan and a bucket both hold water, but you know which is which by looking at them. A .html file is a text file, but the browser knows it should read it as a web page.

What's Really Happening

When you write HTML, you are creating a plain text file. This is important to understand. Your HTML file does not contain any colors, fonts, or layout information by itself. It only contains text and tags.

Here is what "plain text" means: if you open the file with a simple text editor, you see exactly what is in the file. Nothing is hidden. Nothing is formatted. Just characters on a screen.

What tool should you use?

You need a text editor - a program made for writing code. Here are some good ones, and they are all free:

  • Visual Studio Code (VS Code) - the most popular choice. It colors your code so it is easier to read, and it helps you spot mistakes.
  • Notepad++ - simple and lightweight. Good if you want something that just works.
  • Sublime Text - fast and clean.
  • Even Notepad (that comes with Windows) works, but it does not help you much.

Important: Do not use Microsoft Word or Google Docs to write HTML. These programs add hidden formatting to your text that will break your HTML. Always use a plain text editor.

The process looks like this:

  1. Open your text editor
  2. Type your HTML code
  3. Save the file with a .html ending (for example: index.html)
  4. Find the file on your computer and double-click it - it opens in your browser
  5. To make changes: go back to the editor, change the code, save it, then refresh the browser

The file name index.html is special. On the web, when someone visits a website, the server looks for a file called index.html first. It is the default home page. That is why most websites have a file with this name.

Try It Yourself

You do not need to install anything right now. Use the editor below to practice. This is the same as what happens when you write HTML in a text editor and open it in a browser - you just get to do both at the same time here.

HTML Code
Result

Things to try:

  • Add more paragraphs. What happens when you add many of them?
  • Try writing text without any tags at all. Just plain words. What does the browser do with it?
  • Try pressing Enter between two words (without tags). Does the browser show them on separate lines?

Something Important to Notice

If you tried writing text without tags and pressing Enter, you probably discovered something surprising: the browser ignores your Enter key.

Try this in the editor above. Write:

Habari
Karibu

You might expect to see "Habari" and "Karibu" on two separate lines. But the browser shows them on the same line: "Habari Karibu".

This is because the browser does not care about where you press Enter in your code. It only cares about tags. If you want two separate paragraphs, you must use <p> tags:

<p>Habari</p>
<p>Karibu</p>

This is one of the first "ohhh" moments in learning HTML. The browser does not read your file the way your eyes do. It reads the tags.

See the Difference

Compare what happens with and without tags:

HTML Code
Result

See it? The first three lines without tags all run together. The last three lines with <p> tags are properly separated. Tags are how you talk to the browser.

Challenge

Using the editor above, try to create a page that has:

  • A title with the name of your town or village (use <h1>)
  • Three paragraphs - one in English, one in Kiswahili, one mixing both
  • At least one bold word (use <strong>)

Then try this experiment: write the exact same content but without any tags. Compare the two results. This is the best way to understand why HTML exists.

What You Learned

  • HTML is written in a plain text file saved with a .html ending
  • You use a text editor (like VS Code) to write HTML, not Word or Google Docs
  • The file called index.html is the default home page of any website
  • The browser ignores Enter keys and spaces in your code - it only reads tags
  • Tags are how you tell the browser what your content is
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Documents

The Simple Version

In the last two lessons, we just wrote some tags and they worked. But a real HTML page has a specific structure - like a house has a foundation, walls, and a roof.

Every HTML page has the same skeleton. It looks like this:

<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <p>Hello!</p>
  </body>
</html>

It looks like a lot, but there are only 4 things to understand:

  • <!DOCTYPE html> - tells the browser "this is an HTML page"
  • <html> - wraps everything on the page
  • <head> - information about the page (the browser reads this but does not show it on screen)
  • <body> - everything the user actually sees
Think of it like a duka (shop). The <head> is like the sign outside - it tells you the name and what they sell, but you do not buy the sign. The <body> is what is inside the shop - the actual goods, the things you see and use.

What's Really Happening

Let us go through each part and understand what the browser does with it:

<!DOCTYPE html>

This is not a tag - it is a declaration. It tells the browser: "I am writing in HTML5" (the modern version of HTML). Without it, the browser might try to read your page using old rules from the 1990s. Always include it as the very first line. You do not need to close it.

<html> ... </html>

This is the root element. Everything on your page goes inside it. Think of it as the outer container that holds the entire page. The browser looks for this tag to know where your HTML starts and ends.

<head> ... </head>

This section is for the browser, not for the user. Things that go here include:

  • The <title> - this appears in the browser tab, not on the page itself
  • Character encoding information (what alphabet and symbols the page can use)
  • Links to stylesheets (files that make the page look pretty - you will learn about this later)

The user never sees what is inside <head> on the web page. But the browser uses it to set things up before drawing the page.

<body> ... </body>

This is where everything visible goes. Every heading, paragraph, image, button, and link you see on a web page is inside the <body>. When you have been writing <h1> and <p> in the previous lessons, those all belong inside the body.

Good to know: When you write <title>My Page</title> inside the <head>, look at the browser tab at the top of your window. That is where the title appears - not on the page itself.

Try It Yourself

Here is a complete HTML document. Look at the structure carefully. Try changing the title and see where it shows up (hint: look at the top of the preview area).

HTML Code
Result

Things to try:

  • Change the text inside <title>. Notice that it does not change what you see in the preview - it only changes the page title.
  • Move a <p> tag from the <body> to the <head>. What happens? Does it still show up?
  • What happens if you delete the <body> tags but keep the content? Does it still work?

A Surprising Experiment

If you tried removing the <body> tags, you probably noticed that the browser still shows the content. Why?

Browsers are very forgiving. If you make mistakes or leave things out, the browser tries to fix your code and guess what you meant. It will still create a <head> and <body> behind the scenes, even if you do not write them.

But here is why you should always write the full structure:

  • Other people reading your code need to understand it
  • Some browsers might guess differently
  • When your pages get bigger and more complex, the structure keeps things organized
  • It is how professional developers write HTML

The browser being forgiving does not mean you should be sloppy. Write proper HTML, and you will never have to wonder why something looks wrong.

Build a Complete Page

Try writing a full HTML page from scratch. This editor is empty - fill it in with the complete structure.

HTML Code
Result

Need a hint? The structure is:

<!DOCTYPE html>
<html>
  <head>
    <title>...</title>
  </head>
  <body>
    ...your content here...
  </body>
</html>

Challenge

Create a complete HTML page about your village, your school, or your town. It should have:

  • A proper <!DOCTYPE html> declaration
  • A <title> - maybe "Marsabit" or "Isiolo" or "Garissa" or wherever you are from
  • A main heading (<h1>) with the name of your place
  • A subheading (<h2>) - "What I Like About It"
  • At least two paragraphs - one in English, one in Kiswahili

If you can write this without looking at the hint above, you are already thinking like a web developer. Well done!

What You Learned

  • Every HTML page has a skeleton: <!DOCTYPE html>, <html>, <head>, and <body>
  • The <head> is for information about the page (not visible on the page itself)
  • The <body> is for everything the user sees
  • The <title> tag sets the text in the browser tab
  • Browsers are forgiving and try to fix mistakes, but you should always write proper HTML
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Elements

The Simple Version

You already know about tags - those labels like <h1> and <p>. Now let us learn the proper name for the whole thing: an element.

An element is everything together - the opening tag, the content inside, and the closing tag:

<p>Ninajifunza HTML</p>

Let us break it down:

  • <p> = the opening tag - it says "start here"
  • Ninajifunza HTML = the content - the actual words
  • </p> = the closing tag - it says "stop here"

All three parts together make one element. That is the proper word. When developers talk about "the paragraph element" they mean the whole thing - opening tag + content + closing tag.

Think of an element like a sufuria (cooking pot) with a lid. The opening tag is when you take off the lid and start putting food in. The content is the food. The closing tag is putting the lid back on. The whole thing - pot, food, lid - is the element.

What's Really Happening

When the browser reads your HTML file, it does not just see text. It builds a structure in its memory. Every element becomes an object that the browser keeps track of.

Think of it like this: you write <p>Habari</p>, and the browser creates a box in memory labeled "paragraph" with the text "Habari" inside it. It does this for every element on your page.

Elements can go inside other elements. This is called nesting - like putting a box inside a box:

<p>I love <strong>ugali</strong> so much!</p>

Here, the <strong> element is inside the <p> element. The paragraph is the parent, and the bold text is the child. The browser understands this relationship - it knows the bold text belongs to that paragraph.

Important rule about nesting: elements must close in the right order. The last one you open must be the first one you close. Like putting on socks then shoes - you take off shoes first, then socks.

<!-- Correct - Sawa ✔ -->
<p>I love <strong>chai</strong> ya maziwa</p>

<!-- Wrong - Vibaya ✘ -->
<p>I love <strong>chai</p> ya maziwa</strong>

Good to know: Some elements do not have a closing tag because they have no content inside them. These are called empty elements or void elements. You will see these soon - like <br> (line break) and <img> (image). They work alone.

Try It Yourself

Look at the nested elements below. Try changing the nesting - put bold inside italic, or try nesting three levels deep.

HTML Code
Result

Things to try:

  • Notice how <strong><em>fresh fruits</em></strong> makes text bold and italic at the same time
  • Try wrapping a whole paragraph inside <strong> - what happens?
  • What happens if you nest incorrectly? Try closing tags in the wrong order

Empty Elements

Some elements do not need content or a closing tag. They do one thing and that is it. The most common one is <br> which creates a line break - it pushes text to the next line.

Remember when we learned that pressing Enter in your code does nothing? Well, <br> is how you actually break a line without starting a new paragraph.

HTML Code
Result

Notice:

  • <br> breaks the line but stays inside the same paragraph - it is not a new paragraph, just a new line
  • <hr> draws a horizontal line across the page - good for separating sections
  • Neither <br> nor <hr> has a closing tag - they are empty elements

Challenge

Write an introduction about yourself using what you have learned. Include:

  • A heading with your name
  • A paragraph with three lines about you, using <br> between them (your name, where you are from, what you like)
  • An <hr> line to separate sections
  • A second paragraph with something in bold and something in italic nested together
  • Write in both English and Kiswahili!

Remember: An element = opening tag + content + closing tag. Empty elements like <br> and <hr> are the exception - they work alone.

What You Learned

  • An element is the opening tag + content + closing tag, all together
  • Elements can be nested inside each other
  • Nested elements must close in the correct order - last opened, first closed
  • Empty elements like <br> and <hr> have no closing tag
  • <br> = line break, <hr> = horizontal line
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Attributes

The Simple Version

You know how to create elements - headings, paragraphs, bold text. But sometimes you need to give an element extra information. That is what attributes do.

Think of it this way: if an element is a person, the tag is their name, and an attribute is extra detail about them.

For example, if you want to create a link to another website, the <a> tag alone is not enough. The browser needs to know: link to where? You give it that information with an attribute:

<a href="https://google.com">Go to Google</a>

The href="https://google.com" part is the attribute. It tells the link where to go. Without it, the browser would have a link that goes nowhere - like a door with no path.

An attribute always has two parts:

  • A name - what kind of information it is (like href)
  • A value - the actual information (like "https://google.com")

They are written as: name="value" and they always go inside the opening tag.

Imagine you are sending someone to a duka (shop) to buy sugar. You do not just say "go to the shop." You say "go to Mama Halima's shop." The shop is the element. "Mama Halima's" is the attribute - it gives the extra detail needed to know which shop.

What's Really Happening

When the browser reads an element with attributes, it stores the attribute information together with the element in memory. The element becomes more than just "a link" - it becomes "a link that goes to google.com".

Rules for attributes:

  1. Attributes go in the opening tag only - never in the closing tag
  2. Values should be in quotes - use double quotes "like this"
  3. An element can have many attributes - separate them with spaces
  4. Attributes are lowercase - always write href, not HREF

Here are some common attributes you will use often:

  • href - used in links (<a>) to say where to go
  • src - used in images (<img>) to say where the picture is
  • alt - used in images to describe the picture in words (for people who cannot see the image)
  • style - used on any element to change how it looks (color, size, etc.)
  • title - adds a tooltip that appears when you hover over an element
  • lang - tells the browser what language the content is in

Good to know: The alt attribute on images is not optional - it is important! If the image does not load (maybe slow internet), the alt text shows instead. Also, blind people use special software that reads the alt text out loud.

Try It Yourself

Here are elements with different attributes. Change the values and see what happens.

HTML Code
Result

Things to try:

  • Change color: green to color: red or color: orange
  • Change the font-size to 24px or 12px - what happens?
  • Change the title text and hover over the paragraph again
  • Change the link to go to a different website

Multiple Attributes

An element can have more than one attribute. You just put a space between them. Here is an example with images and links that have multiple attributes:

HTML Code
Result

Notice how each element has multiple attributes separated by spaces. The browser reads them all and applies everything together.

Challenge

Create a page about your favourite place. It should include:

  • A heading with a style attribute that changes its color
  • Two paragraphs - one with lang="sw" (Kiswahili) and one with lang="en" (English)
  • Each paragraph should have a title attribute with a tooltip message
  • A link (<a>) with an href to any website you like
  • One paragraph with a coloured background using style="background-color: ..."

Experiment with different colors! Try: red, blue, green, orange, purple, tomato, gold, teal. HTML has over 140 color names!

What You Learned

  • Attributes give extra information to elements
  • They are written as name="value" inside the opening tag
  • Common attributes: href (links), src (images), style (appearance), title (tooltip), alt (image description)
  • An element can have many attributes separated by spaces
  • Values should always be in double quotes
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Comments

The Simple Version

Sometimes when you are writing HTML, you want to leave yourself a note. Maybe you want to remember why you wrote something, or leave a message for someone else who will read your code later.

HTML lets you write comments - notes that the browser completely ignores. The browser will not show them on the page. They are only visible when you look at the code.

A comment looks like this:

<!-- This is a comment. Browser haioni hii -->

It starts with <!-- and ends with -->. Everything between those markers is invisible to anyone visiting your page.

Think of comments like writing with a pencil on the back of a painting. The person looking at the painting does not see what you wrote on the back. But if another artist picks it up and turns it over, they can read your notes. Comments are notes for people who look at the code, not the page.

What's Really Happening

When the browser reads your HTML file and finds <!--, it says to itself: "Everything from here until I find --> is a comment. I will skip it completely." It does not create any element, does not show any text, does not do anything with it.

Why would you use comments?

  • To explain your code: when you come back to your code after weeks or months, comments help you remember what you were thinking
  • To leave notes for others: if someone else works on your code, they can understand your decisions
  • To temporarily hide code: instead of deleting code you might want later, you can "comment it out" - wrap it in a comment so the browser ignores it, but you can bring it back easily
  • To organize sections: in a long HTML file, comments can mark where different parts of the page begin

Important: Even though comments are invisible on the page, they are still in your code. Anyone can see them by viewing the page source (right-click on any webpage and click "View Page Source"). So never put passwords, secrets, or private information in comments!

Good to know: Professional developers use comments all the time. It is a sign of a good coder - not because they cannot remember things, but because they respect the people who will read their code next.

Try It Yourself

Look at the code below. Some lines are comments and some are real HTML. Can you tell which lines will show on the page before looking at the result?

HTML Code
Result

Things to try:

  • Count how many comments are in the code. Now count how many paragraphs show on the page.
  • Remove the <!-- and --> from the last two hidden paragraphs. What happens?
  • Try commenting out the <h1> by wrapping it in <!-- -->. Does the title disappear?

Commenting Out Code

One of the most useful tricks with comments is commenting out code. This means wrapping existing code in comment tags so the browser skips it. It is like turning code on and off without deleting it.

HTML Code
Result

Try this:

  • Uncomment items 3 and 4 (remove the <!-- and --> around them). The menu grows!
  • Now comment out items 1 and 2 instead. The menu changes!
  • This is how developers test different versions of their pages without losing code

Challenge

Build a page about your dream school or community centre. Include:

  • A comment at the top explaining what the page is about (in both languages)
  • A heading and at least three paragraphs
  • Use comments to organize your code: <!-- Section: Introduction -->, <!-- Section: What we need -->
  • Write two extra paragraphs but comment them out - like ideas you are saving for later
  • A multi-line comment at the bottom with a to-do list of things you want to add

Congratulations on finishing Module 1! You now know all the basics of HTML.

What You Learned

  • Comments are notes in your code that the browser ignores - <!-- like this -->
  • They are used to explain code, leave notes, organize sections, and temporarily hide code
  • Commenting out code means wrapping it in comment tags to hide it without deleting
  • Comments can be one line or many lines
  • Never put private information in comments - anyone can view page source!

Module 1 Complete!

You have finished all 6 lessons in Module 1: The Basics. Here is everything you now know:

  1. What HTML is - a markup language that gives structure to web pages
  2. How to write it - in a text editor, saved as a .html file
  3. HTML document structure - DOCTYPE, html, head, body
  4. Elements - opening tag + content + closing tag
  5. Attributes - extra information inside opening tags
  6. Comments - notes the browser ignores

These are the building blocks for everything that comes next. In Module 2, you will learn about all the different ways to work with text - headings, paragraphs, formatting, and more.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Headings

The Simple Version

When you read a newspaper or a textbook, the big bold titles tell you what each section is about. Without titles, you would have to read everything to find what you are looking for.

HTML headings work the same way. They are titles for different parts of your page. HTML gives you six levels of headings, from the biggest to the smallest:

  • <h1> - the biggest, most important heading
  • <h2> - a subheading under h1
  • <h3> - a subheading under h2
  • <h4> - smaller still
  • <h5> - even smaller
  • <h6> - the smallest heading

Think of them as a family: <h1> is the elder, the most important. Each one after it is a bit younger and smaller, but still carries weight.

Imagine the noticeboard at a chief's office (baraza). The biggest text at the top says "IMPORTANT NOTICE" - that is h1. Below it, the section "When and Where" is h2. Under that, smaller details like "Directions" is h3. The board is organized by size so you can quickly find what matters most. Headings organize your page the same way.

What's Really Happening

Headings are not just big text. When the browser sees a heading tag, it understands something important: this text is a title, and it has a rank.

This matters for two big reasons:

1. Structure and meaning

Search engines like Google read your headings to understand what your page is about. If your <h1> says "Turkana County Tourism", Google knows the page is about tourism in Turkana. This helps people find your page when they search.

2. Accessibility

Blind people use software called screen readers that read web pages out loud. Screen readers use headings to let the user jump between sections - like a table of contents they can listen to. If you use headings properly, you make your page usable for everyone.

Rules for headings:

  • Use only one <h1> per page - it is the main title of the whole page
  • Do not skip levels - do not jump from <h1> straight to <h4>. Go in order: h1, then h2, then h3
  • Do not use headings just to make text big. Use them for actual titles and sections

Good to know: Making text big is the job of CSS (which you will learn later). Headings are for meaning - telling the browser and the user "this is a title." Using <h1> just because you want big text is like shouting in a library - it works, but it is the wrong approach!

Try It Yourself

See all six heading levels side by side. Notice how each one gets smaller.

HTML Code
Result

Things to try:

  • Notice the size difference between each heading level
  • Compare <h6> with the paragraph text. Which one looks bigger?
  • Try adding two <h1> tags. It works, but remember: you should only use one per page

Real Page Structure

Here is how headings are used on a real page - to organize content into sections, just like chapters in a book.

HTML Code
Result

Notice the structure:

  • <h1> - the main title (used only once)
  • <h2> - main sections: Geography, People, Why Visit
  • <h3> - sub-sections under each h2: Mount Marsabit, Chalbi Desert, Culture
  • This is like a table of contents - a person can quickly see what the page contains

Challenge

Create a page about your county or region. Use proper heading hierarchy:

  • One <h1> with the name of your county
  • At least three <h2> sections (like: Geography, People, Food)
  • At least two <h3> sub-sections under one of your h2s
  • A paragraph under each heading
  • Write in both English and Kiswahili

When you are done, read just the headings. Can you understand what the page is about without reading the paragraphs? If yes, your headings are good!

What You Learned

  • HTML has six heading levels: <h1> (biggest) through <h6> (smallest)
  • Use only one h1 per page - it is the main title
  • Do not skip levels - go h1, h2, h3 in order
  • Headings help Google understand your page and help blind users navigate
  • Use headings for structure, not just to make text big
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Paragraphs and Line Breaks

The Simple Version

Most of the content on any website is paragraphs - blocks of text. You have already been using the <p> tag, so this lesson goes deeper into how it works and what else you can do with text.

A paragraph in HTML is just like a paragraph in a book or a letter. It is a group of sentences about one idea. When you start a new idea, you start a new paragraph.

<p>This is the first paragraph. It talks about one thing.</p>
<p>This is the second paragraph. It talks about something else.</p>

The browser automatically adds some space between paragraphs so they do not look stuck together.

Paragraphs are like different topics in a conversation. When you are talking with a friend and change the subject, there is a small pause. That pause is like the space between paragraphs - it tells the reader "we are moving to a new idea now."

What's Really Happening

We touched on this in Lesson 2, but it is important enough to explain fully: the browser collapses whitespace.

What does that mean? When the browser reads your HTML:

  • Multiple spaces become one space
  • Pressing Enter (new lines) becomes one space
  • Multiple blank lines become nothing

So if you write this in your code:

<p>Habari      yako?       Mimi    ni     mzima.</p>

The browser shows: "Habari yako? Mimi ni mzima." - all the extra spaces are collapsed into single spaces.

And if you write:

<p>Line one.
Line two.
Line three.</p>

The browser shows: "Line one. Line two. Line three." - all on one line! The Enter keys become spaces.

How to actually break a line:

  • <p> - starts a whole new paragraph (with space above and below)
  • <br> - breaks the line but stays in the same paragraph (no extra space)
  • <pre> - preserves all your spaces and line breaks exactly as you typed them

Good to know: The <pre> tag stands for "preformatted text." It tells the browser: "Do not touch my spacing - show it exactly as I wrote it." This is useful for showing code examples or poetry where spacing matters.

Try It Yourself

See how the browser handles spaces and line breaks differently from how you type them.

HTML Code
Result

Notice the difference:

  • Separate <p> tags = space between each paragraph
  • <br> = new line but no extra space, stays in the same paragraph
  • Just pressing Enter in code = browser ignores it completely

The Pre Tag

Sometimes you need the browser to respect your spaces exactly. The <pre> tag does this. It is perfect for poetry, addresses, or showing code.

HTML Code
Result

See the difference? The <pre> version keeps every space and line break exactly where you put them. The normal <p> version squashes everything together.

Challenge

Create a page that shows the difference between all three methods:

  • Write a short story about your day using separate paragraphs for each part
  • Write an address or a poem using <br> for line breaks
  • Use <pre> to show something that needs exact spacing - like a simple drawing made with text characters, or a recipe with measurements
  • Try adding 10 spaces between two words in a <p> tag and in a <pre> tag. What is the difference?

What You Learned

  • The browser collapses whitespace - extra spaces and Enter keys become single spaces
  • <p> creates paragraphs with space between them
  • <br> breaks a line without starting a new paragraph
  • <pre> preserves all spacing exactly as you typed it
  • Use the right tool for the job: <p> for ideas, <br> for lines, <pre> for exact formatting
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Text Formatting

The Simple Version

When you talk, you do not say every word the same way. Sometimes you say a word louder to show it is important. Sometimes you whisper. Sometimes you stress a word to make a point.

HTML lets you do the same with text.

You already know <strong> (bold) and <em> (italic). But HTML has many more formatting tags:

  • <strong> - important text (bold)
  • <em> - emphasized text (italic)
  • <b> - bold text (just bold, no special meaning)
  • <i> - italic text (just italic, no special meaning)
  • <mark> - highlighted text (like a highlighter pen)
  • <small> - smaller text
  • <del> - deleted text (shown with a line through it)
  • <ins> - inserted text (shown with an underline)
  • <sub> - subscript (small text below the line)
  • <sup> - superscript (small text above the line)
Think of text formatting like the tone of your voice when telling a story around the campfire. When something is scary, your voice gets deeper. When something is exciting, you speak louder. HTML formatting is how you give your text a "voice" - how you tell the browser which words matter most and how they should feel.

What's Really Happening

There is an important difference that many people miss: some formatting tags have meaning, and some are just visual.

<strong> vs <b> - they both look bold, but:

  • <strong> tells the browser "this word is important." A screen reader will say it with more emphasis. Search engines pay more attention to it. It has meaning.
  • <b> just says "make this bold." No special meaning. It is visual only.

<em> vs <i> - they both look italic, but:

  • <em> means "this word is emphasized." Like stressing a word when you speak.
  • <i> just makes text italic. Often used for foreign words or titles of books.

When should you use which?

  • If the word is truly important or emphasized → use <strong> or <em>
  • If you just want the visual style → use <b> or <i>
  • When in doubt, use <strong> and <em> - they are the better choice most of the time

Good to know: The <del> and <ins> tags are often used together to show changes. For example, if a price changed: KSh 500 KSh 350. The browser understands that the old price was deleted and the new price was inserted.

Try It Yourself

See all the formatting tags in action. Try mixing them, nesting them, and see what happens.

HTML Code
Result

Things to try:

  • Change the prices and see the crossed-out effect with <del>
  • Try <mark> on different text - it is like using a highlighter pen!
  • Nest <strong> inside <em> - what does bold italic look like?
  • Try <sub> and <sup> for science formulas like CO2

Practical Example

Here is a realistic page that uses multiple formatting tags together, the way a real website would.

HTML Code
Result

Challenge

Create a community newsletter for your area. Include:

  • A main heading and a <small> date
  • At least two news stories with <strong> for important words
  • A highlighted announcement using <mark>
  • A price list with at least one item that changed price using <del> and <ins>
  • A quote from someone using <i>
  • Mix English and Kiswahili throughout!

What You Learned

  • <strong> = important (bold with meaning), <b> = just bold
  • <em> = emphasized (italic with meaning), <i> = just italic
  • <mark> = highlighted, <small> = smaller text
  • <del> = deleted (strikethrough), <ins> = inserted (underline)
  • <sub> = subscript, <sup> = superscript
  • Use tags with meaning (strong, em) over visual-only tags (b, i) when possible
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Quotations

The Simple Version

Sometimes you want to include words that someone else said, or a famous saying, or a proverb. In real life, you use quotation marks. In HTML, there are special tags for this.

There are two main types:

  • <blockquote> - for long quotes that stand on their own (like a whole sentence or paragraph from someone else)
  • <q> - for short quotes inside a sentence

The browser treats these differently from regular text. A <blockquote> gets indented (pushed to the right) and a <q> automatically gets quotation marks around it.

In a baraza (community meeting), when the chief speaks, everyone listens and his words carry weight. If you later tell someone what the chief said, you make it clear those are his words, not yours. That is what quotation tags do - they tell the browser "these words come from someone else, not from the page author."

What's Really Happening

HTML has several tags related to quoting and referencing. Let us go through each one:

<blockquote> - Block Quotation

This is for longer quotes that deserve their own space on the page. The browser indents it from both sides. You can use the cite attribute to say where the quote came from (the browser does not show this, but search engines and screen readers can use it).

<q> - Inline Quotation

This is for short quotes within a sentence. The browser automatically adds quotation marks around the text. The browser does it for you.

<cite> - Citation

This is for the title of a work - a book, a song, a movie, an article. It usually appears in italic.

<abbr> - Abbreviation

This marks a short form of a word. You can use the title attribute to show the full form when someone hovers over it.

<address> - Contact Information

This marks contact details like an email or physical address. The browser usually shows it in italic.

Good to know: Using the right tags matters for accessibility. When a screen reader encounters <blockquote>, it tells the blind user "this is a quote." If you just indent text using spaces, the screen reader has no idea it is a quote.

Try It Yourself

See how different quotation and reference tags look. Try changing the quotes to proverbs or sayings from your community.

HTML Code
Result

Notice:

  • <blockquote> is indented - the browser pushes it to the right
  • <q> automatically gets quotation marks - you do not need to type them
  • <cite> shows the person's name in italic - it tells the browser this is a source

Abbreviations and Addresses

Two more useful tags: <abbr> for short forms and <address> for contact information.

HTML Code
Result

Things to try:

  • Hover your mouse over the abbreviations to see the full text appear
  • Add your own abbreviations - what short forms are used in your community?
  • Change the address to your own school or community centre

Challenge

Create a "Wisdom Page" for your community. Include:

  • At least two proverbs or sayings from your community in <blockquote> (one in Kiswahili, one in English - or your local language!)
  • A sentence using <q> for a short quote - something your parent or teacher always says
  • The name of the person or source using <cite>
  • At least two abbreviations using <abbr> with the title attribute
  • Your school or community address using <address>

our proverbs carry wisdom from our ancestors. Preserve them in HTML!

What You Learned

  • <blockquote> = long quotes, displayed indented
  • <q> = short inline quotes, browser adds quotation marks automatically
  • <cite> = title of a work or source, displayed in italic
  • <abbr title="..."> = abbreviation with full text shown on hover
  • <address> = contact information, displayed in italic
  • Using the right tags helps screen readers and search engines understand your content
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Entities and Symbols

The Simple Version

You have a problem. Some characters are special in HTML. For example, the < and > characters are used for tags. So what happens if you want to actually show these characters on your page?

If you write <p> in your HTML, the browser thinks it is a tag. It does not show it as text.

The solution is HTML entities. These are special codes that represent characters. Instead of typing the character directly, you type a code and the browser shows the character.

An entity starts with & and ends with ;. For example:

  • &lt; shows < (less than sign)
  • &gt; shows > (greater than sign)
  • &amp; shows & (ampersand)
Think of entities like code words. If you are in a market and cannot say a word directly, you use a code word that everyone understands. Instead of saying "<" you say "&lt;" and the browser knows exactly what you mean.

What's Really Happening

There are three characters that you must write as entities in HTML. If you do not, the browser will get confused:

  • &lt; for < - because the browser thinks < starts a tag
  • &gt; for > - because the browser thinks > ends a tag
  • &amp; for & - because the browser thinks & starts an entity

But entities are also used for characters that are not on your keyboard. There are hundreds of them! Here are some useful ones:

  • &nbsp; = a non-breaking space (the browser will not collapse this space)
  • &copy; = © (copyright symbol)
  • &reg; = ® (registered trademark)
  • &trade; = ™ (trademark)
  • &hearts; = ♥ (heart)
  • &star; or &#9733; = ★ (star)
  • &#10003; = ✓ (checkmark)
  • &#10007; = ✗ (cross mark)
  • &larr; = ← (left arrow)
  • &rarr; = → (right arrow)

Two ways to write entities:

  • Named: &hearts; - uses a name (easier to remember)
  • Numbered: &#9829; - uses a number (works for any character)

Good to know: The &nbsp; (non-breaking space) is very useful. Remember how the browser collapses multiple spaces into one? If you write &nbsp;&nbsp;&nbsp; the browser keeps all three spaces! It also prevents the browser from breaking a line between two words.

Try It Yourself

See how entities work. The left side has the entity codes, the right side shows what they become.

HTML Code
Result

Things to try:

  • Look at the first paragraph - the <p> breaks the page! The entity version works
  • Add more stars to the rating - make it a 10-star review!
  • Try using &nbsp; to add extra spaces between words
  • Add more checkmarks and cross marks for your own to-do list

Practical Example

Here is how entities are used in real pages - for copyright notices, ratings, menus, and showing code examples.

HTML Code
Result

Notice how entities are used in real-world ways:

  • Star ratings using &#9733; (filled star) and &#9734; (empty star)
  • &nbsp; to align the menu prices
  • &lt; and &gt; to show HTML tags as text
  • &copy; for the copyright notice at the bottom

Challenge

Create a review page for a place in your area - a shop, a school, a restaurant, or a market. Include:

  • A heading with the name and a &trade; or &reg; symbol
  • At least three reviews with star ratings using &#9733; and &#9734;
  • A simple price menu using &nbsp; for spacing
  • A section that teaches something about HTML - write tag names using &lt; and &gt;
  • A copyright footer at the bottom using &copy;
  • At least two checkmarks &#10003; and one cross mark &#10007;

Bonus: Try finding more entity codes online! There are hundreds - from emojis to mathematical symbols.

What You Learned

  • HTML entities are special codes for characters: &name; or &#number;
  • You must use entities for: &lt; (<), &gt; (>), &amp; (&)
  • &nbsp; creates a space the browser will not collapse
  • Entities let you show symbols not on your keyboard: © ♥ ★ →
  • Named entities (&hearts;) and numbered entities (&#9829;) both work

Module 2 Complete!

You now know how to work with all kinds of text in HTML:

  1. Headings - h1 through h6 for titles and structure
  2. Paragraphs - p, br, and pre for text blocks and line breaks
  3. Formatting - strong, em, mark, del, ins, sub, sup for styled text
  4. Quotations - blockquote, q, cite, abbr, address for references
  5. Entities - special codes for symbols and reserved characters

In Module 3, you will learn about links - how to connect pages together. This is what makes the web a web!

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Links

The Simple Version

Links are what make the web a web. Without links, every page would be alone - an island with no bridges to other pages.

A link is text (or an image) that you can click to go to another page. You have been clicking links your whole life on the internet. Now you will learn how to create them.

Links are made with the <a> tag (the "a" stands for anchor). You give it an href attribute that tells it where to go:

<a href="https://google.com">Go to Google</a>
  • <a> = the link element (anchor)
  • href = where the link goes (the destination)
  • The text between the tags = what the user sees and clicks
Think of a link like a road sign in your town. The sign says "Isiolo 120km →" and points in a direction. The sign itself is not Isiolo - it just tells you how to get there. A link is the same: the text is the sign, and the href is the road to the destination.

What's Really Happening

When the browser sees an <a> tag with an href, it does several things:

  1. It makes the text clickable
  2. It changes the text color (usually to blue) and adds an underline
  3. It changes the cursor to a pointing hand when you hover over it
  4. When clicked, it sends the browser to the URL in the href

Types of links you can create:

  • External links - to other websites: href="https://google.com"
  • Internal links - to other pages on your site: href="about.html"
  • Anchor links - to a specific section on the same page: href="#section-name"
  • Email links - opens the user's email app: href="mailto:[email protected]"
  • Phone links - on mobile, starts a call: href="tel:+254700123456"

The target attribute:

By default, clicking a link replaces the current page. If you want the link to open in a new tab instead, add target="_blank":

<a href="https://google.com" target="_blank">Opens in new tab</a>

The title attribute:

You can add a title that shows as a tooltip when someone hovers over the link.

Good to know: The text inside a link is called link text or anchor text. It should describe where the link goes. Writing "click here" is bad practice - instead write what the user will find. "Read about Marsabit" is much better than "click here."

Try It Yourself

Try different types of links. Some will work in the preview, some will only work fully in a real browser.

HTML Code
Result

Things to try:

  • Click the anchor links (#food, #contact) - they jump to that section on the page!
  • Hover over the Wikipedia link to see the title tooltip
  • Notice how anchor links use id on the target element and #id in the href
  • Change the link text - the destination stays the same, only the visible text changes

Building a Navigation Menu

Links are used to build navigation menus - the buttons at the top of every website. Here is a simple example:

HTML Code
Result

This is a real mini-website! Notice how:

  • The navigation at the top links to sections below using anchor links
  • The counties list links to sub-sections
  • "Back to top" takes you back up
  • Email and phone links use mailto: and tel:

Challenge

Build a "Guide to Your Area" page. Include:

  • A navigation menu at the top with at least 4 anchor links
  • Sections for: Welcome, Places to Visit, Food, Contact
  • At least one external link (to Wikipedia or Google Maps)
  • An email link and a phone link
  • A "Back to top" link at the bottom
  • Use target="_blank" on external links so they open in new tabs
  • Good link text - no "click here"! Describe where the link goes

This page could be the start of a real website for your area!

What You Learned

  • Links use the <a> tag with href to define the destination
  • External links: href="https://..." go to other websites
  • Internal links: href="page.html" go to other pages on your site
  • Anchor links: href="#id" jump to a section on the same page
  • Email/phone: href="mailto:..." and href="tel:..."
  • target="_blank" opens links in a new tab
  • Write descriptive link text - not "click here"
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

File Paths

The Simple Version

When you link to another page or add an image, you need to tell the browser where to find it. That is what a file path does - it is the address of a file.

Think about giving someone directions in your town. You could say:

  • "Go to the shop next door" - this is a relative direction (from where you are now)
  • "Go to Mama Halima's Shop, Main Street, Isiolo, Kenya" - this is an absolute direction (the full address from anywhere)

File paths work the same way:

  • Relative path: about.html - "the file next to me"
  • Absolute path: https://example.com/about.html - "the full address on the internet"
Imagine your website is a village. Each house is a file. If you are standing at one house and want to visit another, you say "go to the house next to me" (relative) or you give the GPS coordinates (absolute). Inside your village, relative directions are easier. When talking to someone from another village, you need the full address.

What's Really Happening

Files on a website are organized in folders, just like files on your computer. Here is a typical website structure:

my-website/
  index.html
  about.html
  contact.html
  images/
    logo.png
    photo.jpg
  pages/
    team.html
    history.html
  css/
    styles.css

Relative paths:

These describe where a file is relative to the current file.

  • about.html - a file in the same folder as the current file
  • images/photo.jpg - a file inside a subfolder called "images"
  • ../index.html - go up one folder, then find index.html
  • ../../styles.css - go up two folders, then find styles.css

The .. means "go up one level" - like saying "leave this room and go to the hallway."

Absolute paths:

These are full addresses that work from anywhere:

  • https://example.com/about.html - the complete URL
  • /about.html - from the root of the website (starts with /)

When to use which?

  • Relative paths for linking to files on your own website - they still work if you move your website to a different server
  • Absolute paths for linking to other websites - you need the full address

Good to know: The most confusing part for beginners is ../ (go up one folder). Just remember: every ../ takes you one step up in the folder structure, like climbing stairs.

Try It Yourself

Let us practice file paths by building a mini website with multiple pages. Imagine this folder structure:

website/
  index.html      (you are here)
  about.html
  images/
    sunset.jpg
  pages/
    contact.html
HTML Code
Result

The links will not actually go anywhere in this preview, but look at the paths to understand the pattern.

Visual Guide

Here is a practical example showing where you are and where you want to go:

HTML Code
Result

Challenge

Imagine you have this website structure. Write the correct file paths for each link:

my-site/
  index.html
  about.html
  counties/
    turkana.html
    marsabit.html
    images/
      turkana-lake.jpg
  css/
    styles.css
  • From index.html, link to turkana.html - what is the path?
  • From turkana.html, link back to index.html - what is the path?
  • From turkana.html, show turkana-lake.jpg - what is the path?
  • From marsabit.html, link to turkana.html - what is the path?
  • From turkana.html, link to styles.css - what is the path?

Write your answers in the editor above and check them.

What You Learned

  • Relative paths describe location from the current file: about.html, images/photo.jpg
  • ../ means go up one folder
  • Absolute paths are full addresses: https://example.com/page.html
  • Use relative paths for your own site, absolute paths for external sites
  • Files are organized in folders just like on your computer
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

How URLs Work

The Simple Version

Every page on the internet has an address. Just like every house has a physical address, every web page has a URL. You type it in the browser's address bar to visit that page.

URL stands for Uniform Resource Locator. Big name, simple idea: it is just the address of something on the internet.

Here is a URL broken into parts:

https://www.example.com/schools/turkana.html
  • https:// - how to connect (securely)
  • www.example.com - which computer to talk to
  • /schools/turkana.html - which page on that computer
A URL is like a postal address. "Kenya" is like the domain name - it tells you which country. "Isiolo County" narrows it down. "P.O. Box 45" is the specific location. A URL works the same: the domain is the country, the path is the specific mailbox.

What's Really Happening

Let us break down a complete URL piece by piece:

https://www.example.com:443/schools/turkana.html?year=2026&county=turkana#location

1. Protocol: https://

This tells the browser how to communicate with the server. https means the connection is encrypted (secure). http is the older, unsecure version. Always use https when you can.

2. Domain name: www.example.com

This is the name of the website. Behind the scenes, the browser looks up this name and finds the actual computer (server) that hosts the website. It is like looking up a phone number in a contact list - you search by name, the phone dials the number.

3. Port: :443

This is usually hidden. It is like a door number on the server. Port 443 is for HTTPS, port 80 is for HTTP. You almost never need to type this.

4. Path: /schools/turkana.html

This tells the server which specific page you want. It matches the folder structure on the server, just like file paths we learned in the previous lesson.

5. Query string: ?year=2026&county=turkana

Extra information sent to the page. It starts with ? and uses & to separate multiple values. Websites use this to filter or customize content.

6. Fragment: #location

This jumps to a specific section on the page. You learned this in Lesson 12 - anchor links! It is the same thing, just part of a URL.

Good to know: When you search on Google, look at the URL. You will see something like google.com/search?q=learn+html. The ?q=learn+html part is the query string - it tells Google what you searched for. Now you know the secret of Google URLs!

Try It Yourself

Let us build links with different types of URLs and see what each part does.

HTML Code
Result

Look at the diagram at the top of the preview. Each part of the URL has a name and a job. Try clicking the fragment links to jump between sections.

Good URL Practices

HTML Code
Result

Challenge

Look at these URLs and identify each part:

  1. https://www.google.com/search?q=kenya+weather
  2. https://en.wikipedia.org/wiki/Turkana_County#Geography
  3. https://example.com/schools/isiolo/primary.html

For each one, write down:

  • What is the protocol?
  • What is the domain?
  • What is the path?
  • Is there a query string? What does it contain?
  • Is there a fragment?

Then build a page with links that include all parts of a URL: protocol, domain, path, and fragment.

What You Learned

  • A URL is the address of a page on the internet
  • Parts: protocol (https://), domain (example.com), path (/page.html), query (?key=value), fragment (#section)
  • Always use HTTPS for security
  • Use lowercase, hyphens instead of spaces, and meaningful names in URLs
  • The query string sends extra data, the fragment jumps to a section

Module 3 Complete!

You now understand how the web connects together:

  1. Links connect pages to each other using the <a> tag
  2. File paths tell the browser where to find files - relative or absolute
  3. URLs are the addresses of everything on the internet

In Module 4, you will learn about images and media - how to add pictures, video, and audio to your pages. This is where your pages start to really come alive.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Images

The Simple Version

A website without images is like a book without pictures - it works, but it is not very exciting. Images make your pages come alive.

To add an image, you use the <img> tag. It is an empty element - remember those from Lesson 4? It has no closing tag because there is no content between tags. The image itself comes from the src attribute.

<img src="photo.jpg" alt="A description of the photo">

Two attributes are essential:

  • src - the source, where the image file is (using file paths from Lesson 13)
  • alt - alternative text, a description of the image in words
The <img> tag is like a picture frame with a label. The src tells the browser which photo to put in the frame. The alt is a description written on the back - if someone cannot see the photo (maybe it did not load, maybe they are blind), they can read the description instead.

What's Really Happening

When the browser reads an <img> tag, here is what happens:

  1. It sees the src attribute and sends a request to get that image file
  2. While waiting for the image, it reserves space on the page (if you set width and height)
  3. When the image arrives, it displays it in that space
  4. If the image fails to load, it shows the alt text instead

Important: the image is NOT inside your HTML file. Your HTML file just has a reference to the image - like a pointer saying "go get this file." The image is a separate file.

Image formats:

  • .jpg / .jpeg - best for photographs. Good quality, small file size
  • .png - best for logos, icons, and images with transparent backgrounds
  • .gif - for simple animations (moving images)
  • .svg - for icons and logos that need to scale to any size without getting blurry
  • .webp - modern format, smaller files, good quality. Not all old browsers support it

Sizing images:

You can control the size with width and height attributes (in pixels) or with CSS styles:

<img src="photo.jpg" alt="Lake Turkana" width="400" height="300">

<img src="photo.jpg" alt="Lake Turkana" style="width: 100%;">

Good to know: The alt attribute is not optional! It is required for accessibility. Screen readers read the alt text to blind users. If you skip it, blind users will not know what the image shows. Also, Google uses alt text to understand your images. Always write a clear, honest description.

Try It Yourself

We will use placeholder images from the internet so you can see real images in the preview. Try changing the sizes and alt text.

HTML Code
Result

Things to try:

  • Change the numbers in picsum.photos/400/250 to change the image size
  • Add style="border-radius: 50%;" to an img tag to make it circular!
  • Add style="border: 3px solid black;" to give it a border
  • Look at the broken image - the alt text is what people see when images do not load

Images as Links

You can make an image clickable by wrapping it inside an <a> tag.

HTML Code
Result

Challenge

Create a photo gallery page for your area. Include:

  • A heading: "Beautiful [Your County]"
  • At least 3 images using https://picsum.photos/WIDTH/HEIGHT?random=N (change N for each image)
  • Every image must have a meaningful alt description in both languages
  • One image that is full width using style="width: 100%;"
  • One clickable image that links somewhere
  • One image with a <figure> and <figcaption>
  • Try making one image circular with style="border-radius: 50%;"

What You Learned

  • <img> adds images - it is an empty element with no closing tag
  • src = where the image is, alt = description of the image
  • alt is required for accessibility!
  • Control size with width/height attributes or CSS style
  • Wrap <img> in <a> to make clickable images
  • Use <figure> and <figcaption> for images with captions
  • Image formats: JPG for photos, PNG for logos, GIF for animations, SVG for icons
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Favicon

The Simple Version

Look at the tabs in your browser right now. Each tab has a small icon next to the page title. That small icon is called a favicon (short for "favourite icon").

Google has a colourful "G". YouTube has a red play button. Every professional website has one. It helps users find your tab when they have many tabs open.

A favicon is a very small image - usually 16x16 or 32x32 pixels. You add it in the <head> section of your HTML using a <link> tag:

<link rel="icon" type="image/x-icon" href="favicon.ico">
A favicon is like a sticker on the front of a notebook. When you have many notebooks on a shelf, the sticker helps you quickly find the right one without reading every title. Favicons do the same for browser tabs.

What's Really Happening

When the browser loads your page, it looks in the <head> for a <link> tag with rel="icon". If it finds one, it downloads that small image and shows it in the browser tab.

The <link> tag explained:

  • rel="icon" - tells the browser "this is an icon for the page"
  • type="image/x-icon" - the file format (for .ico files)
  • href="favicon.ico" - where to find the icon file

Favicon formats:

  • .ico - the traditional format, works everywhere
  • .png - modern alternative, use type="image/png"
  • .svg - scalable, looks sharp on any screen

How to create a favicon:

  1. Create or find a simple icon image (square, 32x32 or 64x64 pixels)
  2. Save it as favicon.ico or favicon.png
  3. Put it in your website's root folder (next to index.html)
  4. Add the <link> tag in your <head>

You can also use multiple sizes for different devices:

<link rel="icon" type="image/png" sizes="32x32" href="favicon-32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16.png">
<link rel="apple-touch-icon" sizes="180x180" href="apple-icon.png">

Good to know: If you do not add a favicon, browsers will look for a file called favicon.ico in your root folder automatically. If it does not find one, it shows a generic icon or nothing. Professional sites always have one.

Try It Yourself

Here is a complete HTML page with a favicon. The favicon will not show in our small preview, but the code shows exactly how it is done on a real website.

HTML Code
Result

Challenge

This is a short lesson, so the challenge is about applying what you know. Create a complete HTML page that has:

  • A proper <!DOCTYPE html> and full document structure
  • A <title> in the head
  • A favicon <link> in the head
  • At least one image in the body
  • A heading and two paragraphs in both languages

This is exactly what a real web page looks like - head with title and favicon, body with content.

What You Learned

  • A favicon is the small icon in the browser tab
  • Add it with <link rel="icon" href="favicon.png"> in the <head>
  • Common formats: .ico, .png, .svg
  • Size: 16x16 or 32x32 pixels, always square
  • Every professional website should have one
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Video

The Simple Version

Before HTML5, putting a video on a website was complicated - you needed special plugins like Flash. Now it is simple. You just use the <video> tag.

<video src="my-video.mp4" controls></video>

That is it. The browser gives you a video player with play, pause, volume, and fullscreen buttons - all for free.

Important attributes:

  • src - the video file location
  • controls - shows play/pause/volume buttons (without this, the user cannot control the video!)
  • width - how wide the video player should be
Think of the <video> tag like a TV set. You plug in the source (the video file), and the browser builds the TV around it - with buttons to play, pause, and adjust volume. You provide the content, the browser provides the player.

What's Really Happening

The <video> element creates a media player in the browser. It can handle different video formats and has many useful attributes:

Essential attributes:

  • controls - shows the player controls. Always include this!
  • width and height - set the player size in pixels
  • poster - an image to show before the video plays (like a thumbnail)
  • autoplay - starts playing automatically (most browsers block this unless muted is also set)
  • muted - starts with sound off
  • loop - repeats the video when it ends

Video formats:

  • .mp4 - works in all modern browsers. Use this.
  • .webm - good quality, smaller files, but not all browsers support it
  • .ogg - older format, rarely used now

Multiple sources:

You can provide multiple formats so different browsers can pick the one they support:

<video controls width="400">
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  Your browser does not support video.
</video>

The browser tries each source from top to bottom and uses the first one it can play. The text at the bottom shows only if the browser cannot play any of them.

Embedding YouTube videos:

You cannot put a YouTube URL directly in a <video> tag. Instead, YouTube gives you an <iframe> code to embed their player. You will learn about iframes later.

Good to know: Most browsers will not autoplay videos with sound - it is annoying for users. If you need autoplay, you must also add muted. This is a browser rule to protect users from noisy pages.

Try It Yourself

We will use a free sample video to demonstrate. Try changing the attributes to see how they affect the player.

HTML Code
Result

Things to try:

  • Remove the controls attribute - can you still play the video?
  • Change the width to different values like 300 or 100%
  • Remove muted from the autoplay video - does it still autoplay?
  • Add loop to the first video - it will repeat endlessly

Challenge

Create a page that could be a community video gallery. Include:

  • A heading: "Community Videos
  • A main video with controls, width="100%", and a poster image
  • A description of the video in both languages below it
  • A smaller video (width 300) with autoplay, muted, and loop
  • Fallback text for browsers that do not support video

What You Learned

  • <video> creates a video player in the browser
  • Always add controls so users can play/pause
  • poster shows an image before the video plays
  • autoplay needs muted to work in most browsers
  • loop repeats the video endlessly
  • Use <source> tags for multiple formats, .mp4 is safest
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Audio

The Simple Version

If you understood the <video> tag from the last lesson, you already understand <audio>. It works almost the same way - same attributes, same logic.

<audio src="song.mp3" controls></audio>

The browser creates a simple audio player with play, pause, and volume controls. The only difference from video: there is nothing to see, only to hear.

Key attributes (same as video):

  • controls - shows the play/pause/volume buttons. Always include this
  • autoplay - starts playing automatically (browsers usually block this)
  • muted - starts with sound off
  • loop - repeats when it ends
If <video> is a TV, then <audio> is a radio. Same idea - you give it a source file, and the browser builds a player around it. The only difference is there is no picture, just sound.

What's Really Happening

The <audio> element works identically to <video> behind the scenes. The browser downloads the audio file and creates a player widget.

Audio formats:

  • .mp3 - works everywhere, most common. Use this
  • .ogg - open source format, good quality
  • .wav - high quality but very large files. Best for short sounds

Multiple sources:

Just like video, you can provide multiple formats:

<audio controls>
  <source src="song.mp3" type="audio/mpeg">
  <source src="song.ogg" type="audio/ogg">
  Your browser does not support audio.
</audio>

When to use audio:

  • Music on a page
  • Podcasts or recorded talks
  • Language learning - pronunciation guides.
  • Sound effects on a game or app

Good to know: Do not autoplay audio on your website! It is one of the most annoying things on the internet. Always let the user choose when to play audio by including controls. Respect your visitors.

Try It Yourself

Here is a page with audio players. The sample audio comes from the web.

HTML Code
Result

Things to try:

  • Click play on the audio players to hear the sound
  • Remove controls - the player disappears completely! You cannot hear or see it
  • Add loop and listen to it repeat

Building a Podcast Page

Here is a realistic example of how audio is used on a real website - a podcast or radio show page.

HTML Code
Result

Challenge

Create a community radio page or a language learning page. Include:

  • A heading with the name of your show or course
  • At least two audio sections with descriptions
  • Each audio should have controls and style="width: 100%;"
  • Use <small> for dates and duration
  • Descriptions in both English and Kiswahili
  • A contact section with an email link

What You Learned

  • <audio> creates an audio player - works just like <video>
  • Always add controls - without it, users cannot play the audio
  • .mp3 is the safest format - works everywhere
  • Never autoplay audio - it annoys users!
  • Use <source> for multiple format support

Module 4 Complete!

You now know how to add all types of media to your web pages:

  1. Images - with <img>, alt text, and sizing
  2. Favicons - the small icon in the browser tab
  3. Video - with <video>, controls, poster, and autoplay
  4. Audio - with <audio>, controls, and loop

Your pages can now have text, links, images, video, and audio. That covers most of what you see on the web! In Module 5, you will learn about lists and tables - how to organize information in structured ways.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Unordered Lists

The Simple Version

An unordered list is a list of items where the order does not matter. Think of a shopping list - it does not matter if you buy milk before bread or bread before milk.

<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li>Eggs</li>
</ul>

Two tags work together:

  • <ul> - the list container (ul = unordered list)
  • <li> - each item in the list (li = list item)

The browser automatically adds a bullet point before each item.

Think of <ul> like a bag. The bag holds items, but the items inside have no particular order. Each <li> is something you put in the bag. The bag is the <ul>, the things inside are the <li> elements.

What's Really Happening

The <ul> element tells the browser: "here is a group of items that belong together, but their order is not important." The browser responds by displaying each <li> with a bullet point and some indentation.

Rules:

  • The <ul> tag can only contain <li> elements as direct children
  • But inside each <li>, you can put anything - text, links, images, even another list!
  • Every <li> needs a closing </li> tag

Nested lists:

You can put a list inside a list item. This creates a sub-list with a different bullet style:

<ul>
  <li>Fruits - Matunda
    <ul>
      <li>Mango - Embe</li>
      <li>Banana - Ndizi</li>
    </ul>
  </li>
  <li>Vegetables - Mboga</li>
</ul>

The browser automatically changes the bullet style for nested lists: filled circle → empty circle → square. This helps users see which level they are on.

Lists with links:

Many navigation menus on websites are actually unordered lists with links inside:

<ul>
  <li><a href="/">Home</a></li>
  <li><a href="/about">About</a></li>
  <li><a href="/contact">Contact</a></li>
</ul>

Good to know: The bullet points come from CSS, not HTML. Later when you learn CSS, you can change the bullets to squares, dashes, custom images, or remove them entirely. For now, just know that <ul> means "unordered list" and the browser handles the styling.

Try It Yourself

Here is a page with different types of unordered lists. Edit the code and see what happens.

HTML Code
Result

Things to try:

  • Add more items to the shopping list
  • Add a third level of nesting inside "Matunda"
  • Put an <img> tag inside an <li> - images work inside list items!
  • Remove the <ul> tags but keep the <li> tags - what happens?

Real World Example: School Supplies List

Here is how a school might list what students need for different classes.

HTML Code
Result

Challenge

Create a page about your favorite things. Include:

  • A main heading for the page
  • At least 3 different unordered lists with different topics (food, music, places, etc.)
  • Each list should have at least 4 items
  • At least one list should have nesting (sub-items)
  • At least one list item should contain a link
  • Labels in both English and Kiswahili

What You Learned

  • <ul> creates an unordered (bulleted) list
  • <li> creates each list item
  • You can nest lists inside list items for sub-categories
  • List items can contain links, images, and other HTML elements
  • Navigation menus are often built with <ul> and <li>
  • The bullet style changes automatically for nested levels
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Ordered Lists

The Simple Version

An ordered list is a list where the order matters. Think of a recipe - you need to follow the steps in the right order, or the food will not come out right.

<ol>
  <li>Boil water</li>
  <li>Add tea leaves</li>
  <li>Add milk and sugar</li>
</ol>

It works exactly like <ul>, but instead of bullet points you get numbers. The browser counts for you automatically - you do not need to type the numbers yourself.

  • <ol> - the list container (ol = ordered list)
  • <li> - each item (same tag as unordered lists!)
If <ul> is a bag where items have no order, then <ol> is a queue. The first person is number 1, the second is number 2. The position matters. You would not want to be number 10 in a queue if you arrived first!

What's Really Happening

The <ol> element tells the browser: "these items have a specific sequence." The browser numbers them automatically. But you can control the numbering with attributes.

Useful attributes:

  • type - changes the numbering style:
    • type="1" - numbers (default): 1, 2, 3...
    • type="A" - uppercase letters: A, B, C...
    • type="a" - lowercase letters: a, b, c...
    • type="I" - Roman numerals: I, II, III...
    • type="i" - lowercase Roman: i, ii, iii...
  • start - start counting from a different number: start="5" starts at 5
  • reversed - counts backwards: 3, 2, 1...

The value attribute on <li>:

You can set a specific number for any list item, and the following items will continue from there:

<ol>
  <li>First</li>
  <li value="5">Jumps to five</li>
  <li>Continues at six</li>
</ol>

When to use <ol> vs <ul>:

  • <ol> - steps, instructions, rankings, anything with sequence
  • <ul> - shopping lists, features, options, anything without sequence

Good to know: The <li> tag is the same for both <ol> and <ul>. The parent tag (<ol> or <ul>) decides whether items get numbers or bullets.

Try It Yourself

Here are different ordered lists with various numbering styles. Experiment with the attributes!

HTML Code
Result

Things to try:

  • Change type="A" to type="a" or type="i"
  • Change the start value to different numbers
  • Add reversed to the chai recipe - does it make sense backwards?
  • Add value="10" to a middle item and see what happens to the count

Real World Example: Exam Results

Here is how a school might display exam results with rankings.

HTML Code
Result

Challenge

Create a recipe page for your favorite dish. Include:

  • A heading with the dish name in English and Kiswahili
  • An unordered list of ingredients (order does not matter)
  • An ordered list of cooking steps (order matters!)
  • Use type="A" or type="I" for at least one list
  • Add a "Top 3 Tips" ordered list at the bottom
  • Descriptions in both languages

What You Learned

  • <ol> creates an ordered (numbered) list
  • <li> is the same tag used in both <ul> and <ol>
  • type changes the numbering style (1, A, a, I, i)
  • start sets where counting begins
  • reversed counts backwards
  • Use <ol> when order matters, <ul> when it does not
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Description Lists

The Simple Version

You know <ul> for bullet lists and <ol> for numbered lists. There is a third type: the description list. It pairs a term with its description. Think of a dictionary - each word has a definition next to it.

<dl>
  <dt>HTML</dt>
  <dd>The language used to build web pages.</dd>

  <dt>CSS</dt>
  <dd>The language used to style web pages.</dd>
</dl>

Three tags work together:

  • <dl> - the list container (description list)
  • <dt> - the term (description term) - the word or name
  • <dd> - the description (description definition) - the explanation
Think of a description list like a Kiswahili-English dictionary. Each entry has the word (that is the <dt>) and its meaning (that is the <dd>). The dictionary itself is the <dl>.

What's Really Happening

The browser displays each <dt> on its own line, and indents each <dd> below it. This creates a clear visual connection between a term and its meaning.

Multiple descriptions per term:

A term can have more than one description.

<dl>
  <dt>Maji</dt>
  <dd>Water (English)</dd>
  <dd>Eau (French)</dd>
  <dd>Agua (Spanish)</dd>
</dl>

Multiple terms for one description:

You can also have multiple terms that share one description:

<dl>
  <dt>Nyumbani</dt>
  <dt>Home</dt>
  <dd>The place where you live and feel safe.</dd>
</dl>

When to use description lists:

  • Glossaries and dictionaries
  • FAQ pages - term is the question, description is the answer
  • Product details - term is the feature name, description is the value
  • Contact info - term is the label (Phone, Email), description is the value

Good to know: Description lists are less common than <ul> and <ol>, but they are the right choice when you have pairs of information. Using the right list type makes your HTML more meaningful and easier for screen readers to understand.

Try It Yourself

Here are different uses of description lists. Edit the code to see how they work.

HTML Code
Result

Things to try:

  • Add more terms to the glossary
  • Add a second <dd> to give a word two meanings
  • Try putting a link inside a <dd> - for example, make the email clickable with mailto:
  • Swap <dl> for <ul> - notice how the structure breaks

Real World Example: Product Specifications

Here is how an online shop might display product details.

HTML Code
Result

Challenge

Create a page that uses all three list types together. Include:

  • A <dl> glossary with at least 5 Kiswahili tech terms and their English definitions
  • A <ul> list of your favorite websites
  • An <ol> list of steps to do something (e.g., how to create an email account)
  • At least one <dt> that has multiple <dd> descriptions
  • Both English and Kiswahili throughout

What You Learned

  • <dl> creates a description list for term-definition pairs
  • <dt> is the term (the word or label)
  • <dd> is the description (the meaning or value)
  • A term can have multiple descriptions, and vice versa
  • Great for glossaries, FAQs, specs, and contact info
  • You now know all three HTML list types: <ul>, <ol>, <dl>
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Tables

The Simple Version

Tables organize data into rows and columns - like a spreadsheet. When you have information that makes sense in a grid, use a table.

<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Amina</td>
    <td>25</td>
  </tr>
  <tr>
    <td>John</td>
    <td>30</td>
  </tr>
</table>

Four tags build a table:

  • <table> - the table container
  • <tr> - a table row (table row).
  • <th> - a header cell (table header). Bold and centered by default
  • <td> - a data cell (table data). Normal text
Think of a table like a register book in class. Each row is a student. Each column is information about them - name, admission number, age. The top row has the column headings. That is exactly how HTML tables work.

What's Really Happening

You build a table row by row, from top to bottom. Each <tr> creates a new row. Inside each row, you use <th> for headers or <td> for data. Every row should have the same number of cells.

Table structure tags:

For better organization, you can group rows into sections:

<table>
  <thead>
    <tr><th>Name</th><th>Score</th></tr>
  </thead>
  <tbody>
    <tr><td>Amina</td><td>95</td></tr>
    <tr><td>John</td><td>88</td></tr>
  </tbody>
</table>
  • <thead> - wraps the header row(s)
  • <tbody> - wraps the data rows
  • <tfoot> - wraps the footer row(s), like totals

Spanning cells:

Sometimes a cell needs to stretch across multiple columns or rows:

  • colspan="2" - cell spans 2 columns (stretches sideways)
  • rowspan="2" - cell spans 2 rows (stretches downward)
<tr>
  <th colspan="2">Full Name</th>
</tr>
<tr>
  <td>First</td>
  <td>Last</td>
</tr>

The <caption> tag:

Add a title to your table that appears above it:

<table>
  <caption>Student Results - Matokeo ya Wanafunzi</caption>
  ...
</table>

Important: Tables are for data, not for page layout! In the early days of the web, people used tables to create page layouts. This is wrong. Use CSS for layout (you will learn this later). Tables should only contain tabular data - information that belongs in rows and columns.

Try It Yourself

Here are tables with different features. The inline styles add borders so you can see the cells clearly.

HTML Code
Result

Things to try:

  • Add more students to the class register
  • Add a new column (e.g., "House" or "Gender")
  • Change colspan="3" to colspan="2" - notice the table breaks!
  • Remove the <style> block - see how the table looks without borders
  • Add rowspan="2" to a cell and see it stretch down

Real World Example: Price List

Here is a price list for a small business.

HTML Code
Result

Challenge

Create a school timetable page. Include:

  • A <caption> with the class name
  • A full day timetable with columns: Time, Monday, Tuesday, Wednesday
  • Use <thead>, <tbody>, and <tfoot>
  • Use colspan for the break/lunch period (it spans all day columns)
  • At least 5 time slots
  • Add inline <style> for borders and padding
  • Labels in both English and Kiswahili

What You Learned

  • <table> creates a data table with rows and columns
  • <tr> creates a row, <th> creates a header cell, <td> creates a data cell
  • <thead>, <tbody>, <tfoot> organize table sections
  • colspan and rowspan merge cells across columns or rows
  • <caption> adds a title above the table
  • Tables are for data only, not for page layout!

Module 5 Complete!

You now know how to organize information using all the list and table tools HTML offers:

  1. Unordered Lists - bullet points for items with no sequence. <ul>
  2. Ordered Lists - numbered items where order matters. <ol>
  3. Description Lists - term and definition pairs. <dl>
  4. Tables - data in rows and columns. <table>

Your pages can now present information in clear, structured ways. In Module 6, you will learn about structure and layout - how to organize the bigger picture of your page with divs, classes, IDs, and semantic HTML.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Block vs Inline Elements

The Simple Version

Every HTML element is either block or inline. This is one of the most important concepts in HTML.

Block elements take up the full width of the page. They always start on a new line. Think of them like boxes stacked on top of each other.

Inline elements only take up as much space as they need. They sit next to each other on the same line. Think of words in a sentence.

Block elements are like paragraphs in a book - each one starts on a new line and stretches across the page. Inline elements are like bold or italic words inside a paragraph - they flow along with the text without breaking the line.

What's Really Happening

Common block elements:

  • <h1> to <h6> - headings
  • <p> - paragraphs
  • <div> - generic container (you will learn this next)
  • <ul>, <ol>, <li> - lists
  • <table> - tables
  • <hr> - horizontal line
  • <form> - forms (you will learn later)

Common inline elements:

  • <a> - links
  • <strong>, <em> - bold and italic
  • <span> - generic inline container (you will learn this next)
  • <img> - images
  • <code> - code text
  • <br> - line break
  • <small> - small text

Key differences:

Block Inline
Starts on a new line Stays on the same line
Takes full width available Takes only the space it needs
Can contain block and inline elements Can only contain other inline elements

Important rule: You should not put block elements inside inline elements. For example, do not put a <p> inside an <a> (though HTML5 does allow <a> to wrap block elements in some cases). When in doubt, keep inline elements inside block elements.

Try It Yourself

See the difference between block and inline elements. The colored backgrounds make it easy to see how much space each element takes.

HTML Code
Result

Things to try:

  • Remove the background colors - the elements still behave the same way
  • Put two <strong> tags next to each other - they stay on one line
  • Put two <p> tags next to each other - they each get their own line
  • Try putting an <h2> inside a <p> - see what happens!

Real World Example: Understanding Page Flow

Here is how block and inline elements work together on a real page.

HTML Code
Result

Challenge

Create a page about your town or city. Use both block and inline elements:

  • Use block elements: <h1>, <h2>, <p>, <ul>, <hr>
  • Use inline elements: <strong>, <em>, <a>, <code>, <small>
  • Add background colors to some elements to show which are block and which are inline
  • Write in both English and Kiswahili

What You Learned

  • Every HTML element is either block or inline
  • Block elements start on a new line and take full width
  • Inline elements stay on the same line and take only the space they need
  • Block elements can contain inline elements, but not the other way around
  • This concept is fundamental to understanding how CSS layouts work later
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Div and Span

The Simple Version

Sometimes you need to group elements together or target a piece of text, but there is no specific HTML tag for what you want. That is what <div> and <span> are for.

  • <div> - a block container. Groups things together.
  • <span> - an inline container. Wraps a piece of text.

By themselves, they do nothing visible. They have no style, no color, no special meaning. But they become powerful when you add CSS styles to them.

Think of <div> like a cardboard box. The box itself is invisible - it has no color or design. But you can put things inside it, label it, move it around, and paint it. <span> is like a highlighter pen - it marks a piece of text so you can style it differently from the rest.

What's Really Happening

The <div> element:

<div> stands for "division." It is the most used element in web development. It creates a block-level container that you can use to:

  • Group related content together
  • Create sections of a page
  • Apply styles to a group of elements at once
  • Create layout structures (columns, cards, headers, footers)
<div style="background: #f0f0f0; padding: 20px;">
  <h2>This section has a gray background</h2>
  <p>Everything inside this div gets the same background.</p>
</div>

The <span> element:

<span> is the inline version of <div>. Use it when you want to style a piece of text inside a paragraph without breaking the line:

<p>The price is <span style="color: red;">KSh 500</span> today.</p>

When to use each:

  • <div> - when you need to group block-level content (sections, cards, columns)
  • <span> - when you need to style a word or phrase inside a line of text

Good to know: <div> and <span> are "generic" elements - they have no semantic meaning. Later you will learn about semantic elements like <header>, <nav>, and <section> that should be used instead when they fit. But <div> and <span> are still essential for everything else.

Try It Yourself

See how <div> and <span> are used to group and style content.

HTML Code
Result

Things to try:

  • Change the background colors of the div boxes
  • Add more content inside one of the divs
  • Change the span colors and see how only that text changes
  • Replace <span> with <div> in the price line - watch it break to a new line!

Real World Example: Product Cards

Divs are used everywhere to create "cards" on websites.

HTML Code
Result

Challenge

Create a notification center page. Include:

  • At least 3 notification divs with different colored left borders (green=success, red=error, blue=info, yellow=warning)
  • Each notification should have a heading and description
  • Use <span> to highlight important words like dates, amounts, or status
  • Add a "product card" using a div with a border and rounded corners
  • Both English and Kiswahili

What You Learned

  • <div> is a block-level container for grouping elements
  • <span> is an inline container for styling pieces of text
  • Both are generic - they have no meaning on their own.
  • They become useful when you add styles (colors, backgrounds, borders, etc.)
  • <div> is the most commonly used element in web development
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Classes

The Simple Version

In the last lesson, you added styles directly to each element using the style attribute. But what if you have 10 divs that should all look the same? Writing the same style 10 times is terrible.

The class attribute lets you give elements a name, and then style all elements with that name at once using CSS.

<style>
  .highlight { background: yellow; padding: 5px; }
</style>

<p class="highlight">This is highlighted.</p>
<p class="highlight">So is this one!</p>
<p>This one is not.</p>

Both paragraphs with class="highlight" get the yellow background. The third one does not. Write the style once, use it many times.

Think of classes like school uniforms. All students wearing the "Form 2" uniform look the same - same color, same style. The uniform name is the class. You define what the uniform looks like once, and every student who wears it gets the same look. In CSS, the class name is the "uniform name" and the styles are the "uniform design."

What's Really Happening

How classes work:

  1. You give an element a class name: <p class="warning">
  2. In CSS (inside a <style> tag), you target that class with a dot: .warning { color: red; }
  3. Every element with that class gets those styles

Rules for class names:

  • Start with a letter, not a number. box1 is fine, 1box is not
  • No spaces. Use hyphens instead: my-card not my card
  • Case sensitive: myCard and mycard are different
  • Use descriptive names: error-message is better than red-text

Multiple classes:

An element can have multiple classes, separated by spaces:

<div class="card featured large">...</div>

This element gets styles from .card, .featured, AND .large.

Same class, different elements:

Any HTML element can use any class. A <p>, <div>, <span>, and <li> can all share the same class:

<p class="important">Important paragraph</p>
<span class="important">Important word</span>
<li class="important">Important item</li>

Good to know: Classes are the foundation of CSS. Almost every website you visit uses classes to style elements. When you learn CSS later, you will use classes constantly. This is one of the most important attributes in all of HTML.

Try It Yourself

See how classes let you style multiple elements at once, and how multiple classes combine.

HTML Code
Result

Things to try:

  • Change class="card success" to class="card error" - watch it turn red
  • Remove card from a class and see what happens (no more border/padding)
  • Add the bold class to other elements
  • Create a new class called .info with a blue theme and use it

Real World Example: Dashboard

Here is a simple dashboard using classes to keep the styling clean.

HTML Code
Result

Challenge

Create a student report card page using classes. Include:

  • A <style> block with at least 5 different classes
  • A "student info" card with name, class, and admission number
  • Subject cards where grades are colored by performance (green=A, blue=B, orange=C, red=D)
  • Use multiple classes on some elements (e.g., class="subject-card grade-a")
  • A "badge" class for the overall grade
  • Both English and Kiswahili labels

What You Learned

  • The class attribute names an element so CSS can style it
  • In CSS, target a class with a dot: .classname { ... }
  • Multiple elements can share the same class - they all get the same styles
  • An element can have multiple classes separated by spaces
  • Use descriptive, lowercase names with hyphens
  • Classes are the most important tool for CSS styling
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

IDs

The Simple Version

An id is like a class, but with one big difference: an ID must be unique on the page. Only ONE element can have a specific ID. Classes can be shared by many elements, but an ID belongs to only one.

<h2 id="about">About Us</h2>

IDs have two main uses:

  • Linking to a specific part of a page - jumping to a section.
  • Targeting one specific element with CSS or JavaScript
If classes are like school uniforms (many students wear the same one), then IDs are like national ID numbers. Every person has a unique ID number - no two people share the same one. Similarly, every id on a page must be unique. If two elements have the same ID, things break.

What's Really Happening

Linking to sections with IDs:

This is the most common use of IDs. You give a section an ID, then create a link that points to it with #:

<!-- The link -->
<a href="#contact">Jump to Contact</a>

<!-- Somewhere lower on the page -->
<h2 id="contact">Contact Us</h2>

When you click the link, the page scrolls to the element with id="contact". This is called an anchor link or bookmark.

Styling with IDs in CSS:

Use a hash # instead of a dot:

<style>
  #main-title { color: blue; font-size: 2em; }
</style>

<h1 id="main-title">Welcome</h1>

ID vs Class - when to use which:

  • class - use when multiple elements need the same style.
  • id - use when targeting ONE specific element, especially for anchor links.

An element can have both:

<div id="hero" class="card featured">...</div>

Good to know: In practice, most developers prefer classes for styling and use IDs mainly for anchor links and JavaScript. IDs have higher "specificity" in CSS, which can cause problems when you try to override styles later. For now, just remember: classes for styling, IDs for linking to sections.

Try It Yourself

See how IDs create anchor links that jump to different sections of a page.

HTML Code
Result

Things to try:

  • Click the navigation links - the page jumps to each section
  • Click "Back to top" links to jump back up
  • Change an id but not its matching href - the link breaks!
  • Give two elements the same ID - this is invalid HTML, do not do this

Challenge

Create a one-page website with a table of contents at the top. Include:

  • A navigation section with at least 4 anchor links
  • 4 content sections, each with a unique id
  • "Back to top" links at the end of each section
  • Use classes for repeated styling (card borders, section padding)
  • Use IDs only for anchor navigation
  • Content in both English and Kiswahili

What You Learned

  • id gives an element a unique identifier - must be one-of-a-kind on the page
  • Link to an ID with <a href="#id-name"> - creates anchor/bookmark links
  • In CSS, target an ID with #id-name
  • Use classes for styling, IDs for navigation and JavaScript targeting
  • An element can have both an ID and classes
  • Never give two elements the same ID!
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

The Head Element

The Simple Version

Every HTML page has two main parts: the <head> and the <body>. You have been writing content in the <body> this whole time. The <head> is everything the user does not see directly - it contains information about the page.

<!DOCTYPE html>
<html>
  <head>
    <!-- Information about the page (invisible) -->
    <title>My Website</title>
  </head>
  <body>
    <!-- Content the user sees (visible) -->
    <h1>Welcome!</h1>
  </body>
</html>
Think of <head> like the back cover of a book. It contains the title, author, ISBN number, and description - important information about the book, but not the actual story. The <body> is the pages of the book - what you actually read. The <head> tells the browser and search engines what your page is about.

What's Really Happening

The <head> contains metadata (data about data). Here is what goes inside it:

1. <title> - Page title (Required!):

<title>My Shop - Buy Electronics Online</title>

This shows in the browser tab, in search results, and when you bookmark the page.

2. <meta> - Metadata tags:

<!-- Character encoding - always include this -->
<meta charset="UTF-8">

<!-- Makes the page work on mobile -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- Description for search engines -->
<meta name="description" content="Buy phones and laptops at the best prices in Kenya.">

<!-- Author -->
<meta name="author" content="Amina Hassan">

3. <link> - External resources:

<!-- CSS stylesheet -->
<link rel="stylesheet" href="styles.css">

<!-- Favicon -->
<link rel="icon" href="favicon.ico">

4. <style> - Internal CSS:

<style>
  body { font-family: Arial; }
  h1 { color: blue; }
</style>

5. <script> - JavaScript:

<script src="app.js"></script>

The meta description is very important for SEO:

When people search on Google, the description meta tag is what appears below your page title in search results. Write a clear, compelling description of 150-160 characters.

Good to know: The <meta name="viewport"> tag is essential for mobile. Without it, your page will look tiny on phones because the browser will render it at desktop width. Always include this tag in every page you create.

Try It Yourself

See a complete HTML document with a proper <head> section. Note: in our editor, the title and meta tags will not show visible effects, but this is the correct structure for any real website.

HTML Code
Result

Things to try:

  • Change the <title> - in a real browser, the tab name would change
  • Edit the CSS inside <style> to change colors and fonts
  • Change lang="en" to lang="sw" for Kiswahili
  • Remove the viewport meta tag and imagine how it would look on mobile

What Google Sees

Here is how your <head> metadata appears in Google search results.

HTML Code
Result

Challenge

Create a complete HTML page with a perfect <head> section for a local business. Include:

  • A descriptive <title> (include the business name and what they do)
  • charset and viewport meta tags
  • A description meta tag (150-160 characters)
  • An author meta tag
  • An internal <style> block with at least 3 classes
  • Body content that matches the business described in the head

What You Learned

  • <head> contains invisible information about the page
  • <title> sets the browser tab name and search result title
  • <meta charset="UTF-8"> ensures text displays correctly
  • <meta name="viewport"> makes pages mobile-friendly
  • <meta name="description"> controls your Google search preview
  • <link> connects external CSS files and favicons
  • <style> holds internal CSS rules
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Page Layout

The Simple Version

Every website has a structure: a header at the top, navigation menu, main content area, maybe a sidebar, and a footer at the bottom. This is the page layout.

In HTML, we use specific elements to define these sections:

  • <header> - the top section (logo, title)
  • <nav> - navigation links
  • <main> - the main content area
  • <aside> - sidebar content
  • <footer> - the bottom section
  • <section> - a thematic grouping
  • <article> - independent content
Think of a newspaper. It has a masthead at the top (header), a table of contents (nav), the main stories (main), side columns with ads or weather (aside), and the publisher info at the bottom (footer). Every page on the web follows this same basic pattern, just like every newspaper follows the same basic layout.

What's Really Happening

Here is the typical structure of a web page:

<body>
  <header>
    <h1>My Website</h1>
    <nav>
      <a href="/">Home</a>
      <a href="/about">About</a>
    </nav>
  </header>

  <main>
    <article>
      <h2>Article Title</h2>
      <p>Article content...</p>
    </article>
  </main>

  <aside>
    <h3>Related Links</h3>
  </aside>

  <footer>
    <p>&copy; 2026 My Website</p>
  </footer>
</body>

What each element means:

  • <header> - introductory content, usually the logo and navigation. Can appear at the top of the page or at the top of a section
  • <nav> - a set of navigation links. Use for main menus, not every link on the page
  • <main> - the central content unique to this page. Only ONE per page!
  • <article> - content that could stand alone (blog post, news story, product card)
  • <section> - a thematic group of content, usually with a heading
  • <aside> - content related to but separate from the main content (sidebar, ads, related links)
  • <footer> - closing content (copyright, contact info, social links)

Good to know: These elements look the same as <div> by default - they are all block elements with no special styling. The difference is meaning. Screen readers, search engines, and other tools use these elements to understand your page structure. This is called semantic HTML - you will learn more about it in the next lesson.

Try It Yourself

Here is a complete page layout with all the structural elements, plus some CSS to make the layout visible.

HTML Code
Result

Things to try:

  • Add more articles to the main section
  • Add more links to the nav
  • Change the header background color
  • Replace all the layout elements with <div> - it looks the same but loses meaning!

Challenge

Create a complete page layout for a school website. Include:

  • <header> with the school name and motto
  • <nav> with links: Home, About, Academics, Sports, Contact
  • <main> with at least 2 <article> elements (school news)
  • <aside> with upcoming events or announcements
  • <footer> with school address, phone, and email
  • CSS styling in a <style> block
  • Both English and Kiswahili

What You Learned

  • Web pages have a standard structure: header, nav, main, aside, footer
  • <header>, <nav>, <main>, <aside>, <footer> define page sections
  • <article> wraps standalone content, <section> groups related content
  • Only ONE <main> element per page
  • These elements look like divs but carry meaning for accessibility and SEO
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Semantic HTML

The Simple Version

"Semantic" means "having meaning." Semantic HTML means using elements that describe what the content IS, not just how it looks.

Non-semantic: <div> and <span> - they tell us nothing about the content inside them.

Semantic: <header>, <nav>, <article>, <footer> - these tell us exactly what the content is.

Imagine you are moving house and you have boxes. Non-semantic is like labeling every box "STUFF." Semantic is like labeling them "KITCHEN," "BEDROOM," "BOOKS." Both boxes hold things, but the labeled ones tell you what is inside without opening them. That is what semantic HTML does - it tells browsers, search engines, and screen readers what is inside each element.

What's Really Happening

Non-semantic (old way) vs Semantic (modern way):

<!-- OLD WAY - divs everywhere -->
<div class="header">...</div>
<div class="nav">...</div>
<div class="main">...</div>
<div class="sidebar">...</div>
<div class="footer">...</div>

<!-- MODERN WAY - semantic elements -->
<header>...</header>
<nav>...</nav>
<main>...</main>
<aside>...</aside>
<footer>...</footer>

They look the same on screen, but the semantic version is much better because:

1. Accessibility:

Screen readers (used by blind people) can navigate semantic pages much better. They can say "jump to the nav" or "skip to the main content." With divs, they cannot do this.

2. SEO - Search Engine Optimization:

Google understands your page better when you use semantic elements. It knows which content is the main article, which is the sidebar, and which is navigation.

3. Code readability:

When another developer reads your code, <nav> is immediately clear. <div class="nav"> takes more time to understand.

More semantic elements you already know:

  • <h1> to <h6> - headings (not just big text)
  • <p> - paragraph (not just text with spacing)
  • <strong> - important text (not just bold)
  • <em> - emphasized text (not just italic)
  • <ul>, <ol> - lists (not just indented text)
  • <table> - tabular data (not just a grid)

Additional semantic elements:

  • <figure> and <figcaption> - image with caption
  • <details> and <summary> - collapsible content
  • <time> - a date or time value
  • <mark> - highlighted/marked text
  • <address> - contact information

The rule is simple: If there is a semantic element that describes your content, use it instead of <div>. Only use <div> when no semantic element fits.

Try It Yourself

Compare non-semantic and semantic HTML, and try some useful semantic elements.

HTML Code
Result

Things to try:

  • Click the <details> elements to expand/collapse them - no JavaScript needed!
  • Change the <mark> text to highlight different words
  • Add more FAQ items using <details> and <summary>
  • Replace <figure> with <div> - it still works but loses meaning

Challenge

Create a blog post page using only semantic elements (no divs!). Include:

  • <header> with the blog name and <nav>
  • <main> containing an <article>
  • The article should have a heading, <time> for the date, and paragraphs
  • A <figure> with an image and <figcaption>
  • An FAQ section using <details> and <summary> (at least 3 questions)
  • Use <mark> to highlight key information
  • <footer> with an <address> element
  • Both English and Kiswahili

What You Learned

  • Semantic HTML uses elements that describe the content's meaning
  • Use semantic elements instead of <div> whenever possible
  • <figure> + <figcaption> for images with captions
  • <details> + <summary> for collapsible content (no JavaScript!)
  • <time> for dates, <mark> for highlights, <address> for contact info
  • Semantic HTML improves accessibility, SEO, and code readability
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Iframes

The Simple Version

An <iframe> (inline frame) lets you embed another web page inside your page. It is like a window to another website.

<iframe src="https://example.com" width="100%" height="400"></iframe>

This is how YouTube videos, Google Maps, and many other services appear on websites. The video player you see on a blog is actually YouTube's page loaded inside an iframe.

Think of an iframe like a picture frame on a wall. Your wall is your web page. The picture inside the frame is another web page. You can see it, interact with it, but it is not part of your wall - it lives in its own frame.

What's Really Happening

The <iframe> element creates a rectangular area on your page that loads a completely separate HTML document inside it. The embedded page has its own CSS, JavaScript, and behavior.

Common attributes:

  • src - the URL of the page to embed
  • width and height - size of the frame
  • title - describes the iframe for accessibility (always include this!)
  • frameborder="0" - removes the border (older browsers)
  • allowfullscreen - lets embedded videos go fullscreen

YouTube embed:

To embed a YouTube video, go to the video, click "Share" → "Embed", and YouTube gives you the iframe code:

<iframe
  width="560" height="315"
  src="https://www.youtube.com/embed/VIDEO_ID"
  title="Video title"
  allowfullscreen>
</iframe>

Google Maps embed:

On Google Maps, click "Share" → "Embed a map" to get the iframe code:

<iframe
  src="https://www.google.com/maps/embed?pb=..."
  width="100%" height="300"
  style="border:0;"
  title="Map location">
</iframe>

Security:

Not all websites allow you to embed them in an iframe. Many sites block this for security reasons. Facebook, Twitter, and most banks block iframe embedding. You can only embed sites that specifically allow it.

Important: Always include the title attribute on iframes for accessibility. Screen readers use this to tell blind users what the iframe contains. Also, iframes slow down your page because they load an entire separate web page. Use them only when needed.

Try It Yourself

Here are examples of iframes embedding different content. Note: some embeds may not work in the code editor's preview but will work in a real browser.

HTML Code
Result

Things to try:

  • Change the src URL to a different Wikipedia page
  • Change the width and height values
  • Remove the title attribute - the page still works, but accessibility is broken
  • Try embedding https://google.com - most browsers will block it!

Real World Example: Business Contact Page

Here is how a business might use an iframe to show their location on a map.

HTML Code
Result

Challenge

Create a media page that embeds different types of content. Include:

  • A page heading and introduction
  • An embedded Wikipedia page in an iframe
  • The YouTube embed code (even if it does not play in the editor)
  • A Google Maps embed showing a location you know
  • Each iframe must have a title attribute
  • Descriptions in both English and Kiswahili
  • Use proper semantic elements for the page layout

What You Learned

  • <iframe> embeds another web page inside your page
  • Used for YouTube videos, Google Maps, and other external content
  • Always include title for accessibility
  • Use width, height, and allowfullscreen attributes
  • Not all websites allow being embedded - some block it for security
  • Iframes slow down pages, so use them only when needed

Module 6 Complete!

You now understand how web pages are structured and organized:

  1. Block vs Inline - how elements flow on the page
  2. Div and Span - generic containers for grouping and styling
  3. Classes - naming elements for reusable CSS styles
  4. IDs - unique identifiers for anchor links and specific targeting
  5. The Head Element - metadata, titles, and page information
  6. Page Layout - header, nav, main, aside, footer structure
  7. Semantic HTML - using meaningful elements for better accessibility and SEO
  8. Iframes - embedding external content

You are now past the halfway mark of the HTML course! In Module 7, you will learn about styles and colors - how to make your pages look beautiful with CSS.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Styles

The Simple Version

HTML builds the structure of a page. But structure alone looks boring - black text on a white background. To make things look good, you add styles. Styles control how elements look: colors, sizes, spacing, fonts, and more.

There are three ways to add styles to HTML:

  1. Inline styles - directly on an element using the style attribute
  2. Internal styles - in a <style> tag in the <head>
  3. External styles - in a separate .css file linked with <link>
Think of HTML as a house built with bricks - strong but plain. Styles are the paint, curtains, and decorations that make it beautiful. You can paint one wall directly (inline), plan all the interior design on a blueprint in the house (internal), or hire a designer who brings their own plans from outside (external).

What's Really Happening

1. Inline styles:

Add the style attribute directly to any element:

<p style="color: red; font-size: 20px;">Red and big!</p>

Good for quick tests. Bad for real projects - you have to repeat it on every element.

2. Internal styles:

Put a <style> tag in the <head>:

<head>
  <style>
    p { color: red; font-size: 20px; }
  </style>
</head>

Better - write once, affects all matching elements on the page.

3. External styles:

Create a separate .css file and link it:

<head>
  <link rel="stylesheet" href="styles.css">
</head>

Best approach - one CSS file can style your entire website.

Common style properties you can use now:

  • color - text color.
  • background-color or background - background color
  • font-size - text size (use px, em, or %)
  • font-family - the font (Arial, Times New Roman, etc.)
  • text-align - left, center, right
  • padding - space inside an element
  • margin - space outside an element
  • border - a line around an element
  • border-radius - rounded corners

Priority rule: If the same element has inline, internal, AND external styles, inline wins. Inline > Internal > External. This is why inline styles are powerful but dangerous - they are hard to override later.

Try It Yourself

Compare all three ways of adding styles.

HTML Code
Result

Things to try:

  • Change the inline colors to different values
  • Edit the .styled-box class in the style tag - both boxes update!
  • Add font-family: Georgia; to the body in the style tag
  • Combine multiple properties: style="color: white; background: black; padding: 10px;"

Real World Example: Styled Business Page

Here is a simple business page using internal styles.

HTML Code
Result

Challenge

Create a styled personal portfolio page. Include:

  • An internal <style> block with at least 6 different CSS rules
  • A hero section with colored background and white text
  • At least 3 "skill cards" with borders, padding, and rounded corners
  • Use font-family, color, background, padding, margin, border-radius
  • A footer with different styling from the rest
  • Both English and Kiswahili

What You Learned

  • Three ways to add styles: inline, internal, external
  • Inline uses the style attribute directly on elements
  • Internal uses a <style> tag in the <head>
  • External uses a linked .css file (best practice)
  • Common properties: color, background, font-size, padding, margin, border
  • Priority: inline beats internal beats external
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Colors

The Simple Version

Colors in HTML/CSS can be written in several ways. The most common are:

  • Color names - plain English words: red, blue, green
  • Hex codes - a # followed by 6 characters: #ff0000
  • RGB - red, green, blue values: rgb(255, 0, 0)

All three of these produce the exact same red color! They are just different ways of writing the same thing.

It is like giving directions to the same place. You can say "go to the post office" (name), "go to GPS coordinates -1.28, 36.82" (hex code), or "go 3 blocks east, 0 blocks north, 0 blocks south" (RGB). All lead to the same place, just different ways of describing it.

What's Really Happening

1. Color names:

HTML supports 140 named colors. Here are the most useful ones:

  • red, blue, green, yellow, orange, purple, pink
  • black, white, gray, silver
  • tomato, coral, gold, teal, navy, crimson

2. Hex codes:

A hex code starts with # and has 6 digits (0-9, A-F). Every two digits represent Red, Green, Blue:

#RRGGBB

#ff0000 = Red:ff  Green:00  Blue:00  → pure red
#00ff00 = Red:00  Green:ff  Blue:00  → pure green
#0000ff = Red:00  Green:00  Blue:ff  → pure blue
#000000 = all zeros → black
#ffffff = all max → white
#808080 = all middle → gray

Shorthand: #f00 is the same as #ff0000. Each digit gets doubled.

3. RGB - Red Green Blue:

Each color is a number from 0 to 255:

rgb(255, 0, 0)   → pure red
rgb(0, 255, 0)   → pure green
rgb(0, 0, 255)   → pure blue
rgb(0, 0, 0)     → black
rgb(255, 255, 255) → white

4. RGBA - RGB with transparency:

Add a fourth number (0 to 1) for transparency. 0 = invisible, 1 = fully visible:

rgba(255, 0, 0, 0.5)  → 50% transparent red
rgba(0, 0, 0, 0.3)   → 30% visible black (light shadow)

Good to know: Professional developers mostly use hex codes. You will see hex everywhere in real code. Tools like color pickers in browsers and design apps give you hex codes automatically. Try right-clicking any colored element on a website and inspecting it!

Try It Yourself

Experiment with all the color formats. Change values and watch the colors change!

HTML Code
Result

Things to try:

  • Change hex values: try #ff0000, #00ff00, #0000ff
  • Mix RGB values: what does rgb(255, 255, 0) make? (yellow!)
  • Play with RGBA transparency - change the last number from 0 to 1
  • Try color names: salmon, orchid, chocolate, steelblue

Real World Example: Kenya Flag Colors

Let's use exact colors to recreate the Kenyan flag.

HTML Code
Result

Challenge

Create a color palette page for a brand or project. Include:

  • A heading with the brand/project name
  • At least 6 color swatches showing different colors
  • Each swatch should show the color name, hex code, AND rgb value
  • Use at least one RGBA transparent color
  • A section showing how the colors look together (e.g., text on backgrounds)
  • Labels in both English and Kiswahili

What You Learned

  • Colors can be written as names (red), hex (#ff0000), or RGB (rgb(255,0,0))
  • Hex codes: #RRGGBB where each pair is 00-ff
  • RGB: three numbers from 0-255 for red, green, blue
  • RGBA adds transparency as a fourth value (0 to 1)
  • Hex codes are most commonly used by professional developers
  • #000000 = black, #ffffff = white
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

CSS Basics

The Simple Version

CSS stands for Cascading Style Sheets. It is the language that makes HTML look good. HTML builds the structure, CSS makes it beautiful.

A CSS rule has three parts:

selector {
  property: value;
}
  • Selector - which elements to style (e.g., p, .card, #title)
  • Property - what to change (e.g., color, font-size)
  • Value - what to set it to (e.g., red, 20px)
CSS is like a recipe for how things should look. The selector is "which dish" (pasta, rice, soup), the property is "what aspect" (temperature, spice level, portion size), and the value is the specific setting (hot, medium, large). One recipe can apply to many servings of the same dish - just like one CSS rule can style many elements.

What's Really Happening

Types of selectors:

/* Element selector - targets all elements of that type */
p { color: blue; }
h1 { font-size: 2em; }

/* Class selector - targets elements with that class */
.card { border: 1px solid #ddd; }
.error { color: red; }

/* ID selector - targets ONE element with that ID */
#main-title { font-size: 3em; }

/* Multiple selectors - same style for multiple elements */
h1, h2, h3 { font-family: Georgia; }

/* Descendant selector - elements inside other elements */
.card p { color: gray; }  /* paragraphs inside .card */
nav a { text-decoration: none; }  /* links inside nav */

The "Cascading" part:

When multiple rules target the same element, CSS uses specificity to decide which one wins:

  1. Inline styles (most specific) - style="..."
  2. ID selectors - #title
  3. Class selectors - .card
  4. Element selectors (least specific) - p

The Box Model:

Every element in CSS is a rectangular box with four layers:

+---------------------------+
|        margin             |  ← space outside
|  +---------------------+  |
|  |      border         |  |  ← the visible line
|  |  +---------------+  |  |
|  |  |    padding     |  |  |  ← space inside
|  |  |  +---------+   |  |  |
|  |  |  | content |   |  |  |  ← your text/image
|  |  |  +---------+   |  |  |
|  |  +---------------+  |  |
|  +---------------------+  |
+---------------------------+

Good to know: This lesson gives you a preview of CSS within the HTML course. You will learn CSS in much more depth in the CSS course. For now, understanding selectors, properties, and the box model is enough to make your HTML pages look great.

Try It Yourself

See how CSS selectors work and how the box model affects layout.

HTML Code
Result

Things to try:

  • Change the .card padding from 20px to 5px - see how it shrinks
  • Change the .card margin-bottom from 15px to 0 - cards touch each other
  • Change border-radius to 0 (sharp corners) or 50px (very round)
  • Add a new class .tag-orange with background: #ff9800; and use it

Real World Example: Complete Styled Page

Here is a full page that puts together everything from this module.

HTML Code
Result

Challenge

Create a complete styled web page for a restaurant, shop, or school. Include:

  • At least 10 CSS rules in the <style> block
  • Use element selectors, class selectors, and at least one descendant selector
  • Demonstrate the box model: padding, margin, border, border-radius
  • Use hex colors and at least one RGBA color
  • A header, nav, main content with cards, and footer
  • Badge elements with different colors
  • Both English and Kiswahili

What You Learned

  • CSS uses selectors to target elements, then sets properties and values
  • Element selectors (p), class selectors (.card), ID selectors (#title)
  • Descendant selectors target elements inside other elements (.card p)
  • The box model: content → padding → border → margin
  • Specificity determines which styles win when rules conflict
  • CSS is a huge topic - this is just the beginning!

Module 7 Complete!

You now understand how to make HTML pages look good:

  1. HTML Styles - inline, internal, and external stylesheets
  2. Colors - names, hex codes, RGB, and RGBA transparency
  3. CSS Basics - selectors, properties, values, and the box model

In Module 8, you will learn about forms and input - how to collect information from users with text fields, buttons, checkboxes, and more. This is how login pages, search bars, and signup forms work!

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Forms

The Simple Version

Forms are how websites collect information from users. Every login page, search bar, signup page, and checkout uses a form.

<form>
  <input type="text" placeholder="Your name">
  <input type="email" placeholder="Your email">
  <button type="submit">Send</button>
</form>

The <form> tag wraps all the input fields. The <input> tag creates a text box where users can type. The <button> submits the form.

Think of a form like a paper application form. It has blank spaces where you fill in your name, address, phone number. The HTML form is the digital version - blank fields where users type their information. When they click "Submit," it is like handing the form to the receptionist.

What's Really Happening

The <form> element:

<form action="/submit" method="POST">
  ...form fields here...
</form>
  • action - where to send the data (a URL)
  • method - how to send it:
    • GET - data appears in the URL (for searches)
    • POST - data is hidden (for passwords, personal info)

The <input> element:

The most versatile form element. The type attribute determines what kind of input it creates:

<input type="text">      <!-- regular text box -->
<input type="password">  <!-- hidden characters -->
<input type="email">     <!-- email validation -->
<input type="number">    <!-- numbers only -->
<input type="submit">    <!-- submit button -->

Important input attributes:

  • name - identifies the field when data is sent. Required!
  • placeholder - ghost text that disappears when you type
  • value - pre-filled text
  • required - field must be filled before submitting

Important: The name attribute is crucial! Without it, the data from that field will not be sent when the form is submitted. Every input field needs a name.

Try It Yourself

Here is a simple form. Try filling it in and clicking submit.

HTML Code
Result

Things to try:

  • Type in the password field - notice the characters are hidden
  • Type an invalid email (no @) and try to submit
  • Add required to an input and try submitting empty
  • Add value="Amina" to the name field - it starts pre-filled
  • Change type="text" to type="number" - you can only type numbers

Real World Example: M-Pesa Send Money

Here is how a mobile money form might look.

HTML Code
Result

Challenge

Create a contact form for a business. Include:

  • A styled form card with a heading
  • Fields for: name (text), email (email), phone (tel), message subject (text)
  • All fields should have name, placeholder, and labels
  • Mark required fields with required
  • A styled submit button
  • CSS styling for inputs (border, padding, focus state)
  • Labels in both English and Kiswahili

What You Learned

  • <form> wraps all input elements and handles submission
  • <input> creates text fields - the type attribute controls behavior
  • Common types: text, email, password, tel, number
  • The name attribute is required for data to be sent
  • placeholder shows hint text, required prevents empty submissions
  • action and method on the form control where and how data is sent
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Input Types

The Simple Version

The <input> element can become many different things just by changing the type attribute. A text box, a checkbox, a date picker, a color picker, a file upload - all from the same element!

Think of <input> like a Swiss Army knife. One tool, many functions. The type attribute is like choosing which tool to flip out - knife, scissors, screwdriver. Same object, completely different behavior depending on the setting.

What's Really Happening

HTML offers many input types. Here are all the important ones:

Text inputs:

  • text - standard text box
  • password - hides characters as dots
  • email - validates email format
  • tel - for phone numbers (mobile shows number keyboard)
  • url - validates URL format
  • search - like text but with a clear button

Number and date inputs:

  • number - only numbers, with up/down arrows
  • range - a slider control
  • date - date picker calendar
  • time - time picker
  • datetime-local - date and time combined

Choice inputs:

  • checkbox - tick box (can select multiple)
  • radio - circle button (select only one from a group)

Other inputs:

  • color - color picker
  • file - file upload button
  • hidden - invisible field (for sending data the user does not see)

Good to know: On mobile phones, the type attribute changes the keyboard layout! type="email" shows a keyboard with @ visible, type="tel" shows a number pad, type="url" shows a keyboard with .com. Always use the correct type for better mobile experience.

Try It Yourself

See every input type in action. Interact with each one!

HTML Code
Result

Things to try:

  • Click the color picker and choose a color
  • Click the date picker - a calendar appears!
  • Move the range slider back and forth
  • Check multiple checkboxes, but only one radio button can be selected
  • Try to type letters in the number field - it only accepts numbers

Real World Example: Event Registration

Here is a registration form using many input types.

HTML Code
Result

Challenge

Create a job application form. Include:

  • Text inputs: name, email, phone
  • A date picker for "available from" date
  • Radio buttons for position type (Full-time, Part-time, Contract)
  • Checkboxes for skills (HTML, CSS, JavaScript, Python)
  • A number input for years of experience (min 0, max 30)
  • A file input for uploading a CV/resume
  • A submit button
  • Labels in both English and Kiswahili

What You Learned

  • The type attribute transforms <input> into many different controls
  • Text types: text, password, email, tel, url, search
  • Number/date types: number, range, date, time
  • Choice types: checkbox (many), radio (one from group)
  • Other types: color, file, hidden
  • Radio buttons with the same name form a group - only one can be selected
  • On mobile, the input type changes the keyboard layout
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Select, Textarea, and Buttons

The Simple Version

Not everything fits in an <input> tag. Sometimes you need a dropdown menu, a large text area, or different types of buttons.

  • <select> - a dropdown menu (pick one from a list).
  • <textarea> - a multi-line text box (for long messages).
  • <button> - a clickable button (more flexible than <input type="submit">)
If <input> is like a fill-in-the-blank question on an exam, then <select> is like a multiple-choice question (choose one answer from a list), and <textarea> is like an essay question (write as much as you want).

What's Really Happening

The <select> element:

<select name="county">
  <option value="">Choose a county...</option>
  <option value="nairobi">Nairobi</option>
  <option value="mombasa">Mombasa</option>
  <option value="kisumu">Kisumu</option>
</select>
  • Each <option> is one choice in the dropdown
  • value is what gets sent when the form submits (the user sees the text)
  • Add selected to an option to make it the default choice
  • Use <optgroup> to group options with a label

The <textarea> element:

<textarea name="message" rows="5" cols="40"
  placeholder="Write your message..."></textarea>
  • rows - how many lines tall (visible height)
  • cols - how many characters wide (visible width)
  • Users can type as much text as they want - it scrolls
  • Unlike <input>, default text goes between the tags, not in a value attribute

The <button> element:

<button type="submit">Send</button>
<button type="reset">Clear Form</button>
<button type="button">Do Something</button>
  • type="submit" - submits the form (default inside a form)
  • type="reset" - clears all form fields back to defaults
  • type="button" - does nothing until JavaScript tells it to

Good to know: <button> is better than <input type="submit"> because you can put HTML inside a button - images, icons, styled text. <input type="submit"> can only show plain text.

Try It Yourself

Try all three elements: select dropdowns, textareas, and buttons.

HTML Code
Result
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Form Validation

The Simple Version

Validation means checking that the user filled in the form correctly before sending it. HTML can do basic validation automatically - no JavaScript needed!

Key validation attributes:

  • required - field cannot be empty.
  • minlength / maxlength - minimum/maximum characters
  • min / max - minimum/maximum number value
  • pattern - must match a specific format (regex)
  • type="email" - must be a valid email format
Validation is like a security guard at the gate. Before anyone enters, the guard checks: Do you have an ID? (required) Is it the right format? (pattern) Are you old enough? (min). The guard does not let you pass until everything is correct. HTML validation works the same way - it stops the form from submitting until everything is valid.

What's Really Happening

Validation attributes in detail:

<!-- Must be filled -->
<input type="text" required>

<!-- Between 3 and 20 characters -->
<input type="text" minlength="3" maxlength="20">

<!-- Number between 18 and 100 -->
<input type="number" min="18" max="100">

<!-- Must match a pattern (Kenyan phone) -->
<input type="tel" pattern="07[0-9]{8}"
  title="Enter a valid Kenyan phone number like 0712345678">

<!-- Step for numbers (increments of 0.5) -->
<input type="number" step="0.5">

The pattern attribute:

Uses regular expressions (regex) to define allowed formats:

  • [0-9]{4} - exactly 4 digits (like a PIN)
  • [A-Za-z]+ - one or more letters (no numbers)
  • 07[0-9]{8} - starts with 07, then 8 digits (Kenyan phone)
  • .{8,} - at least 8 characters (password length)

The title attribute with patterns:

When you use pattern, always add a title that explains what format is expected. The browser shows this message when validation fails.

Important: HTML validation happens on the client (in the browser). It can be bypassed! A user can disable it or use developer tools. You should ALWAYS also validate on the server. HTML validation is a convenience for users, not a security measure.

Try It Yourself

Try to submit this form with invalid data. The browser will stop you and show error messages!

HTML Code
Result

Things to try:

  • Click Submit with empty fields - see the "required" messages
  • Type 2 characters in username - minlength stops you
  • Type "abc" in the phone field - the pattern rejects it
  • Type age as 5 - below the minimum of 13
  • Type "abc" in PIN - pattern expects 4 digits only
  • Remove required from a field - now it can be submitted empty

Challenge

Create a registration form with strict validation. Include:

  • Full name: required, minimum 2 characters
  • Email: required, must be valid email format
  • Phone: required, must match Kenyan format (07XXXXXXXX)
  • National ID: required, pattern for 8 digits
  • Password: required, minimum 8 characters
  • Age: required, between 18 and 65
  • Helpful <small> hints below each field
  • Red asterisks for required fields
  • Labels in both English and Kiswahili

What You Learned

  • required prevents empty form submissions
  • minlength / maxlength control text length
  • min / max control number ranges
  • pattern enforces specific formats using regex
  • title provides custom error messages for patterns
  • HTML validation is client-side only - always validate on the server too!
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Labels and Accessibility

The Simple Version

Every input field needs a label - a text description that tells the user what to type. The <label> element connects text to an input field. When you click the label, the input gets focused!

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

The for attribute on the label must match the id on the input. This creates a connection.

Labels are like name tags on switches. Imagine a row of light switches with no labels - you would not know which one controls which room! Labels tell users (and screen readers) what each input does. Without them, forms are confusing for everyone, and impossible for blind users.

What's Really Happening

Two ways to connect labels:

Method 1: Using for and id (recommended):

<label for="name">Name:</label>
<input type="text" id="name" name="name">

Method 2: Wrapping the input inside the label:

<label>
  Name:
  <input type="text" name="name">
</label>

Both work, but Method 1 is preferred because it gives you more CSS control.

The <fieldset> and <legend> elements:

Group related inputs together with a title:

<fieldset>
  <legend>Personal Information</legend>
  <label for="fname">First Name:</label>
  <input type="text" id="fname">
  <label for="lname">Last Name:</label>
  <input type="text" id="lname">
</fieldset>

Accessibility:

Accessibility means making your website usable by everyone, including people with disabilities.

  • Screen readers - read labels aloud to blind users. They need labels to know what each input is for
  • Click targets - labels make checkboxes and radio buttons easier to click (you can click the text, not just the tiny box)
  • Keyboard navigation - users who cannot use a mouse navigate forms with Tab key

Accessibility best practices:

  • Every input MUST have a label.
  • Use alt on all images
  • Use semantic HTML (<nav>, <main>, etc.)
  • Ensure good color contrast (dark text on light background)
  • Make sure forms work with keyboard only (Tab, Enter, Space)

Why it matters: About 15% of the world's population has some form of disability. In Kenya, that is millions of people. Making your websites accessible is not just good practice - in many countries it is the law. Building accessible websites is a sign of a professional developer.

Try It Yourself

Compare forms with and without proper labels, and see fieldsets in action.

HTML Code
Result

Things to try:

  • In the "Bad" section, click the "I agree" text - the checkbox does NOT toggle
  • In the "Good" section, click "Subscribe to newsletter" text - the checkbox toggles!
  • Click the label "Full Name" - the input field gets focused automatically
  • Click the radio button labels - much easier than clicking the tiny circles
  • Try navigating the good form using only the Tab key

Challenge

Create a fully accessible registration form. Include:

  • A <fieldset> for personal info with: name, email, phone (all with labels)
  • A <fieldset> for preferences with checkboxes (all connected to labels)
  • A <fieldset> for payment method with radio buttons (all connected to labels)
  • Every input must have a matching <label> with for/id
  • Validation attributes (required, minlength, pattern)
  • Good color contrast in your styling
  • Both English and Kiswahili labels

What You Learned

  • <label> connects descriptive text to form inputs
  • Use for on labels matching id on inputs
  • Labels make checkboxes and radio buttons easier to click
  • <fieldset> groups related fields, <legend> gives the group a title
  • Accessibility means making websites usable by everyone
  • Always use labels, alt text, semantic HTML, and good contrast

Module 8 Complete!

You now know how to build interactive forms that collect user information:

  1. Forms - the container that handles data submission
  2. Input Types - text, email, password, number, date, checkbox, radio, and more
  3. Select & Textarea - dropdowns and multi-line text areas
  4. Form Validation - required, minlength, pattern, min/max
  5. Labels & Accessibility - making forms usable for everyone

In Module 9, you will learn advanced HTML - responsive design, character encoding, code elements, best practices, and more.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Responsive Design

The Simple Version

Responsive design means your website looks great on all screen sizes - phones, tablets, and computers. In Kenya, over 80% of people browse the internet on their phones. So mobile-first is not optional - it is essential.

Two things make responsive design work:

  • The viewport meta tag - tells the browser not to zoom out on mobile
  • Media queries - CSS rules that apply only at certain screen sizes
<!-- This goes in your <head> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Think of a good tailor in Nairobi's Gikomba market. They do not make one size of kanga and hope it fits everyone. They measure each person and adjust the fabric to fit perfectly. A responsive website does the same thing - it measures the screen width and adjusts its layout to fit beautifully on every device.

What's Really Happening

The Viewport Meta Tag

Without the viewport tag, mobile browsers shrink your whole desktop page to fit the small screen - making everything tiny. With it, the browser renders at the actual device width.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • width=device-width - use the actual width of the device
  • initial-scale=1.0 - do not zoom in or out at first load

Media Queries

Media queries let you apply different CSS rules based on screen width:

/* Default styles (mobile first) */
.container {
  width: 100%;
  padding: 10px;
}

/* Tablets (wider than 600px) */
@media (min-width: 600px) {
  .container {
    padding: 20px;
  }
}

/* Desktops (wider than 900px) */
@media (min-width: 900px) {
  .container {
    max-width: 1200px;
    margin: 0 auto;
  }
}

Mobile-First vs Desktop-First

Mobile-first (recommended): Write styles for small screens first, then add media queries for larger screens using min-width. This is better because mobile is your primary audience in East Africa.

Desktop-first: Write styles for big screens first, then override with max-width media queries for smaller screens. This is the older approach.

Responsive Images

<!-- Simple responsive image -->
<img src="nairobi.jpg" alt="Nairobi skyline" style="max-width: 100%; height: auto;">

<!-- Using srcset for different screen resolutions -->
<img
  src="nairobi-small.jpg"
  srcset="nairobi-small.jpg 480w, nairobi-medium.jpg 800w, nairobi-large.jpg 1200w"
  sizes="(max-width: 600px) 100vw, (max-width: 900px) 80vw, 60vw"
  alt="Nairobi skyline"
>

The key rule for responsive images: max-width: 100% ensures an image never overflows its container.

Common Breakpoints

  • 480px - small phones
  • 600px - large phones / small tablets
  • 768px - tablets (iPad)
  • 900px - small desktops
  • 1200px - large desktops

Kenya fact: M-Pesa, the world-leading mobile payment system, was designed mobile-first because Kenyans primarily access the internet via mobile phones. Good responsive design is not just about aesthetics - it directly affects whether your users can complete tasks like making payments or filling forms. If your website is not mobile-friendly, you are losing customers.

Try It Yourself

This example shows a Kenyan business profile card that adapts its layout for mobile vs desktop. Resize the preview panel to see it change!

HTML Code
Result

Things to notice:

  • On narrow screens, the header is on top of the body (stacked layout)
  • On wider screens, the header moves to the left side using CSS Grid
  • The services grid changes from 2 columns to 3 to 4 as the screen widens
  • Try making your browser narrower and wider to see the layout shift
  • The viewport meta tag in your real pages is what enables this on mobile devices

Challenge

Build a responsive Kenyan matatu booking page. The page must:

  • On mobile (under 600px): show a single-column layout with a route search form on top, then a list of available routes below
  • On tablet (600px+): show the search form and routes side by side in a 2-column grid
  • On desktop (900px+): add a third column showing travel tips and fare info
  • Include at least 3 route cards (e.g., Nairobi - Mombasa, Nairobi - Kisumu, Nairobi - Nakuru) with a price and departure time
  • Make the images (or placeholder colored boxes) responsive with max-width: 100%
  • Include the viewport meta tag
  • Write styles mobile-first using min-width media queries

What You Learned

  • The viewport meta tag tells the browser to use the real device width - never skip it
  • Media queries apply CSS rules only at certain screen widths using @media (min-width: ...)
  • Mobile-first means writing base styles for small screens and adding larger-screen overrides
  • Responsive images use max-width: 100%; height: auto; to scale with their container
  • Common breakpoints: 480px (phones), 768px (tablets), 1024px (desktops)
  • In Kenya and East Africa, mobile-first is not optional - most users are on phones
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Character Encoding

The Simple Version

Computers store everything as numbers. The letter "A" might be stored as number 65. But which "alphabet system" maps numbers to letters? That is what character encoding decides.

The most important line in your HTML is this one - always put it first inside <head>:

<meta charset="UTF-8">

UTF-8 is the encoding that supports almost every writing system on Earth - Latin, Arabic, Chinese, Emoji, and yes, Kiswahili characters.

Think of different phone networks in East Africa. Safaricom uses one system, Airtel uses another. If you try to call an Airtel number using Safaricom rules, you might reach the wrong person or get an error. Character encoding works the same way - if your file is saved in one encoding but the browser uses different rules to read it, the characters come out garbled, showing strange symbols like � or ???????????? instead of normal text.

What's Really Happening

The History of Encoding

Early computers were built in America and only needed 128 characters - the 26 letters, numbers, and a few symbols. This was called ASCII. But as computers spread globally, every language needed its own encoding system. There were hundreds: ISO-8859-1 for Western Europe, Windows-1252 for Windows, ISO-8859-6 for Arabic, and many more. Files sent between computers in different countries often showed garbled text.

UTF-8: The Universal Solution

Unicode was created to give every character in every human language a unique number. UTF-8 is the most popular way to store Unicode. It uses 1 to 4 bytes per character:

  • English letters: 1 byte (same as ASCII, so it is backwards-compatible)
  • Kiswahili and most Latin-based letters: 1 byte
  • Arabic, Hebrew, Greek: 2 bytes
  • Chinese, Japanese, Korean: 3 bytes
  • Emoji and rare characters: 4 bytes

The charset meta tag

<!DOCTYPE html>
<html lang="sw"> <!-- sw = Kiswahili language code -->
<head>
  <meta charset="UTF-8"> <!-- ALWAYS first in <head> -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Habari yako</title>
</head>

The lang attribute

The lang attribute on the <html> element tells browsers and search engines what language the page is in. This helps screen readers pronounce words correctly and helps Google show your page to the right audience:

<html lang="en">    <!-- English -->
<html lang="sw">    <!-- Kiswahili -->
<html lang="ar">    <!-- Arabic -->
<html lang="zh">    <!-- Chinese -->
<html lang="fr">    <!-- French -->

You can also mark parts of a page with a different language:

<p>The Kiswahili word for hello is <span lang="sw">habari</span>.</p>

HTML Entities for Special Characters

Some characters need to be written as HTML entities to avoid being misinterpreted as HTML code:

&lt;    →  <  (less than - less than sign)
&gt;    →  >  (greater than - greater than sign)
&amp;   →  &  (ampersand - ishara ya &)
&quot;  →  "  (double quote)
&apos;  →  '  (apostrophe)
&copy;  →  ©  (copyright)
&reg;   →  ®  (registered trademark)
&nbsp;  →     (non-breaking space - nafasi isiyovunjika)

Important rule: Always declare <meta charset="UTF-8"> as the very first element inside <head>, before the title. Browsers must know the encoding before they read any text on the page. If it comes after the title, the browser might already have misread the title using a default encoding. UTF-8 is now the standard for all web pages - never use anything else unless you have a very specific reason.

Try It Yourself

This page shows text in multiple languages and scripts that all work perfectly with UTF-8. It also demonstrates HTML entities.

HTML Code
Result

Things to notice:

  • All scripts (Arabic, Chinese, Japanese, Amharic) display correctly - that is UTF-8 at work
  • Arabic text uses direction: rtl to flow right-to-left
  • The Kenya flag emoji is created from two Unicode characters combined
  • HTML entities like &amp; and &copy; render as their symbol, not the code
  • Try adding your own Kiswahili text and see it display perfectly

Challenge

Build a multilingual welcome page for a Nairobi tech company. It should:

  • Have <meta charset="UTF-8"> as the very first element in <head>
  • Use <html lang="en"> on the root element
  • Display a welcome greeting in at least 4 languages (Kiswahili, English, French, and one more of your choice)
  • Use lang attributes on the foreign-language <span> elements
  • Include a footer with a copyright symbol using the &copy; entity
  • Use at least 3 other HTML entities in the page content (e.g., &amp;, &lt;, &reg;)
  • Show the company name and tagline in both English and Kiswahili

What You Learned

  • Character encoding tells the browser which number-to-character system to use
  • UTF-8 is the universal standard - it supports every language and emoji
  • Always put <meta charset="UTF-8"> first in <head>
  • The lang attribute on <html> declares your page's language
  • You can mark inline text in different languages with <span lang="sw">
  • HTML entities like &amp;, &copy;, and &lt; display special characters safely
  • Without proper encoding, multilingual text becomes garbled symbols
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Code Elements

The Simple Version

HTML has special elements for displaying computer code, keyboard input, and program output. These elements help distinguish code from regular text.

The main code-related elements are:

  • <code> - for code snippets.
  • <pre> - for preformatted text (preserves spaces and line breaks).
  • <kbd> - for keyboard input.
  • <samp> - for sample output from a program.
  • <var> - for variables.
Think of a textbook. Regular text is in one font, but code examples are in a different font (usually monospace), keyboard shortcuts are shown in little boxes, and variable names are in italics. HTML gives us elements to mark these differences so the browser knows how to display them, and screen readers know how to read them.

What's Really Happening

1. <code> - Inline code:

Use for short code snippets within a sentence. Displays in a monospace font.

<p>Use the <code>console.log()</code> function to print output.</p>

2. <pre> - Preformatted text:

Preserves ALL whitespace - spaces, tabs, and line breaks. Normal HTML collapses multiple spaces into one, but <pre> keeps them exactly as written.

<pre>
Line 1
  Line 2 (indented)
    Line 3 (more indented)
</pre>

3. <pre> + <code> together - Code blocks:

Combine them for multi-line code examples. This is the standard way to show code blocks.

<pre><code>function greet(name) {
  return "Habari, " + name + "!";
}
console.log(greet("Kenya"));
</code></pre>

4. <kbd> - Keyboard input:

Shows keys the user should press.

<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>

5. <samp> - Sample output:

Shows what a program outputs.

<p>The program displayed: <samp>Hello, World!</samp></p>

6. <var> - Variables:

Marks variable names in math or programming. Displayed in italics.

<p>The area is <var>length</var> × <var>width</var>.</p>

Good to know: By default, <code>, <kbd>, and <samp> all display in a monospace font. <var> displays in italics. You can style them all with CSS to make them look more distinct. Most websites add background colors and borders to make code stand out.

Try It Yourself

See all the code elements in action.

HTML Code
Result

Things to try:

  • Change the code background color in the CSS
  • Add more keyboard shortcuts using <kbd>
  • Write a Python code block inside <pre><code>
  • Remove the <pre> from the code block - watch the formatting collapse!
  • Change the kbd styling to look like real keyboard keys

Challenge

Create a mini coding tutorial page. Include:

  • A title and introduction paragraph
  • At least 2 code blocks with <pre><code> showing different programming examples
  • Inline <code> references within explanatory paragraphs
  • A "Keyboard Shortcuts" section with at least 5 shortcuts using <kbd>
  • Expected output shown with <samp>
  • Mathematical formulas using <var>
  • CSS styling to make each element visually distinct
  • Both English and Kiswahili

What You Learned

  • <code> marks inline code snippets - displays in monospace font
  • <pre> preserves whitespace - use with <code> for code blocks
  • <kbd> shows keyboard input like Ctrl + S
  • <samp> displays sample program output
  • <var> marks variable names in code or math
  • These are semantic elements - they describe what the content IS, not just how it looks
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML Best Practices

The Simple Version

Best practices are the agreed-upon "right ways" to write HTML. They are not enforced by the browser - you can break them all and your page might still display. But following them makes your code clean, maintainable, accessible, and professional.

Best practices cover things like: how to indent your code, whether to use uppercase or lowercase tags, when to add comments, and how to name your files.

Think of building a house in Nairobi. A fundi can stack bricks without mortar, leave wires hanging, and skip the foundation - and the house might stand for a while. But a professional fundi follows building codes: proper foundation, correct wiring, sealed walls. The rules exist because they make the building stronger, safer, and easier for other fundis to work on later. HTML best practices are your web coding building code.

What's Really Happening

1. Always Declare the DOCTYPE

The very first line of every HTML file must be the DOCTYPE declaration. It tells the browser which version of HTML you are using (HTML5).

<!-- GOOD - Nzuri -->
<!DOCTYPE html>
<html lang="en">

<!-- BAD - Mbaya (missing DOCTYPE) -->
<html>

2. Use Lowercase Tag Names

HTML5 allows uppercase tags, but lowercase is the professional standard.

<!-- GOOD - Nzuri -->
<p>Habari za asubuhi!</p>
<img src="nairobi.jpg" alt="Nairobi city">

<!-- BAD - Mbaya -->
<P>Habari za asubuhi!</P>
<IMG SRC="nairobi.jpg" ALT="Nairobi city">

3. Always Close Your Tags

Browsers are forgiving and will often guess what you meant, but unclosed tags cause layout bugs that are very hard to debug.

<!-- GOOD - Nzuri -->
<ul>
    <li>Ugali</li>
    <li>Sukuma wiki</li>
    <li>Chapati</li>
</ul>

<!-- BAD - Mbaya (unclosed tags) -->
<ul>
    <li>Ugali
    <li>Sukuma wiki
    <li>Chapati
</ul>

4. Always Quote Attribute Values

Attribute values without quotes can cause errors, especially when the value contains spaces.

<!-- GOOD - Nzuri -->
<a href="https://example.co.ke" class="btn-primary">Visit</a>
<input type="text" placeholder="Ingiza jina lako">

<!-- BAD - Mbaya -->
<a href=https://example.co.ke class=btn-primary>Visit</a>

5. Always Add Alt Text to Images

The alt attribute is read by screen readers for blind users, shown when images fail to load, and used by search engines.

<!-- GOOD - Nzuri -->
<img src="mount-kenya.jpg" alt="Mount Kenya summit covered in snow">
<img src="logo.png" alt="Safaricom logo">

<!-- BAD - Mbaya -->
<img src="mount-kenya.jpg">
<img src="logo.png" alt=""> <!-- empty alt is only OK for decorative images -->

6. Use Semantic Elements

Use HTML elements for their intended meaning, not just for how they look by default.

<!-- GOOD - Nzuri -->
<header>
    <nav>...</nav>
</header>
<main>
    <article>
        <h1>Habari za Leo</h1>
        <p>...</p>
    </article>
</main>
<footer>...</footer>

<!-- BAD - Mbaya (div soup - supu ya div) -->
<div class="header">
    <div class="nav">...</div>
</div>
<div class="main">
    <div class="article">
        <div class="title">Habari za Leo</div>
    </div>
</div>

7. Proper Indentation

Indent nested elements consistently (2 or 4 spaces, or one tab).

<!-- GOOD - Nzuri (4 spaces per level) -->
<ul>
    <li>
        <a href="nairobi.html">Nairobi</a>
    </li>
    <li>
        <a href="mombasa.html">Mombasa</a>
    </li>
</ul>

<!-- BAD - Mbaya (no indentation) -->
<ul>
<li>
<a href="nairobi.html">Nairobi</a>
</li>
</ul>

8. Smart Comments

Use comments to mark sections, explain complex code, and leave notes for yourself or teammates. But do not over-comment obvious things.

<!-- Navigation - start -->
<nav class="main-nav">
    ...
</nav>
<!-- Navigation - end -->

<!-- Hero section: full-width banner image with CTA button -->
<section class="hero">
    ...
</section>

9. File Naming Rules

  • Use lowercase only: about-us.html not About-Us.html
  • Use hyphens, not spaces or underscores: contact-form.html not contact form.html
  • Keep names short and descriptive: products.html not p.html
  • The main page is always index.html

10. Complete Document Structure

<!DOCTYPE html>
<html lang="sw"> <!-- or "en" or "en-KE" for Kenyan English -->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Duka la bidhaa bora Nairobi">
    <title>Duka Bora - Nairobi</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- content here - maudhui hapa -->
    <script src="app.js"></script>
</body>
</html>

Why this matters in Kenya: Mobile data is expensive. Slow 2G and 3G connections are common outside Nairobi. Clean, well-structured HTML loads faster, works better on slow connections, and ranks higher on Google searches. The leading Kenyan businesses online - Jumia Kenya, Safaricom, KCB - all follow HTML best practices because poor code costs them real customers and real money.

Try It Yourself

Below is a "bad" HTML page for a Nairobi restaurant. It has multiple best-practice violations. Click Run to see it working (browsers are forgiving!), then fix the mistakes and run again to compare.

Violations to find and fix:

  1. Tags are uppercase
  2. The <img> is missing an alt attribute
  3. An attribute value is not quoted
  4. A <li> tag is not closed
  5. A <div> is used instead of a semantic <header>
  6. The file would be named "My Restaurant.HTML" - rename it mentally to my-restaurant.html
HTML Code
Result

The fixed version should:

  • Use all lowercase tag names
  • Have alt="Food at Mama Odhiambo's Kitchen" on the image
  • Have src="food-photo.jpg" quoted properly
  • Close the first <li> with </li>
  • Replace <div class="header"> with <header>

Challenge

Write a complete, best-practices HTML page for a Kenyan business of your choice. The page must pass this checklist:

  • <!DOCTYPE html> on the first line
  • <html lang="en"> or lang="sw" set correctly
  • <meta charset="UTF-8"> present
  • <meta name="viewport" ...> present
  • A descriptive <title> (not just "My Page")
  • All tag names in lowercase
  • All tags properly closed
  • All attribute values in double quotes
  • At least one image with a descriptive alt attribute
  • Semantic elements used: <header>, <main>, <footer>
  • Consistent indentation (2 or 4 spaces)
  • At least 2 comments marking major sections
  • File would be named with lowercase and hyphens (e.g., safari-tours.html)

Ideas for your business: safari tours, M-Pesa agent, chapati shop, kikwembe designs, boda boda booking, Nairobi apartment rental.

What You Learned

  • Always start with <!DOCTYPE html> - it enables standards mode in the browser
  • Use lowercase tag names and attribute names - it is the professional standard
  • Always close your tags - unclosed tags cause hard-to-find layout bugs
  • Always quote attribute values - unquoted values break when they contain spaces
  • Every <img> needs an alt attribute for accessibility and SEO
  • Use semantic elements (<header>, <main>, <footer>, <nav>) instead of generic <div> for everything
  • Indent nested elements consistently so your code is readable by other developers
  • Name files with lowercase letters and hyphens: about-us.html, not About Us.HTML
  • Best practices make your code faster to load, more accessible, and easier to maintain
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML vs XHTML

The Simple Version

XHTML is a stricter version of HTML. It follows the rules of XML, which means every tag MUST be closed, everything MUST be lowercase, and attributes MUST be quoted.

Today, we use HTML5, which is more relaxed. But knowing about XHTML helps you understand why writing clean, well-structured HTML matters.

Think of Kiswahili sanifu (standard Kiswahili) versus everyday Kiswahili. Kiswahili sanifu has strict grammar rules - every word must be correct. Everyday Kiswahili is more relaxed - people understand you even with small mistakes. XHTML is like Kiswahili sanifu (very strict), and HTML5 is like everyday Kiswahili (relaxed but still has rules). Both communicate the same thing, but one demands perfection.

What's Really Happening

A brief history:

  • HTML 4 (1997) - The dominant version for years. Relaxed rules
  • XHTML 1.0 (2000) - HTML reformulated as XML. Very strict rules
  • HTML5 (2014) - The modern standard. Balanced approach. Won the battle

Key differences:

Rule HTML5 XHTML
Tag case <P> or <p> both OK <p> only (lowercase required)
Closing tags <br> OK <br /> required
Attribute quotes class=name sometimes OK class="name" required
Boolean attributes disabled OK disabled="disabled" required
DOCTYPE <!DOCTYPE html> Long, complex DOCTYPE
Error handling Browser fixes errors Page breaks on errors

Examples:

<!-- HTML5 (relaxed) -->
<br>
<img src="photo.jpg" alt="Photo">
<input type="text" disabled>

<!-- XHTML (strict) -->
<br />
<img src="photo.jpg" alt="Photo" />
<input type="text" disabled="disabled" />

Why HTML5 won:

  • XHTML was too strict - one small error and the entire page would not display.
  • HTML5 is forgiving - browsers fix small errors automatically.
  • HTML5 added new features (video, audio, canvas, new form types) that XHTML did not have
  • Most developers preferred the flexibility of HTML5

Good to know: Even though HTML5 is relaxed, you should STILL follow good practices: use lowercase tags, close all elements, quote all attributes. Writing clean HTML makes your code easier to read and less likely to have bugs. Think of it as choosing to write well even when the rules allow shortcuts.

Try It Yourself

See the differences between HTML5 and XHTML style code.

HTML Code
Result

Things to try:

  • In the HTML5 box, try writing <P>text</p> - it still works!
  • Try leaving out closing tags like </p> - HTML5 is forgiving
  • Add your own comparison boxes for other differences

Challenge

Take this messy HTML and rewrite it following best practices (XHTML-style cleanliness).

<HTML>
<BODY>
<H1>My Page</h1>
<P>Welcome to my page
<BR>
<IMG SRC=photo.jpg>
<INPUT TYPE=text DISABLED>
<UL>
<LI>Item 1
<LI>Item 2
<LI>Item 3
</UL>
</body>
</html>
  • Make all tags lowercase
  • Close all tags properly (including <p>, <li>)
  • Self-close void elements (<br />, <img />, <input />)
  • Quote all attribute values
  • Add missing attributes (alt for img)
  • Add proper indentation
  • Add the full document structure (DOCTYPE, head, meta, title)

What You Learned

  • XHTML is a stricter version of HTML based on XML rules
  • XHTML requires: lowercase tags, closed elements, quoted attributes, self-closing void tags
  • HTML5 is more relaxed and forgiving of errors
  • HTML5 is the current standard - XHTML is mostly historical
  • Best practice: write HTML5 but follow XHTML-style cleanliness
  • Clean, well-structured code is easier to read and maintain
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

HTML APIs Overview

The Simple Version

Modern browsers come with built-in superpowers called APIs (Application Programming Interfaces). These let your web pages do things like remember data, know the user's location, draw graphics, and drag things around - all without installing anything extra.

You need JavaScript to use these APIs, but they are part of the HTML5 standard. This lesson gives you a preview of what is possible.

Think of a Swiss Army knife. HTML on its own is the main blade - it cuts (structures content). But the knife also has scissors, a screwdriver, a bottle opener, and more. These extra tools are the browser APIs. They are built into every modern browser, waiting for you to use them. You just need JavaScript to activate them.

What's Really Happening

Here are the most important HTML5 APIs. You will learn to use these properly in the JavaScript course, but here is what they do:

1. Web Storage API:

Store data in the browser that persists even after closing the tab.

  • localStorage - data stays forever (until cleared).
  • sessionStorage - data stays only while the tab is open.
// Save data - Hifadhi data
localStorage.setItem("name", "Amina");

// Get data - Pata data
let name = localStorage.getItem("name");

// Remove data - Ondoa data
localStorage.removeItem("name");

2. Geolocation API:

Get the user's geographical location (with their permission).

navigator.geolocation.getCurrentPosition(function(pos) {
  let lat = pos.coords.latitude;
  let lon = pos.coords.longitude;
  alert("You are at: " + lat + ", " + lon);
});

3. Canvas API:

Draw graphics, shapes, and animations directly on the page.

<canvas id="myCanvas" width="300" height="200"></canvas>

<script>
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
ctx.fillStyle = "#4caf50";
ctx.fillRect(10, 10, 150, 100);
</script>

4. Drag and Drop API:

Make elements draggable and define drop zones.

5. Web Workers API:

Run JavaScript in the background without freezing the page.

6. Fetch API:

Get data from servers (like weather data, news, or M-Pesa transaction status).

Good to know: These APIs all require JavaScript to use. In this HTML course, we are just showing you what exists. You will learn to use them properly in the JavaScript course. Think of this as a preview - like seeing a trailer for a movie before watching it.

Try It Yourself

See some HTML5 APIs in action. These examples use a bit of JavaScript to make the APIs work.

HTML Code
Result

Things to try:

  • Save a name, then click "Run" again - the name persists in storage!
  • Click "Draw Flag" to see the Canvas API draw the Kenya flag
  • Try the Geolocation button (you will be asked for permission)
  • Change the canvas colors or dimensions
  • Try saving different data in localStorage (change the key name)

Challenge

Create a page that uses at least two HTML5 APIs. Ideas:

  • A "Notes App" that saves and loads notes using localStorage
  • A simple drawing app using <canvas>
  • A page that shows the user's location on text
  • A color picker that saves your favorite color to localStorage
  • Combine: Draw on canvas AND save/load the drawing description to storage
  • Both English and Kiswahili text

What You Learned

  • HTML5 APIs are built-in browser features accessed through JavaScript
  • localStorage saves data permanently in the browser
  • sessionStorage saves data only for the current session
  • The Geolocation API can find the user's location (with permission)
  • The Canvas API lets you draw graphics with code
  • Other APIs include Drag and Drop, Web Workers, and Fetch
  • You will learn to use these properly in the JavaScript course

Module 9 Complete!

You have completed the Advanced HTML module! You now understand:

  1. Responsive Design - making pages work on all screen sizes
  2. Character Encoding - how computers handle text in all languages
  3. Code Elements - marking up code, keyboard input, and variables
  4. HTML Best Practices - writing clean, professional HTML
  5. HTML vs XHTML - understanding HTML's history and strict vs relaxed rules
  6. HTML APIs - the powerful features built into modern browsers

Only ONE lesson remains - the Final Project! You will build a complete website using EVERYTHING you have learned.

/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();

Build a Complete Website

The Final Challenge

You have learned everything you need to build a real website with HTML. Now it is time to put it ALL together.

In this final lesson, you will build a complete, multi-section website from scratch. It will use every major concept you have learned across all 10 modules.

Think of this like a final exam, but better - because you are building something real. A chef does not just learn to chop, boil, and fry separately - at the end, they cook a complete meal using ALL their skills together. That is what you are doing now. You are the chef, and this website is your meal.

What You Will Build

You will build a website for "Safari Adventures Kenya" - a tour company based in Nairobi. The website will have all of these sections on a single page:

  1. Header & Navigation - Company logo, name, and anchor links to each section
  2. Hero Section - A big welcome area with a heading, tagline, and call-to-action
  3. About Section - Company description with a figure and image
  4. Tours Section - Tour packages displayed as article cards in a grid
  5. Testimonials - Customer reviews using blockquote
  6. Pricing Table - A table with tour prices, dates, and details
  7. FAQ Section - Common questions using details/summary
  8. Booking Form - A complete form with all input types, labels, validation, fieldsets
  9. Footer - Contact info with address element, social links, copyright

Skills checklist: This project uses: semantic HTML (header, nav, main, article, section, aside, footer, figure, details, address), headings, paragraphs, text formatting, links, images, lists, tables, forms with validation, labels, classes, IDs with anchor links, responsive meta tag, and CSS styling. Basically everything!

Build It

Here is the complete website. Study it carefully, then modify it to make it your own.

HTML Code
/* Lesson playground + migrated-lesson callouts [5818]. Standalone: every colour has a fallback, the learn template's variables are used when present. */ .learn-body .editor-container, .editor-container { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 20px 0; align-items: stretch; } @media (max-width: 760px) { .editor-container { grid-template-columns: minmax(0, 1fr); } } .editor-container .editor-panel, .editor-container .preview-panel { display: flex; flex-direction: column; min-width: 0; overflow: hidden; border: 1px solid var(--line, rgba(15, 61, 40, .18)); border-radius: 10px; background: #fff; } .editor-container .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; background: var(--green-900, #0f3d28); color: #fff; font: 600 12px/1.2 'Sora', system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase; } .editor-container .editor-buttons { display: flex; gap: 6px; } .editor-container .editor-buttons button { font: 600 12px/1 'Sora', system-ui, sans-serif; padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff; } .editor-container .editor-buttons .btn-run { background: var(--green-500, #34c759); border-color: var(--green-500, #34c759); color: #0b2016; } .editor-container .editor-buttons button:hover { filter: brightness(1.12); } .editor-container .editor-buttons button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; } .editor-container textarea { flex: 1; width: 100%; min-height: 220px; resize: vertical; border: 0; outline: 0; margin: 0; padding: 12px 14px; background: #1a1a1a; color: #e6e6e6; tab-size: 2; white-space: pre; overflow: auto; font: 13.5px/1.55 ui-monospace, 'Cascadia Code', 'Fira Code', monospace; } .editor-container textarea:focus-visible { box-shadow: inset 0 0 0 2px var(--green-500, #34c759); } .editor-container iframe { flex: 1; width: 100%; min-height: 220px; border: 0; background: #fff; } .editor-container[data-pg-lang="javascript"] iframe, .editor-container[data-pg-lang="python"] iframe { background: #1a1a1a; } /* CodeHub lesson callouts and sections, unstyled in the plain learn template */ .learn-body .analogy, .learn-body .info-box { margin: 16px 0; padding: 14px 18px; border-radius: 10px; border-left: 4px solid var(--green-600, #22a65a); background: var(--green-100, #d9f5e3); color: var(--green-900, #0f3d28); font-size: 15.5px; line-height: 1.65; } .learn-body .info-box { border-left-color: #2f7fd3; background: #e8f1fb; color: #123a63; } .learn-body .lesson-header h1 { font-family: 'Sora', system-ui, sans-serif; font-size: clamp(26px, 3vw, 36px); margin: 0 0 8px; } .learn-body .try-it, .learn-body .challenge { margin-top: 28px; } .learn-body .challenge { padding: 6px 18px 12px; border: 1px dashed var(--green-600, #22a65a); border-radius: 12px; } /* Lesson playground [5818] * * Makes the "Try it yourself" blocks of migrated CodeHub lessons work on the learn pages. * A block is the markup CodeHub lessons already carry: * *
*
*
Label *
* *
*
Result
*
* * No data-lang means html (the HTML and CSS course blocks). The code runs in a sandboxed iframe * (sandbox="allow-scripts", no allow-same-origin): it cannot read the page, its cookies or storage. * JavaScript mirrors console.* into the output; Python runs on Skulpt (loaded from jsDelivr, as CodeHub does). * Plain ES5 on purpose: the file is inlined into WordPress pages without a build step. */ (function () { 'use strict'; if (window.__pgPlayground) { window.__pgPlayground.scan(document); return; } var SKULPT = [ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt.min.js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/skulpt-stdlib.js' ]; var AUTO_RUN_DELAY = 400; var LANGS = { html: 1, css: 1, javascript: 1, python: 1 }; // Source text for a script that runs inside the generated document. JSON.stringify output is safe in a // script block except for a closing script tag, an HTML comment opener and the two line separators, so escape // every "<" and U+2028/2029. function js(value) { return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); } var OUTPUT_STYLE = 'body{font:13px/1.45 "Cascadia Code","Fira Code",ui-monospace,monospace;padding:12px;margin:0;background:#1a1a1a;color:#d4d4d4}' + '.line{padding:1px 0;white-space:pre-wrap;word-break:break-word}.warn{color:#e5c07b}.error{color:#e06c75}.info{color:#61afef}.hint{color:#8a8a8a;font-style:italic}'; function javascriptDoc(code) { return '' + OUTPUT_STYLE + '
' + '(function(){var out=document.getElementById("out");' + 'function show(args,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");' + 'd.textContent=Array.prototype.map.call(args,function(a){if(a===null)return "null";if(a===undefined)return "undefined";' + 'if(typeof a==="object"){try{return JSON.stringify(a,null,2)}catch(e){return String(a)}}return String(a)}).join(" ");out.appendChild(d)}' + 'console.log=function(){show(arguments,"")};console.info=function(){show(arguments,"info")};' + 'console.warn=function(){show(arguments,"warn")};console.error=function(){show(arguments,"error")};' + 'window.alert=function(m){show(["alert: "+m],"info")};' + 'window.addEventListener("error",function(e){show([e.message],"error")});' + 'window.addEventListener("unhandledrejection",function(e){show(["Unhandled rejection: "+(e.reason&&e.reason.message||e.reason)],"error")});' + 'try{(0,eval)(' + js(code) + ')}catch(e){show([String(e)],"error")}})();' + ''; } function pythonDoc(code) { return '' + OUTPUT_STYLE + '
Running...
' + '' + '(function(){var out=document.getElementById("out");' + 'function line(t,cls){var d=document.createElement("div");d.className="line"+(cls?" "+cls:"");d.textContent=t;out.appendChild(d)}' + 'if(typeof Sk==="undefined"){out.innerHTML="";line("Could not load the Python runtime. Check your connection and press Run again.","error");return}' + 'out.innerHTML="";' + 'function read(x){if(Sk.builtinFiles===undefined||Sk.builtinFiles.files[x]===undefined)throw "File not found: \'"+x+"\'";return Sk.builtinFiles.files[x]}' + 'Sk.configure({output:function(t){var parts=t.split("\\n");parts.forEach(function(p,i){if(i<parts.length-1||p!=="")line(p)})},' + 'read:read,__future__:Sk.python3,inputfun:function(){return ""}});' + 'Sk.misceval.asyncToPromise(function(){return Sk.importMainWithBody("",false,' + js(code) + ',true)})' + '.catch(function(e){line(String(e),"error")})})();' + ''; } function htmlDoc(code) { if (/<!doctype|]/i.test(code)) { return code; } return 'body{font-family:Arial,sans-serif;padding:16px}' + code + ''; } function buildDoc(lang, code) { if (lang === 'javascript') { return javascriptDoc(code); } if (lang === 'python') { return pythonDoc(code); } return htmlDoc(code); } function langOf(box) { var l = (box.getAttribute('data-lang') || 'html').toLowerCase(); return Object.prototype.hasOwnProperty.call(LANGS, l) ? l : 'html'; } function ensure(parent, selector, make) { var el = parent.querySelector(selector); if (!el) { el = make(); parent.appendChild(el); } return el; } function element(tag, className, text) { var e = document.createElement(tag); if (className) { e.className = className; } if (text) { e.textContent = text; } return e; } function setup(box) { if (box.getAttribute('data-pg-ready')) { return; } var lang = langOf(box); var editor = box.querySelector('.editor-panel'); var textarea = box.querySelector('textarea'); if (!editor || !textarea) { return; } box.setAttribute('data-pg-ready', '1'); box.setAttribute('data-pg-lang', lang); var preview = ensure(box, '.preview-panel', function () { var p = element('div', 'preview-panel'); var h = element('div', 'panel-header'); h.appendChild(element('span', '', lang === 'html' || lang === 'css' ? 'Result' : 'Output')); p.appendChild(h); return p; }); var frame = preview.querySelector('iframe'); if (!frame) { frame = document.createElement('iframe'); preview.appendChild(frame); } frame.setAttribute('sandbox', 'allow-scripts'); frame.setAttribute('title', lang === 'html' || lang === 'css' ? 'Result' : 'Output'); frame.setAttribute('loading', 'lazy'); frame.setAttribute('referrerpolicy', 'no-referrer'); textarea.setAttribute('spellcheck', 'false'); textarea.setAttribute('aria-label', 'Code editor'); var buttons = ensure(editor.querySelector('.panel-header') || editor, '.editor-buttons', function () { return element('div', 'editor-buttons'); }); var run = buttons.querySelector('.btn-run') || buttons.appendChild(element('button', 'btn-run', 'Run')); var reset = buttons.querySelector('.btn-reset') || buttons.appendChild(element('button', 'btn-reset', 'Reset')); run.type = 'button'; reset.type = 'button'; var initial = textarea.value; var live = lang === 'html' || lang === 'css'; var timer = null; function render() { if (timer) { clearTimeout(timer); timer = null; } frame.srcdoc = buildDoc(lang, textarea.value); } run.addEventListener('click', render); reset.addEventListener('click', function () { textarea.value = initial; if (live) { render(); } else { frame.srcdoc = emptyDoc(); } }); textarea.addEventListener('keydown', function (e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); render(); } }); if (live) { textarea.addEventListener('input', function () { if (timer) { clearTimeout(timer); } timer = setTimeout(render, AUTO_RUN_DELAY); }); render(); } else { frame.srcdoc = emptyDoc(); } } function emptyDoc() { return '' + OUTPUT_STYLE + '
Press Run (or Ctrl+Enter) to see the output.
'; } function scan(root) { var boxes = (root || document).querySelectorAll('.editor-container'); for (var i = 0; i < boxes.length; i++) { setup(boxes[i]); } } window.__pgPlayground = { scan: scan, buildDoc: buildDoc }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { scan(document); }); } else { scan(document); } })();
Questions while you work through the steps? The Course Coach (bottom right) knows this course inside out.
Browse more courses →
Ask the coachAI guide for this course