HTML 5 Interview Questions & Answers
HTML5 interview questions: HTML vs HTML5, semantic elements, canvas vs SVG, new multimedia and form elements, DOCTYPE, data attributes and localStorage vs sessionStorage vs cookies.
What is DOCTYPE in HTML?
The <!DOCTYPE> declaration tells the browser which version of HTML the page uses, so it can show the page in the correct way. In HTML5 it is very short, and it must be the first line of the page.
<!DOCTYPE html>
<html lang="en">
...
</html>
It is not an HTML tag and it is not case-sensitive. In HTML 4 the doctype was long and had a link to a DTD file. HTML5 removed all that. See HTML Page Structure.
Will HTML5 work without DOCTYPE?
The page will still open, but the browser goes into quirks mode. In this mode the browser copies the old behaviour of very old browsers, so spacing, box sizes and some CSS may look different and break the layout.
With <!DOCTYPE html> the browser uses standards mode and shows the page as the HTML5 and CSS rules say. So always add it as the first line. Read What is DOCTYPE in HTML?
How is the page structure of HTML5 different from previous HTML versions?
HTML5 made the page structure simpler and more meaningful. The two main differences are a shorter head and semantic layout tags.
| Point | Older HTML | HTML5 |
|---|---|---|
| Doctype | Long, with a DTD link | <!DOCTYPE html> |
| Charset | Long http-equiv meta tag | <meta charset="UTF-8"> |
| Layout | Many <div id="header"> tags | header, nav, main, section, footer |
<!-- Older style -->
<div id="header">...</div>
<div id="nav">...</div>
<div id="footer">...</div>
<!-- HTML5 style -->
<header>...</header>
<nav>...</nav>
<footer>...</footer>
What are the different new form element types in HTML5?
HTML5 added many new input types, which give built-in checks and better keyboards on phones.
| Type | Use |
|---|---|
email | Email address |
number | Numbers, with min and max |
tel | Phone number |
url | Website address |
date, time, month, week | Date and time pickers |
color | Colour picker |
range | Slider |
search | Search box |
It also added the <datalist> element for suggestions, the <output> element for results, and attributes like placeholder, required, pattern, autofocus and min/max.
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
</datalist>
Full list in HTML Input Types.
What are the new multimedia elements in HTML5?
HTML5 added elements that play media and draw graphics without any plugin like Flash.
<video>plays a video.<audio>plays sound.<source>gives more than one file format for video or audio.<track>adds subtitles to a video.<canvas>is a drawing area for JavaScript. SVG can also be used inline for vector graphics.
<video width="400" controls>
<source src="movie.mp4" type="video/mp4">
</video>
<audio controls>
<source src="song.mp3" type="audio/mpeg">
</audio>
See HTML5 Video, HTML5 Audio and HTML5 Canvas.
What is the difference between HTML and HTML5?
HTML5 is the latest major version of HTML. It added new elements and features, so many things that needed plugins can now be done with plain HTML.
| Point | HTML 4 | HTML5 |
|---|---|---|
| Doctype | Long and complex | <!DOCTYPE html> |
| Layout tags | Only div | header, nav, main, footer and more |
| Media | Needed Flash | audio and video |
| Graphics | Images only | canvas and inline SVG |
| Forms | Few input types | email, date, number, range and more |
| Old tags | font, center, frame allowed | Removed. Use CSS |
| Storage | Cookies only | localStorage and sessionStorage |
Read HTML5 Introduction.
What are semantic elements in HTML5? Give examples
Semantic elements are tags whose name tells what the content is. For example <nav> holds navigation links and <footer> is the bottom part of a page. A <div> tells nothing.
header,nav,main,footersection,article,asidefigure,figcaption,time
<header>Site title</header>
<nav>Menu</nav>
<main>
<article>Blog post</article>
<aside>Related links</aside>
</main>
<footer>Copyright</footer>
They help SEO, accessibility and make the code easy to read. See HTML Semantic Elements.
What is the difference between canvas and SVG in HTML5?
Canvas draws pixels using JavaScript. SVG describes shapes as XML elements.
| Point | Canvas | SVG |
|---|---|---|
| Type | Raster (pixels) | Vector (shapes) |
| Quality when zoomed | Becomes blurry | Always sharp |
| Elements in the page | One canvas element only | Each shape is an element |
| CSS and events on shapes | No | Yes |
| Best for | Games, charts with lots of data, image effects | Icons, logos, simple charts |
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="orange" />
</svg>
See HTML5 Canvas.
What are data attributes in HTML5?
Data attributes let you store your own extra information in any HTML element. The name must start with data-.
<button data-product-id="105" data-price="499">Add to cart</button>
<script>
const btn = document.querySelector("button");
console.log(btn.dataset.productId); // 105
console.log(btn.dataset.price); // 499
</script>
In JavaScript they are read from element.dataset, and a name like data-product-id becomes productId. They are not shown on the page. See HTML Attributes.
What is the difference between localStorage, sessionStorage and cookies?
All three store data in the visitor’s browser, but they work differently.
| Point | localStorage | sessionStorage | Cookies |
|---|---|---|---|
| Lifetime | Until deleted | Until the tab is closed | Until the expiry date |
| Size | About 5 MB | About 5 MB | About 4 KB |
| Sent to server with every request | No | No | Yes |
| Use for | Settings, theme, saved drafts | Data of one tab session | Login sessions, tracking |
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme")); // dark
sessionStorage.setItem("step", "2");
Never store passwords or other secrets in web storage, because any JavaScript on the page can read it. Web storage is a part of HTML5.