DevAcademy
All interview questions
92+ Questions

HTML Interview Questions & Answers

Curated HTML interview questions with detailed answers, grouped by topic. Search, revise and get interview-ready.

Filter by difficulty:

HTML Introduction

Q1. What does HTML stand for, and what is it used for?

HTML stands for HyperText Markup Language. It is used to structure content on the web — headings, paragraphs, links, images, and forms — so browsers know what to display and how.

Q2. Is HTML a programming language?

No. HTML is a markup language, not a programming language — it has no variables, loops, or conditional logic. It only describes structure and meaning; logic comes from JavaScript.

Q3. What is the DOM, and how does it relate to HTML?

IntermediateLearn topic →

The DOM (Document Object Model) is the tree-like structure a browser builds in memory after parsing an HTML document. JavaScript interacts with the page by reading and modifying this DOM, not the raw HTML text.

Q4. How do HTML, CSS, and JavaScript work together?

HTML provides structure and content, CSS controls presentation and layout, and JavaScript adds behavior and interactivity. They are separate layers that combine to build a complete web page.

HTML Setup

Q5. What tools do you need to start writing HTML?

Just a text editor and a web browser. No compiler, build tool, or server is required to write and view a basic HTML page.

Q6. What file extension do HTML files use?

.html (or the older .htm), which tells the operating system and browser to render the file as a web page.

Q7. Why is a homepage conventionally named index.html?

IntermediateLearn topic →

Web servers are configured to automatically serve a file named index.html when a directory URL is requested without a specific filename, making it the default entry point.

HTML Document Structure

Q8. What does <!DOCTYPE html> do?

It tells the browser to render the page using the HTML5 standards mode, avoiding inconsistent "quirks mode" rendering used for very old, non-standard documents.

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

<head> contains metadata not shown directly on the page (title, stylesheets, meta tags), while <body> contains everything the user actually sees and interacts with.

Q10. Why should <meta charset="UTF-8" /> be one of the first things in <head>?

IntermediateLearn topic →

It declares the character encoding early so the browser interprets all subsequent text — including special characters and emoji — correctly before rendering begins.

Q11. How many <html>, <head>, and <body> elements should a valid document have?

Exactly one of each. Having more than one of any of these elements produces invalid HTML and can lead to unpredictable rendering.

Elements & Tags

Q12. What is the difference between a tag and an element?

A tag is the markup itself, like <p> or </p>. An element is the opening tag, its content, and its closing tag together, e.g. <p>Hello</p>.

Q13. What is a void element? Give examples.

A void element has no content and no closing tag, such as <img>, <br>, <hr>, and <input>. They are self-contained and self-closing by nature.

Q14. What happens if you incorrectly nest closing tags?

IntermediateLearn topic →

Closing tags must close in the reverse order they were opened. Crossing tags (e.g. <p><strong>text</p></strong>) produces invalid HTML that browsers try to auto-correct, often causing unexpected rendering.

HTML Attributes

Q15. What is an HTML attribute, and where is it written?

An attribute provides extra information about an element, written as name="value" pairs inside the element’s opening tag, e.g. <a href="...">.

Q16. What is a boolean attribute? Give an example.

IntermediateLearn topic →

A boolean attribute takes no value — its mere presence enables a feature. Examples include disabled, required, and checked.

Q17. What is the difference between the class and id attributes?

class can be applied to many elements and reused for shared styling, while id must be unique within the page and is typically used for a single, specific target like a same-page anchor or JavaScript hook.

Q18. Why should attribute values always be quoted?

IntermediateLearn topic →

Unquoted values break as soon as they contain a space or special character. Quoting (single or double) keeps the value unambiguous and is considered best practice even when technically optional.

HTML Comments

Q19. How do you write a comment in HTML?

With <!-- comment text -->. Anything between the markers is ignored by the browser and never rendered.

Q20. Are HTML comments visible to users?

Not in the rendered page, but anyone can see them by viewing the page source, so sensitive information should never be placed inside a comment.

Q21. Can HTML comments be nested?

IntermediateLearn topic →

No. Placing <!-- --> inside another comment closes the outer comment early, which can produce broken or unexpected markup.

Headings & Paragraphs

Q22. How many heading levels does HTML provide?

