Top HTML Interview Questions & Answers

Top HTML Interview Questions & Answers Collection

Here, you can find Frequently Asked HTML Interview Questions & Answers for jobs. These are top Interview Questions related to HTML basic tags, images, links, table, forms and other elements.

Tutorials Class covers Interview Questions for Freshers as well as experienced professionals.

What is the difference between inline, internal and external CSS?

There are three ways to add CSS to HTML.

TypeWhereBest for
Inlinestyle attribute on one elementQuick tests, one-off change
Internal<style> tag in the headOne page only
ExternalSeparate .css file linked with <link>Whole websites. Best choice
<!-- inline -->
<p style="color:red;">Text</p>

<!-- internal -->
<style>
  p { color: red; }
</style>

<!-- external -->
<link rel="stylesheet" href="style.css">

External CSS is the best for real sites, because one file styles all the pages and the browser keeps it in the cache. See HTML Styles.

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.