Show contents
Chapter 6 of 7 · 25 min
Project: an online business card
By the end of this chapter, you'll be able to
- Build a complete mobile page: viewport, fluid layout, touch buttons
- Add call and chat links that work on a phone
- Keep the page within a weight budget
- Write a JavaScript checker that verifies the rules of a mobile page
The page
Here is the complete page. It fits in a single file, and each choice comes from a chapter of the course: the viewport tag, a fluid column, 48-pixel buttons placed at the bottom of the screen, tel: and wa.me links, and no heavy image (the icon is a character, which weighs almost nothing).
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fatou Repairs — phones in Bamako</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.5; padding-bottom: 6rem; }
main { max-width: 40rem; margin: 0 auto; padding: 1rem; }
.services { padding-left: 1.25rem; }
.actions { position: fixed; bottom: 0; left: 0; right: 0; display: flex; gap: 0.75rem;
padding: 0.75rem 1rem; background: white; box-shadow: 0 -2px 8px rgba(0,0,0,.1); }
.button { flex: 1; min-height: 48px; display: flex; align-items: center; justify-content: center;
border-radius: 0.75rem; font-weight: 700; text-decoration: none; }
.call { background: #0b3b7a; color: white; }
.whatsapp { border: 2px solid #0b3b7a; color: #0b3b7a; }
</style>
</head>
<body>
<main>
<h1>📱 Fatou Repairs</h1>
<p>Phone repairs in Médina Coura, Bamako.</p>
<ul class="services">
<li>Broken screens</li>
<li>Batteries and charging ports</li>
<li>Updates and backups</li>
</ul>
<p><strong>Opening hours:</strong> Monday to Saturday, 9 a.m. to 7 p.m.</p>
</main>
<nav class="actions">
<a class="button call" href="tel:+22370000000">Call</a>
<a class="button whatsapp" href="https://wa.me/22370000000?text=Hello%20Fatou%2C%20my%20phone%20has%20a%20problem">WhatsApp</a>
</nav>
</body>
</html>
The body's padding-bottom: 6rem keeps room below the content: without it, the button bar fixed at the bottom would hide the last line. The number is an example.
Project: Fatou's business cardHTML · CSS · JS
Preview
Console
(Nothing in the console yet.)Your code runs in an isolated page, with no access to this site or the Internet · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
The mobile page checker
So as not to forget a rule as the page evolves, you can write a checker that reads the HTML and reports problems. A simple version, working on the page's text:
function check(html, weightKB, budgetKB = 100) {
const problems = [];
if (!/<meta name="viewport" content="width=device-width/.test(html)) {
problems.push("viewport tag missing");
}
if (/user-scalable=no/.test(html)) {
problems.push("zoom is blocked (user-scalable=no)");
}
const images = html.match(/<img\b[^>]*>/g) || [];
for (const img of images) {
if (!/\balt="/.test(img)) problems.push("image without alternative text: " + img);
}
if (!/href="(tel:|https:\/\/wa\.me\/)/.test(html)) {
problems.push("no link to call or message");
}
if (weightKB > budgetKB) problems.push(`weight ${weightKB} kB, over the ${budgetKB} kB budget`);
return problems;
}
const goodPage = `<meta name="viewport" content="width=device-width, initial-scale=1">
<a href="tel:+22370000000">Call</a>`;
const badPage = `<meta name="viewport" content="width=device-width, user-scalable=no">
<img src="shop.jpg">`;
console.log("good page:", check(goodPage, 12));
console.log("bad page:");
for (const p of check(badPage, 4300)) console.log("-", p);
good page: []
bad page:
- zoom is blocked (user-scalable=no)
- image without alternative text: <img src="shop.jpg">
- no link to call or message
- weight 4300 kB, over the 100 kB budget
Regular expressions (/…/) look for patterns in the text: \b marks a word boundary, [^>]* means "every character up to the next >". For a real site, you would use a complete audit tool, such as Lighthouse, built into Chrome's developer tools; but a small home-made checker is enough to keep the basic rules in view.
You fix, run the checker again, and only go online with an empty list.
Text description of the animation
- The tag exists, but it blocks zooming: that is an accessibility problem.
- An image has no description: a screen reader will not know what to say.
- For a business card, the lack of a call button is a deal-breaker.
- The page is 43 times too heavy: an unresized photo, most likely.
- You fix, run the checker again, and only go online with an empty list.
Your turn
Worked exercise
Worked exercise: one more rule
Add a rule to the checker: every image must have the attribute loading="lazy", except the first one on the page, which is often visible straight away. Write the checkImages(html) function that returns the list of faulty images, and test it on a page with three images.
Hint 1
html.match(/<img\b[^>]*>/g) || [] gives the list of <img> tags, in page order.Hint 2
forEach((img, i) => …) and skip index 0. For the others, test /loading="lazy"/.Show the solution and common mistakes
function checkImages(html) {
const images = html.match(/<img\b[^>]*>/g) || [];
const faulty = [];
images.forEach((img, i) => {
if (i > 0 && !/loading="lazy"/.test(img)) faulty.push(img);
});
return faulty;
}
const page = `<img src="workshop.webp" alt="Fatou's workshop">
<p>Our repairs</p>
<img src="screen.webp" alt="Replaced screen" loading="lazy">
<img src="battery.webp" alt="New battery">`;
for (const img of checkImages(page)) console.log("to fix:", img);
to fix: <img src="battery.webp" alt="New battery">
Common mistakes:
- Putting
loading="lazy"on every image, including the first: the browser waits before loading it, and the top of the page shows up more slowly. - Looking for
lazyanywhere in the tag: a file namedlazy-cat.webpwould pass the test. Look for the full attribute,loading="lazy".
Summary
- 1A good mobile page brings together: viewport tag, fluid column, 48-pixel buttons within thumb reach.
- 2tel: and wa.me links turn a page into a useful business card.
- 3A weight budget and light images keep the page fast on slow networks.
- 4An automatic checker verifies the basic rules before each release.
- 5Test at 360 pixels, on slow 3G and with enlarged text, not only on your own phone.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.