Programming Books

HTML5, CSS & JavaScript for Beginners: 3 Mini Projects with Source Code (Free PDF)

html5 and javascript projects

Project 2 — To-Do List That Remembers (localStorage)

Now something people actually use. Type a task, press Add, it appears in a list with a delete button. The HTML5 feature here is localStorage — the browser’s built-in key-value store. Your tasks survive a page refresh, a closed tab, even a restart, with no server and no database.

🌐 index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>To-Do List</title>
  <link rel="stylesheet" href="style.css">
  <script src="script.js" defer></script>
</head>
<body>
  <main>
    <h1>My Tasks</h1>
    <div class="add-row">
      <input id="task" type="text" placeholder="What needs doing?">
      <button id="add">Add</button>
    </div>
    <ul id="list"></ul>
  </main>
</body>
</html>
🎨 style.css
body { font-family: sans-serif; padding: 40px; background: #f8fafc; }
main { max-width: 420px; margin: 0 auto; }
.add-row { display: flex; gap: 8px; }
input { flex: 1; padding: 10px; font-size: 1rem; border: 1px solid #cbd5e1; border-radius: 8px; }
button { padding: 10px 16px; background: #059669; color: #fff; border: none; border-radius: 8px; cursor: pointer; }
ul { list-style: none; padding: 0; margin-top: 16px; }
li { display: flex; justify-content: space-between; align-items: center;
     background: #fff; padding: 12px; margin-bottom: 8px; border-radius: 8px;
     border: 1px solid #e2e8f0; }
li button { background: #ef4444; padding: 4px 10px; }
⚡ script.js
const input = document.getElementById("task");
const list  = document.getElementById("list");

// load saved tasks, or start with an empty list
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];

function save() {
  localStorage.setItem("tasks", JSON.stringify(tasks));
}

function render() {
  list.innerHTML = "";
  tasks.forEach((task, i) => {
    const li  = document.createElement("li");
    li.textContent = task;
    const del = document.createElement("button");
    del.textContent = "✕";
    del.dataset.index = i;          // HTML5 data-attribute
    li.appendChild(del);
    list.appendChild(li);
  });
}

document.getElementById("add").addEventListener("click", () => {
  const text = input.value.trim();
  if (!text) return;                // ignore empty input
  tasks.push(text);
  input.value = "";
  save();
  render();
});

// one listener on the list handles every delete button
list.addEventListener("click", (e) => {
  if (e.target.dataset.index !== undefined) {
    tasks.splice(Number(e.target.dataset.index), 1);
    save();
    render();
  }
});

render();

Three ideas worth noticing. localStorage only stores strings, so we JSON.stringify the array on the way in and JSON.parse it on the way out — that pair is how you store anything structured. The || [] handles the very first visit, when nothing is saved yet. And instead of attaching a click handler to every delete button, we put one listener on the whole list and use an HTML5 data-index attribute to know which task was clicked — this is called event delegation, and it’s how real apps handle lists.

Test the persistence: add three tasks, then press F5. They’re still there. Open DevTools → Application → Local Storage and you’ll see the tasks key with your JSON inside it.

Project 3 — Live Form Validator (HTML5 Input Types)

A sign-up form that tells you what’s wrong as you type, not after you hit submit. This one leans hardest on HTML5: the browser already knows how to check an email address or enforce a pattern — we just use its answers. The HTML5 features are type="email", type="tel", required, minlength, pattern, and the Constraint Validation API (input.validity) that exposes the results to JavaScript.

🌐 index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sign Up</title>
  <link rel="stylesheet" href="style.css">
  <script src="script.js" defer></script>
</head>
<body>
  <main>
    <h1>Create account</h1>
    <form id="signup" novalidate>
      <label>Name
        <input id="name" type="text" required minlength="2">
      </label>
      <span class="msg" id="name-msg"></span>

      <label>Email
        <input id="email" type="email" required>
      </label>
      <span class="msg" id="email-msg"></span>

      <label>Phone
        <input id="phone" type="tel" pattern="[0-9]{11}" placeholder="03001234567">
      </label>
      <span class="msg" id="phone-msg"></span>

      <button type="submit">Sign up</button>
    </form>
  </main>
</body>
</html>

novalidate on the form matters: it switches off the browser’s default pop-up bubbles so our own messages show instead. The validation rules still work — we’re just choosing how to display them.

🎨 style.css
body { font-family: sans-serif; padding: 40px; background: #f8fafc; }
main { max-width: 380px; margin: 0 auto; }
label { display: block; margin-top: 14px; font-weight: 600; }
input { width: 100%; padding: 10px; margin-top: 4px; font-size: 1rem;
        border: 2px solid #cbd5e1; border-radius: 8px; box-sizing: border-box; }
input.error { border-color: #ef4444; background: #fef2f2; }
input.ok    { border-color: #059669; }
.msg { display: block; color: #ef4444; font-size: 0.85rem; min-height: 1.2em; margin-top: 4px; }
button { margin-top: 18px; width: 100%; padding: 12px; background: #4f46e5;
         color: #fff; border: none; border-radius: 8px; font-size: 1rem; cursor: pointer; }
⚡ script.js
const form   = document.getElementById("signup");
const fields = ["name", "email", "phone"];

function validate(id) {
  const input = document.getElementById(id);
  const msg   = document.getElementById(id + "-msg");
  const v     = input.validity;            // the browser's verdict

  if (v.valid) {
    input.classList.remove("error");
    input.classList.add("ok");
    msg.textContent = "";
    return true;
  }

  input.classList.add("error");
  input.classList.remove("ok");
  if (v.valueMissing)         msg.textContent = "This field is required";
  else if (v.typeMismatch)    msg.textContent = "Please enter a valid email";
  else if (v.patternMismatch) msg.textContent = "Enter an 11-digit number";
  else if (v.tooShort)        msg.textContent = "Too short — at least 2 characters";
  return false;
}

// check each field live, on every keystroke
fields.forEach((id) => {
  document.getElementById(id).addEventListener("input", () => validate(id));
});

// on submit, check everything and only proceed if all pass
form.addEventListener("submit", (e) => {
  e.preventDefault();
  const results = fields.map(validate);
  if (results.every(Boolean)) {
    alert("Form is valid! ✓");
    form.reset();
    fields.forEach((id) => document.getElementById(id).classList.remove("ok"));
  }
});

The key line is const v = input.validity. The browser has already checked the input against its HTML5 attributes — required, type="email", pattern, minlength — and validity is a small object of true/false flags saying what passed and what didn’t. We just read those flags and show a message. No regular expressions for email, no length-counting; HTML5 did it.

One subtle detail: on submit we use fields.map(validate) rather than fields.every(validate). every stops at the first failure, so only one error would show; map runs all three so every bad field lights up at once.

HTML5 Features Used in These Projects

Feature What it does Used in
<header>, <main> Semantic structure — says what content is, not just where All three
defer Runs the script after HTML is loaded All three
placeholder Grey hint text inside an input Projects 2, 3
localStorage Browser key-value storage that survives refresh Project 2
data-* attributes Attach your own data to any element Project 2
type="email", type="tel" Input types with built-in checking and mobile keyboards Project 3
required, minlength, pattern Declarative validation rules Project 3
input.validity Constraint Validation API — read the browser’s verdict in JS Project 3
novalidate Suppress default bubbles so you can show your own messages Project 3

Common Mistakes (and the Exact Error You’ll See)

Mistake 1: Script runs before the HTML exists

⬛ Console error
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

Cause: The <script> tag is in <head> without defer, so it runs before the <button> exists. getElementById finds nothing, returns null, and null.addEventListener crashes.
Fix: Add defer to the script tag (what all three projects do), or move the <script> to just before </body>.

Mistake 2: getElementById returns null (wrong id)

Same error as above, different cause. If you already have defer and still see “Cannot read properties of null,” the id doesn’t match. Usual culprits: a typo ("btn" vs "button"), a capital letter ("Count" vs "count"), or writing getElementById("#count") with a # — that’s CSS-selector syntax and belongs in querySelector, not here.
Fix: Copy the id straight from the HTML. If in doubt, console.log(document.getElementById("count")) — if it prints null, the id is wrong.

Mistake 3: == instead of ===

What you see: No error — just logic that’s subtly wrong. "5" == 5 is true because == converts types before comparing; "5" === 5 is false. In Project 2, e.target.dataset.index is a string — which is why we wrap it in Number() before splice.
Fix: Always use === and !==. There’s no situation in beginner code where == is what you want.

Mistake 4: CSS isn’t applied

What you see: The page is unstyled — Times New Roman, default blue buttons.
Cause A — wrong path: href="style.css" but the file is in a css/ folder, or named styles.css. Open DevTools → Network, refresh, and look for a red 404 on the CSS file.
Cause B — cache: The browser is showing the old CSS. Hard-refresh with Ctrl+Shift+R (Windows) or Cmd+Shift+R (Mac). Live Server usually prevents this, but a plain file-open doesn’t.

What to Build Next

  • Theme switcher — a dark/light toggle that saves your choice in localStorage. Same two features as Project 2, new purpose.
  • Countdown timer — setInterval, a number that ticks down, and a button to reset. Introduces timing.
  • Quiz app — questions in an array, one shown at a time, score at the end. Combines everything above plus arrays and objects.

Exercises on the three projects

  • Project 1: add a Reset button that sets the count back to 0, and make the number turn red when it passes 10.
  • Project 2: add a “done” state — clicking a task’s text toggles a line-through style and saves that too (hint: store objects {text, done} instead of strings).
  • Project 3: add a password field with minlength="8" and a “confirm password” field that checks they match — that one isn’t an HTML5 rule, so you’ll write the comparison yourself.

📥 Download: All 3 Projects + PDF Notes

Free from TechProFree: a zip containing the three project folders (project-1-counter, project-2-todo, project-3-form), each with index.html, style.css and script.js ready to open — plus a PDF of these notes with every code block, the HTML5 features table and the four mistakes. No signup required.

Final Thoughts

You’ve just built three real web pages that do something — and along the way you used semantic HTML5, browser storage, data attributes, smart input types and the Constraint Validation API. More importantly, you’ve seen the one pattern that every interactive page shares: find an element, listen for an event, change the page. Everything from here is that pattern with more pieces.

Do the exercises, then build one of the three “next” ideas without looking at the code above. The moment you can do that, you’re no longer following a tutorial — you’re a web developer with a small portfolio.

Which project did you build first? Stuck on something not covered in the four mistakes? Drop a comment — I read and reply to every one.

Frequently Asked Questions (FAQ)

What is the difference between HTML5 and HTML?

HTML5 is simply the current version of HTML — the one every browser uses today. It added semantic tags like <header> and <main>, new input types like email and tel, built-in form validation, localStorage, and <video>/<audio>. When people say “HTML” in 2026 they mean HTML5; the “5” just signals those features are available.

Can I download these projects and notes as a PDF?

Yes — the free zip (all three projects, ready to open) and the PDF notes are linked in the download section above. No signup required. The PDF covers every code block, the HTML5 features table, the four common mistakes and the exercises.

What is the best free book to learn HTML5 and JavaScript?

Honestly, the best free “book” is MDN Web Docs — it’s more current and more accurate than any printed book, and it’s free. For JavaScript specifically, javascript.info is a complete free textbook with exercises. Both are linked above. Avoid searching for pirated PDFs of commercial books; the free resources are genuinely better.

How long does it take to learn HTML5, CSS and JavaScript?

You can build all three projects on this page in an afternoon. Being comfortable with HTML and CSS takes about 2–4 weeks of regular practice; JavaScript basics take another 1–2 months. Reaching a level where you can build a small site from scratch unaided typically takes 3–6 months — much faster if you build projects rather than only watching tutorials.

Do I need to learn CSS before JavaScript?

You need enough CSS to make a page look reasonable — selectors, colours, spacing, a bit of flexbox — but not more than what’s in these three projects before you start JavaScript. Learning them together, as these projects do, is faster than finishing one before touching the other. Go deeper into CSS layout once you’re comfortable making things interactive.