74 questions - Beginner friendly

HTML interview questions and answers

HTML gives a web page its structure. Almost every frontend interview starts here, because clean HTML usually means clean CSS and JavaScript too. Read each answer once and say it back in your own words.

Last updated:

HTML basics

The first questions in almost every frontend interview.

What is HTML?

HTML means HyperText Markup Language. It is not a programming language. It is a markup language: you use tags to say what each piece of content is. This is a heading. This is a paragraph. This is an image. The browser reads those tags and draws the page.

What is the difference between HTML and HTML5?

HTML5 is the newest version of HTML. It added meaning tags like header, nav, main, article and footer. It added new input types like email, date and number. It added audio, video and canvas, so you no longer need Flash. It also added local storage and other browser APIs. Today "HTML" almost always means HTML5.

What is the difference between an element and a tag?

A tag is the text you type, like <p>. An element is the full thing: the opening tag, the content inside, and the closing tag. So <p>Hello</p> is one element made of two tags.

<p>Hello</p>
<!-- <p> and </p> are tags. The whole line is one element. -->

What is an attribute?

An attribute is extra information you write inside the opening tag. The shape is always name="value". For example href tells a link where to go. src tells an image which file to load.

<a href="/pricing" title="See plans">Pricing</a>
<img src="/logo.png" alt="Company logo" />

What does <!DOCTYPE html> do?

It tells the browser that this page is HTML5. The browser then follows the modern rules, called standards mode. If you forget it, the browser uses quirks mode and copies old broken behaviour from the 1990s. Your CSS box sizes can then go wrong.

What is the basic structure of an HTML page?

Doctype first. Then <html>. Inside it, <head> holds information about the page. <body> holds the content people actually see.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My page</title>
  </head>
  <body>
    <h1>Hello</h1>
  </body>
</html>

What is the difference between <head> and <body>?

<head> holds information about the page: the title, the character set, CSS links, meta tags for SEO. Nothing inside <head> is drawn on the screen. <body> holds everything the user sees and clicks.

Why do we write <meta charset="UTF-8">?

It tells the browser which character set to use while reading the file. UTF-8 covers almost every language and symbol. Without it, names, the rupee sign and emojis can turn into strange boxes.

What does the viewport meta tag do?

It tells a mobile browser to use the real width of the phone. Without it, the phone pretends to be a 980px desktop. Your responsive CSS then does not work and the page looks zoomed out.

<meta name="viewport" content="width=device-width, initial-scale=1" />

What is the difference between block and inline elements?

A block element starts on a new line and takes the full width it can. Examples: <div>, <p>, <h1>. An inline element stays inside the line of text and takes only the width it needs. Examples: <span>, <a>, <strong>.

What are void or self-closing elements?

They are elements that cannot hold anything inside, so they have no closing tag. Common ones are <br>, <hr>, <img>, <input>, <meta> and <link>.

What is the difference between <div> and <span>?

Both have no meaning of their own. They are only boxes. <div> is a block box used for layout sections. <span> is an inline box used to wrap a few words inside a sentence.

Why should a page have only one <h1>?

The <h1> is the main title of the page. Screen readers and search engines use it to know what the page is about. Many <h1> tags make that unclear. Use <h2> and <h3> for the parts below it, in order, without skipping a level.

What are HTML entities?

Entities are codes for characters that you cannot type directly. Some characters have a meaning in HTML, so writing them plainly would break the page. Write &lt; for <, &gt; for >, &amp; for &, and &nbsp; for a space that never breaks a line.

<p>Use &lt;div&gt; for layout</p>
<!-- shows: Use <div> for layout -->
<p>Rs&nbsp;999</p>
<!-- the price never splits across two lines -->

How do you write a comment in HTML?

Put the text between <!-- and -->. The browser ignores it. Remember that anyone can still read it with view-source, so never write secrets there.

<!-- This note is only for developers -->

What is the difference between id and class?

An id must be unique on the page and points to one element. A class can be used on many elements and gives the same style or behaviour to a group. In CSS you select an id with # and a class with a dot.

What are data-* attributes?

They let you store your own data on an element without breaking HTML rules. JavaScript reads them through the dataset property. They are useful for linking a button to a record id.

<button data-user-id="42" data-role="admin">Edit</button>

<script>
  const btn = document.querySelector("button");
  console.log(btn.dataset.userId); // "42"
