HTML questions open most frontend and web developer interviews. These HTML interview questions cover elements, attributes and document structure for freshers, then semantic HTML, forms, accessibility, SEO, performance and modern HTML5 APIs for experienced developers.
30 questions · Updated
Reading answers is not the same as saying them.Practise HTML questions out loud and get a score, what you missed and a model answer for each one.
Practise free with AIHTML Interview Questions: 30 questions with model answers on this page.
Basic HTML interview questions
Fundamentals, definitions and simple scenarios. Good for freshers and warm-ups.
1. What is HTML, and what is the structure of an HTML document?
HTML (HyperText Markup Language) describes the structure and meaning of content on a web page. A document starts with <!DOCTYPE html>, then an <html lang="en"> element containing <head>, with metadata such as <meta charset="utf-8">, the viewport meta tag, <title> and links to CSS, and <body>, which holds the visible content.
2. What is the difference between block and inline elements?
Answer flow · 3 steps
1
Block elements such as div, p, h1 and section start on a new line and take the full available width.
2
Inline elements such as span, a, strong and img sit within a line of text and only take the width of their content; width and height do not apply to non-replaced inline elements.
3
CSS display can change this, and inline-block combines both behaviours.
3. What are semantic elements? Give examples.
Answer flow · 3 steps
1
Semantic elements describe their meaning, such as header, nav, main, article, section, aside, footer, figure, time and button.
2
They help screen readers navigate, help search engines understand content, and make code easier to read.
3
Using a div for everything loses this meaning.
4. What does <!DOCTYPE html> do?
Answer flow · 3 steps
1
It tells the browser that the page uses modern HTML, so it renders in standards mode.
2
Without it, browsers switch to quirks mode, which imitates old browser bugs, for example in how the box model and some CSS behave, which causes layout problems.
3
It must be the very first line of the document.
5. Which meta tags should every page have?
<meta charset="utf-8"> so characters display correctly, <meta name="viewport" content="width=device-width, initial-scale=1"> so the page works on phones, a unique <title> and <meta name="description"> for search results, and Open Graph tags such as og:title and og:image for link previews on social media and messaging apps.
6. What is the difference between id and class?
Answer flow · 3 steps
1
An id must be unique on the page and identifies one element, used for links to a section (#contact), label for attributes and JavaScript lookups.
2
A class can be used on many elements and an element can have many classes, so classes are used for styling.
3
Styling with ids is avoided because their high specificity makes CSS harder to override.
7. How do you build an accessible table?
Answer flow · 3 steps
1
Use tables only for tabular data, not layout.
2
Add a <caption> describing the table, group rows with <thead>, <tbody> and <tfoot>, and use <th> for headers with scope="col" or scope="row" so screen readers announce the right header for each cell.
3
For complex tables, headers and id attributes link cells to headers.
8. How should images be added to a page?
Answer flow · 3 steps
1
Use <img> with a src, an alt attribute that describes the image's purpose (or alt="" for purely decorative images so screen readers skip them), and width and height attributes so space is reserved and the layout does not jump.
2
Wrap images with captions in <figure> and <figcaption>.
3
Use loading="lazy" for images further down the page.
9. What do target="_blank" and rel="noopener" do on links?
Answer flow · 3 steps
1
target="_blank" opens the link in a new tab.
2
In older browsers the new page could access the original page through window.opener and redirect it, a phishing risk called tabnabbing; rel="noopener" (and noreferrer) prevents that, and modern browsers now apply noopener by default.
3
Opening new tabs should be used sparingly and ideally signalled to users.
10. What input types does HTML5 provide?
email, tel, url, number, range, date, time, datetime-local, month, week, color, search, file, password, checkbox and radio. The right type brings the right keyboard on phones, built-in validation (for example email format), and native pickers for dates and colours, with less JavaScript.
11. What is the difference between <b> and <strong>, and <i> and <em>?
<strong> means the text is important and <em> means it is stressed, which changes the meaning; screen readers can convey them. <b> and <i> only change the look (bold and italic) without extra meaning, used for things like keywords, product names or terms in another language. Choose by meaning, and use CSS when you only want styling.
Intermediate HTML interview questions
Applied problems, trade-offs and questions about your own projects.
12. What is the difference between the script tag's async and defer attributes?
A normal script blocks HTML parsing while it downloads and runs. async downloads in parallel and runs as soon as it is ready, in any order, which suits independent scripts like analytics. defer downloads in parallel and runs in order after the HTML is parsed, before DOMContentLoaded, which suits most application scripts. Module scripts are deferred by default.
13. How do you make an HTML form accessible and user-friendly?
Every input has a visible label linked with for and id, related fields are grouped with fieldset and legend, and the right input types (email, tel, number, date) bring the correct mobile keyboards and validation. I use autocomplete attributes, required and pattern for native validation, clear error messages linked with aria-describedby, and a real button with type="submit".
14. What are data attributes?
Answer flow · 3 steps
1
data-* attributes store custom data on an element, such as <button data-product-id="42">.
2
JavaScript reads them through element.dataset.productId, and CSS can select them with attribute selectors.
3
They are useful for passing small amounts of data to scripts without non-standard attributes or hidden elements.
15. What is the difference between localStorage, sessionStorage and cookies?
localStorage stores data in the browser with no expiry, per origin, about 5 to 10 MB. sessionStorage is the same but cleared when the tab closes. Cookies are small (about 4 KB), are sent to the server with every request, can expire, and can be HttpOnly so JavaScript cannot read them, which makes them the right place for session tokens.
16. What is an iframe, and how do you use it safely?
Answer flow · 3 steps
1
An <iframe> embeds another web page, such as a YouTube video or a map, inside the current page.
2
Use the sandbox attribute to restrict what the embedded page can do, the allow attribute to grant only needed features such as fullscreen, a title for screen readers and loading="lazy".
3
To stop your own site being embedded by others (clickjacking), send a frame-ancestors Content Security Policy.
17. What is the difference between canvas and SVG?
SVG describes graphics as shapes in the DOM, so they scale without losing quality, can be styled with CSS, are accessible and respond to events per shape; they suit icons, logos, charts and diagrams. Canvas is a pixel bitmap drawn with JavaScript, which is faster for thousands of objects, games, image editing and real-time visualisation, but needs extra work for accessibility.
18. How do you add audio and video to a page?
Use the <video> and <audio> elements with controls, several <source> elements in different formats, and <track kind="captions"> for subtitles, which are essential for accessibility. preload controls how much is downloaded, poster shows an image before playback, and autoplaying video must be muted and should be avoided for users who prefer reduced motion.
19. What is the <dialog> element?
Answer flow · 3 steps
1
<dialog> is a native modal or non-modal dialog. showModal() opens it as a modal with a backdrop, makes the rest of the page inert, traps focus inside and closes on Escape; close() closes it.
2
A form with method="dialog" closes it when submitted.
3
It replaces much custom modal JavaScript and gets accessibility right by default.
20. What are <details> and <summary>?
Answer flow · 3 steps
1
They create a native disclosure widget: the <summary> is always visible and clicking it shows or hides the rest of the <details> content, with no JavaScript.
2
They are keyboard accessible by default and suit FAQs and expandable sections.
3
The open attribute controls the state, and the name attribute groups several details so only one is open at a time.
21. What are web components?
Web components are a set of browser standards for building reusable custom elements that work in any framework: custom elements (defining your own tags like <user-card> with a class), Shadow DOM (encapsulated markup and styles), and HTML templates and slots. They suit design systems shared across apps built with different frameworks.
22. What are Open Graph tags?
Answer flow · 3 steps
1
Open Graph meta tags control how a link looks when shared on WhatsApp, LinkedIn, Facebook, Slack and similar apps: og:title, og:description, og:image (ideally 1200 by 630 pixels), og:url and og:type.
2
Twitter/X uses twitter:card tags but falls back to Open Graph.
3
A good preview image and title noticeably increase clicks on shared links.
High level HTML interview questions
System design, deep internals, leadership and tough follow-ups.
23. Which HTML practices help SEO?
A unique, descriptive title and meta description on every page, one h1 and a logical heading order, semantic elements, descriptive link text, alt text on meaningful images, canonical links to avoid duplicates, the lang attribute, structured data with JSON-LD, and server-rendered content so crawlers see it without running JavaScript. Fast loading and mobile-friendly pages also help.
24. How can HTML improve page loading performance?
Use responsive images with srcset and sizes, modern formats with picture, width and height attributes to prevent layout shift, loading="lazy" for below-the-fold images and iframes, and fetchpriority="high" for the main hero image. Preload critical fonts and resources, preconnect to important origins, defer non-critical scripts, and inline critical CSS.
25. What is ARIA, and when should you use it?
Answer flow · 3 steps
1
ARIA (Accessible Rich Internet Applications) attributes add roles, states and properties for assistive technologies, such as role="dialog", aria-expanded and aria-live.
2
The first rule of ARIA is not to use it when a native element does the job: a real button is better than a div with role="button".
3
ARIA is for custom widgets such as tabs, comboboxes and live updates, and incorrect ARIA makes accessibility worse.
26. What are preload, prefetch and preconnect?
They are resource hints in <link> tags. preload fetches a resource needed for the current page early and at high priority, such as the main font or hero image. prefetch downloads a resource likely needed for the next navigation at low priority. preconnect opens a connection (DNS, TCP and TLS) to an important third-party origin in advance. Overusing them wastes bandwidth and can slow the page.
27. What is the Shadow DOM?
Answer flow · 3 steps
1
The Shadow DOM attaches a hidden, separate DOM tree to an element, with its own scoped styles that do not leak in or out.
2
Browsers use it for built-in elements such as <video> controls, and web components use it for encapsulation.
3
Content from outside is shown inside through slots, and the component can expose styling hooks through CSS custom properties and ::part.
28. How do you add structured data to a page?
Answer flow · 3 steps
1
Structured data describes the page's content to search engines using schema.org vocabulary, most commonly as JSON-LD in a <script type="application/ld+json"> tag, for types such as Article, Product, BreadcrumbList, Organization and Event.
2
It can enable rich results such as star ratings and breadcrumbs.
3
It must match the visible content, and can be checked with Google's Rich Results Test.
29. What is a Content Security Policy?
Answer flow · 3 steps
1
A Content Security Policy is an HTTP header (or meta tag) that tells the browser which sources of scripts, styles, images, frames and connections are allowed.
2
It is a strong defence against cross-site scripting, because injected inline scripts will not run unless allowed by a nonce or hash.
3
A strict policy uses nonces with strict-dynamic, and Report-Only mode helps roll it out safely.
30. How does the browser parse HTML?
Answer flow · 4 steps
1
The parser reads bytes, decodes them into characters, turns them into tokens and builds the DOM tree.
2
It is very forgiving and fixes invalid markup in standard ways, which can move elements unexpectedly, for example a <div> inside a <p> closes the paragraph.
3
A preload scanner looks ahead for resources to fetch early.
4
Synchronous scripts pause parsing, which is why defer and async exist.
Ready to test yourself?Pick your topics and level, answer by voice or text, and get instant feedback. Free.