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.

PointOlder HTMLHTML5
DoctypeLong, with a DTD link<!DOCTYPE html>
CharsetLong http-equiv meta tag<meta charset="UTF-8">
LayoutMany <div id="header"> tagsheader, 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>

See HTML Semantic Elements.

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.

TypeUse
emailEmail address
numberNumbers, with min and max
telPhone number
urlWebsite address
date, time, month, weekDate and time pickers
colorColour picker
rangeSlider
searchSearch 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.

PointHTML 4HTML5
DoctypeLong and complex<!DOCTYPE html>
Layout tagsOnly divheader, nav, main, footer and more
MediaNeeded Flashaudio and video
GraphicsImages onlycanvas and inline SVG
FormsFew input typesemail, date, number, range and more
Old tagsfont, center, frame allowedRemoved. Use CSS
StorageCookies onlylocalStorage 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, footer
  • section, article, aside
  • figure, 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.

PointCanvasSVG
TypeRaster (pixels)Vector (shapes)
Quality when zoomedBecomes blurryAlways sharp
Elements in the pageOne canvas element onlyEach shape is an element
CSS and events on shapesNoYes
Best forGames, charts with lots of data, image effectsIcons, logos, simple charts
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="orange" />
</svg>
Output in the browser

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.

PointlocalStoragesessionStorageCookies
LifetimeUntil deletedUntil the tab is closedUntil the expiry date
SizeAbout 5 MBAbout 5 MBAbout 4 KB
Sent to server with every requestNoNoYes
Use forSettings, theme, saved draftsData of one tab sessionLogin 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.