</script>

What is the DOM?

DOM means Document Object Model. When the browser reads your HTML, it builds a tree of objects in memory. Every tag becomes a node in that tree. JavaScript changes the page by changing this tree, not the HTML file. That is why the page updates without a reload.

What is the difference between HTML and XHTML?

XHTML is a stricter HTML written with XML rules. Every tag must be closed, tags must be lowercase, and every attribute value must be in quotes. One small mistake breaks the whole page. HTML5 forgives such mistakes, so XHTML is rare today.

Semantic HTML

Using the right tag for the right meaning.

What is semantic HTML?

Semantic HTML means picking tags that describe the meaning of the content, not the look. <header>, <nav>, <main>, <article>, <aside> and <footer> each say what that block is for. A screen reader, a search engine and a new developer can then understand the page faster.

<body>
  <header>Logo and site title</header>
  <nav>Menu links</nav>
  <main>
    <article>The blog post</article>
    <aside>Related links</aside>
  </main>
  <footer>Contact and copyright</footer>
</body>

Why is semantic HTML better than using div everywhere?

Three reasons. Accessibility: a screen reader can jump straight to the main content or the menu. SEO: search engines understand the page structure. Maintenance: the code reads like a description of the page instead of a wall of divs.

What is the difference between <article> and <section>?

An <article> makes sense on its own: a blog post, a news item, a product card. A <section> is one part of a bigger page, like the "Features" part of a landing page. Simple test: if you could copy it out and publish it alone, it is an article.

When do you use <main>?

Once per page, around the main content of that page. Things that repeat on every page stay outside it: the header, the side menu, the footer. Browsers use <main> for the "skip to content" shortcut.

What is the difference between <strong> and <b>, or <em> and <i>?

<strong> and <em> carry meaning: high importance and emphasis. A screen reader changes its tone for them. <b> and <i> only change the look and add no meaning. Use <strong> and <em> when the meaning matters.

What is <figure> and <figcaption> for?

<figure> wraps content that the text refers to, like an image, a chart or a code block. <figcaption> gives it a caption. The browser then knows that this caption belongs to that image.

<figure>
  <img src="chart.png" alt="Sales grew 40 percent in 2025" />
  <figcaption>Sales growth in 2025</figcaption>
</figure>

What does the <time> element do?

It marks a date or a time so machines can read it. People read the text inside. Machines read the datetime attribute.

<time datetime="2026-03-14">14 March 2026</time>

What are <details> and <summary>?

They make an open and close block with no JavaScript. <summary> is the line the user clicks. Everything else inside <details> is hidden until the user opens it. They are the simplest way to build an FAQ or a "read more" block.

<details>
  <summary>What is the refund policy?</summary>
  <p>Full refund within 7 days.</p>
</details>

What is the <dialog> element?

It is a built-in modal box. Call showModal() to open it and close() to close it. The browser handles the dark background, the focus trap and the Escape key for you. Before <dialog>, developers had to build all of that by hand.

<dialog id="box">
  <p>Saved.</p>
  <button onclick="box.close()">OK</button>
</dialog>
<script>box.showModal();</script>

What is the <template> element?

It holds HTML that the browser reads but does not show. You copy it with JavaScript when you need it. It is useful for repeating rows in a list without building HTML strings by hand.

<template id="row">
  <li class="item"></li>
</template>

<script>
  const tpl = document.getElementById("row");
  const node = tpl.content.cloneNode(true);
  document.querySelector("ul").appendChild(node);
</script>

Forms and input

The part of HTML that interviewers dig into most.

What is the difference between GET and POST in a form?

GET puts the data in the URL. You can see it, bookmark it and share it, but the length is limited. It suits search. POST sends the data in the request body. It is not shown in the URL and can be large. It suits creating or changing data.

Why should every input have a label?

A label tells the user and the screen reader what the field is for. Clicking the label also focuses the field, which helps a lot on mobile. Connect them with the for attribute matching the input id, or put the input inside the label.

<label for="email">Email</label>
<input id="email" name="email" type="email" />

What input types does HTML5 add?

email, url, tel, number, range, date, time, color, search and more. You get free checking from the browser. On mobile you also get the right keyboard: a number pad for tel, an @ key for email.

What is the difference between the placeholder and the label?

