Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester
Week 2 Theory – HTML5 for Mobiles – Responsive Pages, Mobile Forms, Device APIs and PWAs
Week 2 – HTML5 for Mobiles · 75 min read · 32 MCQs to check yourself
Kevin Powell – Get your media queries working with the meta viewport tag
Let's code a responsive webpage (mobile first): viewport, Flexbox and media queries
1. HTML5 and the mobile web
HTML5 is the fifth major version of HyperText Markup Language, the language that structures web pages. Together with CSS3 (presentation) and JavaScript (behaviour) it lets developers build web pages and complete applications that run in any mobile browser (Chrome, Safari, Samsung Internet, Firefox).
Why HTML5 matters for mobiles:
- One code base for Android, iOS and desktop – no store approval needed.
- New semantic elements (
header,nav,main,article,footer) give clean structure, better accessibility and SEO. - Native audio and video (
audio,videotags) – no Flash plug-in (Flash never ran on iPhone and is dead). - Mobile-friendly form inputs (
type="email",tel,number,date) open the right on-screen keyboard. - Device APIs – Geolocation, Web Storage, Device Orientation, Camera (media capture), Vibration, Notifications, Offline (service workers).
- Canvas and SVG for graphics and games.
- Basis of hybrid apps (Cordova/Ionic) and Progressive Web Apps.
Basic HTML5 page skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Mobile Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header><h1>Campus News</h1></header>
<main> ... </main>
<footer>© 2026</footer>
</body>
</html>
<!DOCTYPE html>– tells the browser to use HTML5 standards mode (much shorter than older doctypes).meta charset="UTF-8"– supports all languages including Urdu.lang="en"– page language (useurfor Urdu, withdir="rtl").
2. The viewport and responsive design
The viewport meta tag
Mobile browsers originally pretended to be about 980 px wide and zoomed the page out, so desktop sites looked tiny. The viewport tag tells the browser to use the real device width:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
| Part | Meaning |
|---|---|
width=device-width |
Layout width = the device's screen width in CSS pixels |
initial-scale=1.0 |
No zoom when the page first loads |
maximum-scale, user-scalable=no |
Disable zoom – avoid, it harms accessibility |
Without it, media queries do not work as expected on phones.
CSS pixels vs device pixels
Phones have high-density screens. Device pixel ratio (DPR) = physical pixels ÷ CSS pixels. A phone 1080 physical pixels wide with DPR 2.75 is about 393 CSS pixels wide. Developers design in CSS pixels; images may need srcset for sharp display.
Responsive web design (RWD)
A responsive page adapts its layout to any screen size. Three ingredients (Ethan Marcotte, 2010):
- Fluid grids – widths in
%,fr,vw,reminstead of fixed pixels; Flexbox and CSS Grid layouts. - Flexible images –
img { max-width: 100%; height: auto; } - Media queries – apply CSS only when conditions are true:
/* Mobile-first: base styles are for small screens */
.cards { display: grid; grid-template-columns: 1fr; gap: 12px; }
/* Tablets and larger */
@media (min-width: 600px) {
.cards { grid-template-columns: 1fr 1fr; }
}
/* Desktops */
@media (min-width: 1024px) {
.cards { grid-template-columns: repeat(4, 1fr); }
}
@media (orientation: landscape) { header { padding: 4px; } }
Mobile-first design
Write styles for the smallest screen first, then add min-width media queries for bigger screens. Benefits: phones download less CSS, the design focuses on essential content, and progressive enhancement is natural.
Other mobile-friendly practices
- Use relative font sizes (
rem) and at least 16 px body text. - Touch targets at least about 48 × 48 px with spacing.
- Avoid hover-only menus (no hover on touch screens) – use a hamburger menu.
- Optimise images (WebP/AVIF,
loading="lazy"), minimise JavaScript – mobile data is slow and costly. - Test with Chrome DevTools device mode (Ctrl + Shift + M) and real phones; check with Lighthouse.
- Responsive images:
<img src="small.jpg" srcset="small.jpg 480w, large.jpg 1200w" sizes="100vw" alt="...">and thepictureelement.
3. HTML5 semantic elements and mobile forms
Semantic elements
| Element | Use |
|---|---|
header |
Top section of the page or of an article |
nav |
Navigation links |
main |
Main unique content (one per page) |
section |
Thematic group of content |
article |
Self-contained content (news post, card) |
aside |
Side content (related links) |
footer |
Bottom section |
figure / figcaption |
Image with caption |
audio / video |
Media without plug-ins, e.g. <video src="intro.mp4" controls playsinline> |
canvas |
Drawing area for JavaScript graphics |
HTML5 input types (each shows a suitable mobile keyboard)
| Input | Mobile behaviour |
|---|---|
type="email" |
Keyboard with @ and .com; built-in format validation |
type="tel" |
Numeric phone dial pad |
type="number" |
Number keyboard; min, max, step |
type="url" |
Keyboard with / and .com |
type="date", time, datetime-local |
Native date/time picker |
type="range" |
Slider |
type="color" |
Colour picker |
type="search" |
Search keyboard with a "Go/Search" key |
type="file" accept="image/*" capture="environment" |
Opens the camera directly on phones |
Useful attributes: required, placeholder, pattern="[0-9]{11}", autocomplete="email", inputmode="numeric", autofocus, min/max, maxlength.
<form action="/register" method="post">
<label for="name">Full name</label>
<input id="name" name="name" type="text" required autocomplete="name">
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="phone">Mobile</label>
<input id="phone" name="phone" type="tel" pattern="03[0-9]{9}" placeholder="03xxxxxxxxx">
<label for="dob">Date of birth</label>
<input id="dob" name="dob" type="date">
<button type="submit">Register</button>
</form>
Always connect label for to the input id – a tap on the label focuses the field and screen readers read it.
4. HTML5 APIs for mobile devices
| API | What it does | Example |
|---|---|---|
| Geolocation | Gets latitude/longitude (with user permission, HTTPS only) | navigator.geolocation.getCurrentPosition(ok, err) |
| Web Storage | Stores key–value data in the browser: localStorage (permanent) and sessionStorage (until tab closes), about 5 MB |
localStorage.setItem("user", "Ali") |
| IndexedDB | Larger structured offline database in the browser | Offline notes app |
| Device Orientation / Motion | Reads tilt and acceleration | Games, compass |
| Vibration | Vibrates the phone (Android browsers) | navigator.vibrate(200) |
| Media capture / getUserMedia | Access camera and microphone | Video calls, QR scanning |
| Notifications + Push | Show system notifications (with a service worker) | News alerts |
| Web Share | Opens the phone's share sheet | navigator.share({title, url}) |
| Canvas | 2-D drawing with JavaScript | Charts, games |
| Service Worker + Cache API | Intercepts network requests and caches files for offline use | PWAs |
<button onclick="where()">Where am I?</button>
<p id="out"></p>
<script>
function where() {
navigator.geolocation.getCurrentPosition(
p => document.getElementById("out").textContent =
"Lat " + p.coords.latitude + ", Lng " + p.coords.longitude,
e => alert("Permission denied or unavailable"));
}
</script>
5. Progressive Web Apps (PWA) and WebView
A PWA is a web app enhanced to feel like a native app:
| Requirement | Purpose |
|---|---|
| Served over HTTPS | Security; needed for service workers and geolocation |
Web app manifest (manifest.json) |
Name, icons, theme colour, display: "standalone", start URL – lets users install to home screen |
| Service worker | JavaScript that runs in the background; caches files so the app works offline, handles push messages |
| Responsive design | Works on every screen |
{
"name": "Campus Feedback",
"short_name": "Feedback",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#0F766E",
"icons": [{ "src": "icon-192.png", "sizes": "192x192", "type": "image/png" }]
}
Examples: Twitter/X Lite, Starbucks, Pinterest, Uber's web app. Android supports PWAs very well; iOS supports installation from Safari ("Add to Home Screen") with some limits.
WebView – web content inside native apps
A WebView (android.webkit.WebView on Android, WKWebView on iOS) is a native component that displays web pages inside an app. Hybrid apps use it for the whole UI; native apps use it for help pages, terms and conditions, or payment pages.
WebView web = findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://developer.android.com");
(Requires <uses-permission android:name="android.permission.INTERNET"/> in the manifest.)
Native app vs HTML5 web app
| HTML5 web app | Native app | |
|---|---|---|
| Install | No (or "add to home screen") | From store |
| Updates | Instant (server) | User downloads update |
| Device access | Limited to browser APIs | Full |
| Performance | Good, but lower | Best |
| Offline | With service worker | Yes |
| Discovery | Search engines, links | App stores |
Short and long questions
1. Give four features of HTML5 that make it suitable for mobile development.
Model answer
(1) Viewport and responsive design support. (2) Mobile-friendly input types (email, tel, date) that show suitable keyboards. (3) Native audio/video without plug-ins. (4) Device APIs such as Geolocation, Web Storage, Device Orientation and service workers for offline use.
2. Write the basic skeleton of an HTML5 page suitable for mobile devices.
Model answer
<!DOCTYPE html> then <html lang="en">, a head containing <meta charset="UTF-8">, <meta name="viewport" content="width=device-width, initial-scale=1.0"> and a <title>, and a body containing header, main and footer elements. The viewport tag is the essential mobile part.
3. What is a WebView? Give two uses.
Model answer
A WebView is a native UI component that renders web pages inside an app (WebView on Android, WKWebView on iOS). Uses: showing the whole UI of hybrid apps, and displaying help pages, terms, news articles or payment pages inside native apps.
4. What is a Progressive Web App? List its three technical requirements.
Model answer
A PWA is a web application that can be installed and behaves like a native app – home-screen icon, full-screen display, offline use and push notifications. Requirements: served over HTTPS, a web app manifest (name, icons, start URL, display mode) and a service worker for caching/offline.
5. Explain responsive web design for mobile devices. Discuss the viewport meta tag, fluid grids, flexible images, media queries and the mobile-first approach with suitable code.
Model answer
Responsive web design (RWD) is an approach where one HTML page adapts its layout to the screen size, resolution and orientation of the device, so it looks good on phones, tablets and desktops.
Viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1.0"> – tells mobile browsers to make the layout width equal to the device width in CSS pixels and not to zoom initially. Without it phones assume about 980 px and shrink the page.
Fluid grids: layouts use relative units (%, fr, rem, vw) and modern layout systems such as Flexbox and CSS Grid, so columns stretch or wrap, e.g. .cards { display: grid; grid-template-columns: 1fr; }.
Flexible images: img { max-width: 100%; height: auto; } keeps images inside their containers; srcset/picture serve smaller files to phones.
Media queries: CSS rules applied only when conditions match: @media (min-width: 600px) { .cards { grid-template-columns: 1fr 1fr; } } and @media (orientation: landscape) { ... }.
Mobile-first: base styles are written for small screens, and min-width queries progressively add layout for larger screens. This loads less CSS on phones, focuses on key content and fits progressive enhancement.
Other good practices: readable font sizes (16 px+), touch targets about 48 px, hamburger menus instead of hover menus, compressed and lazy-loaded images, and testing in Chrome DevTools device mode and on real devices.
6. What is mobile-first design?
Model answer
Writing the base CSS for the smallest screens first and then adding min-width media queries to enhance the layout for tablets and desktops. Phones load less CSS and the design focuses on essential content.
7. What is responsive web design? Name its three ingredients.
Model answer
Responsive web design makes a single page adapt its layout to any screen size and orientation. Ingredients: fluid grids (relative units, Flexbox/Grid), flexible images (max-width 100%) and CSS media queries.
8. What is the purpose of the viewport meta tag?
Model answer
It tells mobile browsers to set the layout width equal to the device width (width=device-width) and to start at 100% zoom (initial-scale=1.0). Without it, phones render pages at about 980 px and zoom out, making text tiny and media queries ineffective.
9. List six HTML5 semantic elements.
Model answer
header, nav, main, section, article, aside, footer (also figure/figcaption).
10. Name five HTML5 input types and the keyboard or control each shows on a phone.
Model answer
email – keyboard with @ and .com; tel – numeric dial pad; number – number keyboard; date – calendar picker; url – keyboard with / and .com (also range – slider, color – colour picker).
11. Describe any five HTML5 APIs that are useful for mobile web applications, with examples.
Model answer
- Geolocation API – obtains the user's location after permission over HTTPS:
navigator.geolocation.getCurrentPosition(success, error); used in maps, delivery and attendance apps. - Web Storage API –
localStorage(permanent) andsessionStorage(per tab) store key–value data such as settings or a cart:localStorage.setItem("theme", "dark"). IndexedDB stores larger structured data offline. - Device Orientation and Motion – events
deviceorientationanddevicemotiongive tilt and acceleration for games, compasses and step counters. - Media Capture / getUserMedia – access camera and microphone for video calls, QR scanning or profile photos; a file input with
captureopens the camera directly. - Service Worker and Cache API – cache files for offline use and receive push messages – the basis of PWAs.
Others: Vibration API (
navigator.vibrate(200)), Notifications API, Web Share API (navigator.share), Canvas for 2-D graphics.
12. Differentiate between localStorage and sessionStorage.
Model answer
Both store key–value strings in the browser (about 5 MB). localStorage keeps data permanently until cleared by code or the user; sessionStorage keeps data only for the current tab session and is deleted when the tab is closed.
13. Compare HTML5 mobile web apps, Progressive Web Apps and native apps. Explain how a PWA is made installable and offline-capable.
Model answer
An HTML5 mobile web app is a responsive website that runs in the mobile browser. It needs no installation, uses one code base and updates instantly, but has limited device access, usually needs internet and is not listed in stores.
A Progressive Web App is a web app enhanced with modern APIs so it behaves like a native app. It is made installable by a web app manifest (manifest.json) containing name, short_name, icons, start_url, theme_color and display: "standalone", linked from the page; the browser then offers "Install" / "Add to Home Screen". It becomes offline-capable through a service worker – a background JavaScript file registered with navigator.serviceWorker.register("sw.js") that caches HTML, CSS, JS and images (Cache API) during installation and serves them when there is no network; it also enables push notifications. PWAs must be served over HTTPS.
A native app is built with the platform SDK (Kotlin/Java for Android, Swift for iOS), installed from an app store, and has full access to device hardware with the best performance, but requires separate code per platform and store approval.
| Web app | PWA | Native | |
|---|---|---|---|
| Install | No | Home screen | Store |
| Offline | No | Yes (service worker) | Yes |
| Device access | Basic | More (push, some sensors) | Full |
| Updates | Instant | Instant | Store update |
| Code base | One | One | Per platform |
Native apps can also include web content through a WebView component, which hybrid frameworks use for their entire UI.
This lesson ends with a quiz of 32 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Week 1 Lab 1 – Install Android Studio and Create a "Hello World" Application · Next: Week 2 Lab 2 – Create a Mobile-Friendly Web Page with Headings, Images, Links and a Form
All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester