Top HTML Interview Questions & Answers
Top 23 HTML interview questions: what HTML is, tags vs elements, attributes, page structure, inline vs block, id vs class, images, video, tables, GET vs POST and the viewport meta tag.
What is the difference between inline, internal and external CSS?
There are three ways to add CSS to HTML.
| Type | Where | Best for |
|---|---|---|
| Inline | style attribute on one element | Quick tests, one-off change |
| Internal | <style> tag in the head | One page only |
| External | Separate .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.
| 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.
- « Previous
- 1
- 2