Six, from <h1> (most important) to <h6> (least important), used to build a logical outline of the page content.

Q23. How many <h1> elements should a well-structured page have?

IntermediateLearn topic →

Typically exactly one, representing the main topic of the page — similar to a book’s title — with subsequent sections using <h2> and deeper levels.

Q24. Why shouldn’t you choose a heading level just because of its default font size?

IntermediateLearn topic →

Heading levels should reflect document structure, not visual appearance. Skipping levels for styling purposes breaks the logical outline that screen readers rely on for navigation — use CSS for styling instead.

Text Formatting

Q25. What is the difference between <strong>/<em> and <b>/<i>?

IntermediateLearn topic →

<strong> and <em> carry semantic meaning (importance and emphasis) that screen readers announce differently, while <b> and <i> are purely visual with no added meaning.

Q26. What does <br> do, and when should it be avoided?

<br> forces a single line break within a block of text. It should not be used to create space between paragraphs — separate <p> elements or CSS margin are the correct tools for that.

Q27. What are <sub> and <sup> used for?

<sub> renders subscript text (like the 2 in H2O) and <sup> renders superscript text (like the 10 in 2^10), both shifting the text’s baseline and typically reducing its size.

HTML Images

Q32. What is the purpose of the alt attribute on an <img>?

It provides a text alternative for the image — read aloud by screen readers, shown if the image fails to load, and used by search engines to understand the image content.

Q33. What alt value should a purely decorative image use, and why?

IntermediateLearn topic →

An empty string, alt="", so screen readers skip over it entirely instead of reading out an unhelpful file name for an image that carries no meaning.

Q34. Why should you set width and height on an <img>?

IntermediateLearn topic →

It lets the browser reserve the correct space for the image before it finishes loading, preventing surrounding content from jumping around (layout shift) as the page renders.

Q35. What does loading="lazy" do on an image?

IntermediateLearn topic →

It tells the browser to defer loading that image until it is close to entering the viewport, improving initial page load performance for pages with many offscreen images.

HTML Lists

Q36. What is the difference between <ul> and <ol>?

<ul> creates an unordered (bulleted) list for items with no particular sequence, while <ol> creates an ordered (numbered) list for sequential items.

Q37. What must be the direct parent of an <li> element?

A <ul> or <ol> element. <li> elements placed directly inside anything else are invalid HTML.

Q38. What is a description list used for?

IntermediateLearn topic →

A <dl> pairs terms (<dt>) with their definitions (<dd>) — useful for glossaries, metadata, or key-value style content.

Div & Span

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

<div> is a block-level generic container, starting on a new line and taking full width. <span> is an inline generic container, flowing with surrounding text and only as wide as its content.

Q40. Why should you consider semantic elements before using a <div>?

IntermediateLearn topic →

Semantic elements like <header>, <nav>, and <article> describe their content’s role, improving accessibility and SEO, whereas <div> carries no meaning at all — it should be a fallback, not a default.

Q41. What is "divitis"?

IntermediateLearn topic →

A term for markup that overuses <div> for everything, making the structure harder to read, style, and understand for both developers and assistive technology.

HTML Tables

Q42. What is the difference between <th> and <td>?

<th> defines a header cell (bold and centered by default, with semantic meaning for assistive technology), while <td> defines a standard data cell.

Q43. What do colspan and rowspan do?

colspan merges a cell across multiple columns, and rowspan merges a cell across multiple rows, letting a single cell visually span a larger area of the table.

Q44. Why shouldn’t tables be used for overall page layout?

IntermediateLearn topic →

Using tables for layout (common in the early 2000s) hurts accessibility, since screen readers announce genuine tabular relationships that don’t exist, and it hurts responsiveness. CSS Grid or Flexbox should be used for layout instead.

Q45. What does the scope attribute on <th> do?

It tells assistive technology whether a header applies to a column ("col") or a row ("row"), which is especially important for complex tables with multiple header levels.

HTML Forms

Q46. What do the action and method attributes of a <form> control?

action specifies the URL the form data is submitted to, and method specifies how — "get" appends data to the URL, "post" sends it in the request body.

Q47. How do you correctly associate a <label> with an input?

By matching the label’s for attribute to the input’s id. This makes the input clickable via its label text and is essential for screen reader accessibility.