A placeholder is a hint that disappears when the user starts typing. So it cannot be the only description. A label always stays visible. Using a placeholder in place of a label is a common accessibility mistake.

How does built-in HTML form validation work?

Attributes like required, min, max, minlength, maxlength, pattern and type make the browser check the value before the form is sent. The browser also shows the error message. Add novalidate on the form if you want to do all checks in JavaScript.

<input
  type="text"
  name="pin"
  required
  pattern="[0-9]{6}"
  title="Enter a 6 digit PIN code"
/>

What is the difference between disabled and readonly?

A disabled field cannot be focused or edited, and it is not sent with the form. A readonly field cannot be edited, but it can be focused and copied, and it is still sent with the form.

What does the name attribute do on an input?

It is the key used when the form data goes to the server. Without a name, the value is simply not sent. The id is only for labels, CSS and JavaScript.

How do radio buttons get grouped?

By giving them the same name attribute. Only one radio in a group can be selected. Checkboxes with the same name are all sent, so they act like a multi-select list.

<input type="radio" name="plan" value="monthly" id="m" /><label for="m">Monthly</label>
<input type="radio" name="plan" value="yearly" id="y" /><label for="y">Yearly</label>

What is <fieldset> and <legend>?

<fieldset> groups related fields together. <legend> gives that group a title. A screen reader reads the legend before each field in the group, which helps a lot with a set of radio buttons.

How do you upload a file?

Use input type="file". The form needs method="post" and enctype="multipart/form-data". Without that enctype, only the file name is sent, not the file. Add accept to limit file types and multiple to allow more than one file.

<form method="post" enctype="multipart/form-data">
  <input type="file" name="resume" accept=".pdf,.docx" />
  <button type="submit">Upload</button>
</form>

What is autocomplete and why does it matter?

It tells the browser what kind of value the field expects, so the browser can fill it from saved data. Correct values like autocomplete="email" or "one-time-code" make forms much faster, above all on mobile.

What is the difference between <button> and <input type="submit">?

Both send the form. <button> can hold HTML inside it, like an icon plus text, so it is more flexible. Note that a <button> inside a form submits by default. Set type="button" when it should not submit.

What is the <datalist> element?

It gives an input a list of suggestions, but the user can still type any value. It works like an autocomplete dropdown with no JavaScript.

<input list="cities" name="city" />
<datalist id="cities">
  <option value="Bengaluru"></option>
  <option value="Pune"></option>
</datalist>

Tables and lists

Small topics that still show up in interviews.

What are the main parts of a table?

<table> wraps everything. <caption> names it. <thead> holds the header row, <tbody> holds the data rows and <tfoot> holds totals. Header cells use <th> and data cells use <td>.

<table>
  <caption>Monthly sales</caption>
  <thead>
    <tr><th scope="col">Month</th><th scope="col">Total</th></tr>
  </thead>
  <tbody>
    <tr><td>March</td><td>12,000</td></tr>
  </tbody>
</table>

What does the scope attribute do on <th>?

It says whether that header belongs to a column or to a row. A screen reader uses it to read the right header while the user moves between cells. A blind user then knows that "12,000" is the March total.

Why should tables not be used for page layout?

A table means "this is data in rows and columns". Using it for layout confuses screen readers, makes responsive design very hard and gives messy HTML. Use CSS grid or flexbox for layout.

What are the three list types in HTML?

<ul> is an unordered list, used when the order does not matter. <ol> is an ordered list, used when the order matters, and items get numbers. <dl> is a description list of term and meaning pairs, written with <dt> and <dd>.

How do colspan and rowspan work?

colspan makes one cell stretch across several columns. rowspan makes one cell stretch down several rows. Both are used for merged header cells.

Accessibility

A topic that separates a junior from a mid-level candidate.

What is web accessibility?

It means building pages that people with disabilities can use. That includes people using a screen reader, people who cannot use a mouse, and people with low vision or colour blindness. Good accessibility usually improves SEO and helps every user too.

What is ARIA?

ARIA means Accessible Rich Internet Applications. It is a set of extra attributes, such as role, aria-label and aria-expanded. They describe custom widgets to screen reader software. The first rule of ARIA is simple: if a normal HTML element already does the job, do not use ARIA.

What is aria-label used for?

It gives an element a name when there is no visible text. A button with only an X icon should have aria-label="Close dialog". Without it, a screen reader just says "button".