Q48. Why is a placeholder not a substitute for a <label>?

IntermediateLearn topic →

Placeholder text disappears once the user starts typing and is not reliably announced by all screen readers, so relying on it alone leaves the field effectively unlabeled for many users.

Q49. What do <fieldset> and <legend> do?

IntermediateLearn topic →

<fieldset> groups related form controls together (like a set of radio buttons), and <legend> provides a caption describing that group.

Input Types

Q50. Why should you choose the most specific input type available?

Specific types like email, tel, and number trigger the most appropriate mobile keyboard and provide free client-side validation, improving usability without any extra code.

Q51. How are radio buttons grouped so only one can be selected?

By giving them the same name attribute — the browser then treats them as a mutually exclusive group, regardless of how many there are.

Q52. What is the difference between checkboxes and radio buttons?

Checkboxes allow any number of independent selections, while radio buttons restrict the user to exactly one selection per group (same name attribute).

Q53. What does the accept attribute do on a file input?

IntermediateLearn topic →

It restricts which file types are shown/selectable in the file picker, e.g. accept="image/png, image/jpeg" limits selection to those image formats.

Form Validation

Q54. What does the required attribute do?

It prevents the form from submitting until that field has a value, using the browser’s built-in constraint validation with no JavaScript needed.

Q55. How does the pattern attribute work?

IntermediateLearn topic →

It requires the input’s value to match a given regular expression before the form can submit, useful for formats like ZIP codes that a specific input type doesn’t cover.

Q56. Why is client-side HTML validation not enough on its own?

Native validation can be bypassed (e.g. via dev tools or a direct request), so the server must always validate and sanitize incoming data again before trusting it.

Semantic HTML

Q57. What is semantic HTML, and why does it matter?

Semantic elements (like <header>, <nav>, <main>, <article>) describe their meaning and role, unlike generic <div>. This helps screen readers, search engines, and other developers understand the page structure.

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

IntermediateLearn topic →

<article> is for content that could stand alone, like a blog post. <section> is for a thematic grouping within a page, usually accompanied by its own heading.

Q59. How many <main> elements should a page have?

IntermediateLearn topic →

Exactly one, representing the page’s primary, unique content — excluding repeated elements like headers, footers, and navigation.

Q60. What is <figcaption> used for?

It provides a caption for a <figure>, such as an image, diagram, or code block, describing what it shows.

Block vs Inline Elements

Q61. What is the difference between a block-level and an inline element?

A block-level element starts on a new line and takes up the full available width by default; an inline element flows with surrounding content and only takes as much width as needed.

Q62. Can CSS override an element’s default block/inline behavior?

IntermediateLearn topic →

Yes — the CSS display property (block, inline, inline-block, flex, grid, none) can change how any element behaves, regardless of its default.

Q63. Why is <img> considered a special case among inline elements?

Unlike text-based inline elements, <img> is a "replaced" element and accepts width and height even though it is inline-level by default.

Classes & IDs

Q64. What is the key difference between class and id?

class can be reused across many elements for shared styling, while id must be unique across the entire page.

Q65. Why is it invalid to duplicate an id on a page?

IntermediateLearn topic →

A duplicate id can cause CSS to apply inconsistently and breaks JavaScript methods like document.getElementById() that expect exactly one match.

Q66. Can an element have multiple classes?

Yes, classes are space-separated in the class attribute, e.g. class="btn btn-primary btn-large", letting you compose small, reusable style rules.

HTML Entities

Q67. Why can’t you write < or > directly as literal text in HTML?

Those characters have special meaning — they define tags. To display them as literal text, you must use entities like &lt; and &gt; instead.

Q68. What is a numeric character reference?

IntermediateLearn topic →

A way to represent any Unicode character by its code point, like &#169; or &#x00A9; for the copyright symbol, as an alternative to named entities.

Q69. Why is escaping user-generated content important?

Failing to escape characters like < and & in content coming from users can allow malicious markup or scripts to be injected into the page — a cross-site scripting (XSS) vulnerability.

Head Elements

Q70. What does the viewport meta tag do?

It controls how a page scales on mobile devices. Without it, mobile browsers render the page at a wide desktop width and zoom out, breaking responsive layouts.

Q71. What is the difference between <style> and <link rel="stylesheet">?

<style> embeds CSS rules directly inline in the document, while <link rel="stylesheet"> loads CSS from a separate, cacheable file.

Q72. What does the defer attribute do on a <script> tag?

IntermediateLearn topic →

It delays execution of the script until after the HTML has finished parsing, while still preserving the order scripts appear in — commonly used for scripts that need the full DOM.

IFrames

Q73. What does an <iframe> do?

It embeds another HTML document inside the current page, rendered in its own independent browsing context — commonly used for maps, videos, and third-party widgets.

Q74. What does the sandbox attribute do on an iframe?

It applies restrictions to the embedded content, such as disabling scripts or form submission, as a security measure when embedding untrusted content.

Q75. Why should every iframe have a title attribute?

IntermediateLearn topic →

It gives screen reader users an accessible name describing the embedded content before they enter it — without one, the iframe is announced with no useful context.

Audio & Video

Q76. What does the controls attribute do on <video> or <audio>?

It shows the browser’s built-in play/pause/volume UI, letting users control playback without any custom JavaScript.

Q77. Why would you provide multiple <source> elements inside a <video>?

IntermediateLearn topic →

Different browsers support different video formats. Multiple sources let the browser pick the first format it supports, providing broader compatibility.

Q78. What does the <track> element add to a video?

IntermediateLearn topic →

Captions or subtitles, loaded from a WebVTT file, improving accessibility for deaf and hard-of-hearing users.

Q79. Why is autoplay with sound often blocked by browsers?

IntermediateLearn topic →

Browsers restrict autoplaying audio/video with sound to avoid disrupting users unexpectedly. Autoplay generally only works reliably when the media is also muted.

Meta Tags & SEO

Q80. What is the purpose of the meta description tag?

It provides a short summary of the page, often shown as the snippet text in search engine results, influencing click-through rates.

Q81. What do Open Graph meta tags control?

IntermediateLearn topic →

They control how a link preview appears when shared on social platforms like Facebook, LinkedIn, and Slack — the preview title, description, and image.

Q82. What does <meta name="robots" content="noindex, nofollow" /> do?

IntermediateLearn topic →

It tells search engines not to index the page or follow its links — useful for staging environments, thank-you pages, or duplicate content.

Data Attributes

Q83. What is a data-* attribute used for?

It stores custom data directly on an HTML element, without inventing new HTML attributes, for use by CSS attribute selectors or JavaScript.

Q84. How do you read data attributes in JavaScript?

IntermediateLearn topic →

Through the element’s dataset property, e.g. element.dataset.userId, which automatically converts kebab-case attribute names to camelCase.

Q85. Should sensitive information be stored in data attributes?

IntermediateLearn topic →

No — data attributes are visible in the page source and can be read or modified by anyone using browser dev tools.

SVG Basics

Q86. What does SVG stand for, and why is it useful?

Scalable Vector Graphics — an XML-based format for drawing 2D graphics with shapes and paths instead of pixels, so it stays crisp at any zoom level or resolution.

Q87. What does the viewBox attribute control?

IntermediateLearn topic →

It defines the internal coordinate system of the SVG canvas independently of its displayed width/height, which is what allows the graphic to scale cleanly.

Q88. What is the advantage of inline SVG over <img src="icon.svg">?

Inline SVG can be styled with CSS (fill, stroke) and manipulated or animated with JavaScript, while an <img>-referenced SVG is treated as an opaque image.

HTML Accessibility

Q89. What is the single biggest accessibility win in HTML?

Using the correct semantic element for the job. Native elements like <button>, <a>, and <nav> come with built-in keyboard support and screen reader behavior for free.

Q90. What is the "first rule of ARIA"?

No ARIA is better than bad ARIA — always prefer a native semantic element over adding ARIA attributes to a generic <div>. ARIA should fill gaps, not replace semantics you could get for free.

Q91. What does aria-label do?

IntermediateLearn topic →

It provides an accessible name for an element when there is no visible text to describe it, such as an icon-only button.

Q92. Why must interactive elements be usable with a keyboard alone?

IntermediateLearn topic →

Many users navigate exclusively via keyboard or other assistive technology, not a mouse. Every interactive element must be reachable with Tab and operable with Enter or Space.