<button aria-label="Close dialog">
  <svg aria-hidden="true">...</svg>
</button>

What does tabindex do?

tabindex="0" puts an element into the normal keyboard tab order. tabindex="-1" makes it focusable only from JavaScript, which helps when you move focus into a modal. Positive numbers break the natural order, so avoid them.

What is a skip link?

It is a link at the very top of the page that jumps straight to the main content. Without it, keyboard users must tab through the whole menu on every page. It is usually hidden until it gets focus.

<a class="skip-link" href="#main">Skip to main content</a>
...
<main id="main">...</main>

Why is semantic HTML the easiest accessibility win?

Because real elements already come with keyboard support, focus behaviour and correct announcements. A real <button> works with Enter and Space and is read as a button. A <div onclick> gives you none of that until you build it all again by hand.

What is aria-hidden?

It hides an element from screen readers while keeping it visible on screen. Use it for icons that are only decoration. Never put it on something the user can focus, or a keyboard user will land on an element that says nothing.

HTML5 APIs and performance

Modern browser features usually asked at the end of the round.

What is the difference between localStorage, sessionStorage and cookies?

localStorage keeps data until you delete it and holds about 5MB. sessionStorage keeps data only until the tab is closed. Cookies are small, about 4KB, and go to the server with every request. That makes cookies right for login and wrong for general data.

Is localStorage safe for tokens?

No. Any JavaScript on the page can read it, including a bad script loaded from another website. So one XSS bug leaks the token. A cookie marked httpOnly cannot be read by JavaScript, so it is the safer place for a session.

What is the difference between defer and async on a script tag?

Both download the script without stopping HTML parsing. async runs it as soon as the download finishes, so the order is not fixed. defer waits until the HTML is fully parsed and then runs scripts in the order you wrote them. Use defer for your app code and async for separate things like analytics.

<script src="app.js" defer></script>
<script src="analytics.js" async></script>

Why is a script tag often placed at the end of the body?

Because a plain <script> in the <head> stops HTML parsing until it downloads and runs. The user then sees a blank page. Putting it before </body> avoids that. Today defer in the head gives the same benefit and starts the download earlier.

What is the Canvas element?

It is a blank drawing area that you paint on with JavaScript. It draws pixels, so it is fast for games and charts with many points. The shapes are not real elements, so they are not accessible and not searchable.

What is the difference between Canvas and SVG?

Canvas is pixel based and drawn by script. Once drawn, the browser forgets what the shapes were. SVG is vector based, and every shape is a real element you can style with CSS and attach events to. SVG stays sharp at any size. Canvas is faster with thousands of objects.

What is a Web Worker?

It is a script that runs on a separate thread. Heavy work then does not freeze the page. A worker cannot touch the DOM. It talks to the main thread by sending messages.

const worker = new Worker("heavy.js");
worker.postMessage({ rows: 100000 });
worker.onmessage = (e) => console.log(e.data);

What is a Service Worker?

It is a background script that sits between your page and the network. It can save files in a cache and serve them when the user is offline. It also powers push notifications. It is the base of a Progressive Web App and works only over HTTPS.

What are preload, prefetch and preconnect?

preload downloads something this page needs very soon, like a font. prefetch downloads something the next page will probably need, at low priority. preconnect opens the connection to another domain early, so the real request starts faster.

<link rel="preload" href="/fonts/main.woff2" as="font" crossorigin />
<link rel="preconnect" href="https://api.example.com" />

What is the difference between the DOMContentLoaded and load events?

DOMContentLoaded fires when the HTML is parsed and the DOM is ready, even if images are still downloading. load fires later, when every image, stylesheet and iframe has finished. Most scripts should use DOMContentLoaded.

What does the Geolocation API do?

It asks the browser for the location of the user. The browser always shows a permission prompt first, and it works only on HTTPS. The result gives latitude, longitude and accuracy.

How does the browser render an HTML page, in short?

It reads the HTML and builds the DOM tree. It reads the CSS and builds the CSSOM. It joins both into a render tree. Then it works out the size and position of every box, which is layout. Then it paints the pixels and joins the layers on screen.

Continue with another topic

Practise live

CrackInterviewAI listens to the live interview and gives a structured answer on screen for coding, system design, HR and project questions. Download for Windows.