HTML interview questions: answers with real browser output
The markup questions front-end developers get, answered in plain words, with examples run in a real browser and checked against the HTML standard.
HTML interviews cover the doctype and standards mode, semantic elements and accessibility, data attributes, boolean attributes, form controls, labels and built-in validation, script loading with defer and async, how the parser repairs invalid markup, textContent versus innerHTML, dialog, iframe sandboxing, images and safe links. Below are 14 questions; 11 examples were loaded in headless Google Chrome and the output is what each page printed.
Each snippet is a small page run in Chrome
Each example is a complete HTML snippet. The build loads it in headless Google Chrome at an 800 × 600 viewport with web requests blocked, and prints whatever the snippet's script logs with console.log.
Basic HTML interview questions
Basic1. What does <!DOCTYPE html> do?
It tells the browser to render the page in no-quirks (standards) mode. Without a doctype, browsers fall back to quirks mode, which emulates old browser bugs in layout. The example parses the same markup with and without a doctype and asks each resulting document which mode it is in: CSS1Compat means standards mode, BackCompat means quirks mode.
HTML<!DOCTYPE html>
<script>
const parse = html => new DOMParser().parseFromString(html, 'text/html');
console.log('this page:', document.compatMode);
console.log('parsed with a doctype:', parse('<!DOCTYPE html><p>hi').compatMode);
console.log('parsed without a doctype:', parse('<p>hi').compatMode);
</script>Output (Google Chrome 157.0.8103.40 headless)this page: CSS1Compat parsed with a doctype: CSS1Compat parsed without a doctype: BackCompat
Checked against: WHATWG HTML Standard: parsing, WHATWG HTML Standard: the HTML syntax
Basic2. Why use semantic elements such as header, nav, main and article instead of div?
Semantic elements say what a part of the page is, not just how it looks. Browsers, search engines and assistive technology use that meaning: screen reader users can jump between landmarks such as the navigation and the main content, and an article can be understood on its own. A div carries no meaning at all, so it is the right choice only when no element fits. Use one main per page for the dominant content.
Checked against: WHATWG HTML Standard: grouping content, WHATWG HTML Standard: sections
Basic3. What are data-* attributes, and how do you read them in JavaScript?
Custom data attributes store private data for the page's own scripts on any element, without inventing non-standard attributes. In JavaScript the dataset property exposes them, converting dashed names to camelCase. Values are always strings, so convert them yourself.
HTML<button id="apply" data-job-id="42" data-remote="true">Apply</button>
<script>
const b = document.getElementById('apply');
console.log(b.dataset.jobId, typeof b.dataset.jobId, b.dataset.remote);
b.dataset.status = 'saved';
console.log(b.outerHTML);
</script>Output (Google Chrome 157.0.8103.40 headless)42 string true <button id="apply" data-job-id="42" data-remote="true" data-status="saved">Apply</button>
Checked against: WHATWG HTML Standard: semantics, structure and APIs
Basic4. Why is <input disabled="false"> still disabled?
disabled is a boolean attribute: its presence means true, whatever its value, and the only way to make it false is to remove the attribute. In JavaScript, set the property (input.disabled = false) or call removeAttribute. The same applies to required, checked and readonly. hidden is different in the current standard: it is an enumerated attribute, and hidden="until-found" hides content while keeping it findable with find-in-page.
HTML<input id="a" disabled="false">
<input id="b">
<script>
const a = document.getElementById('a'), b = document.getElementById('b');
console.log('a disabled:', a.disabled, '| b disabled:', b.disabled);
a.removeAttribute('disabled');
console.log('a after removeAttribute:', a.disabled);
</script>Output (Google Chrome 157.0.8103.40 headless)a disabled: true | b disabled: false a after removeAttribute: false
Checked against: WHATWG HTML Standard: common microsyntaxes, WHATWG HTML Standard: user interaction (the hidden attribute)
Basic5. What type is a <button> inside a form if you do not set one?
submit. A button without a type attribute (and without the newer command and commandfor attributes) submits its form, which is a common bug when a button meant for JavaScript (Clear, Show more) reloads the page. Give such buttons type="button". In the example only the first button submits the form.
HTML<form id="f"><input name="q" value="nurse"><button>Search</button><button type="button">Clear</button></form>
<script>
const f = document.getElementById('f');
f.addEventListener('submit', e => { e.preventDefault(); console.log(' form submitted by:', e.submitter.textContent); });
for (const btn of f.querySelectorAll('button')) {
console.log(btn.textContent, 'type =', btn.type);
btn.click();
}
</script>Output (Google Chrome 157.0.8103.40 headless)Search type = submit form submitted by: Search Clear type = button
Checked against: WHATWG HTML Standard: form elements
Intermediate HTML interview questions
Intermediate6. How do you connect a label to a form field, and why does it matter?
Either give the label a for attribute matching the field's id, or put the field inside the label. The field then gets the label's text as its accessible name, and clicking the label activates the field, which also gives users a bigger target. A placeholder is not a substitute for a label.
HTML<label for="remote">Remote only</label> <input type="checkbox" id="remote">
<label>Email <input id="email" type="email"></label>
<script>
document.querySelector('label[for=remote]').click();
console.log('checkbox checked after clicking its label:', document.getElementById('remote').checked);
console.log('labels attached to the email field:', document.getElementById('email').labels.length);
</script>Output (Google Chrome 157.0.8103.40 headless)checkbox checked after clicking its label: true labels attached to the email field: 1
Checked against: WHATWG HTML Standard: forms
Intermediate7. How does built-in form validation work?
Attributes such as required, type="email", pattern, min and max add constraints, and the browser checks them before submitting. Scripts can check too: checkValidity() returns true or false, and the validity object says which constraint failed. Client-side validation is for the user's convenience only; the server must validate again.
HTML<form id="f" novalidate>
<input id="email" type="email" required>
<input id="zip" pattern="[0-9]{5}" value="1234">
</form>
<script>
const email = document.getElementById('email'), zip = document.getElementById('zip');
console.log('empty email valid:', email.checkValidity(), '| valueMissing:', email.validity.valueMissing);
email.value = 'not-an-email';
console.log('typeMismatch:', email.validity.typeMismatch, '| zip patternMismatch:', zip.validity.patternMismatch);
email.value = 'ava@example.com'; zip.value = '30301';
console.log('form valid now:', document.getElementById('f').checkValidity());
</script>Output (Google Chrome 157.0.8103.40 headless)empty email valid: false | valueMissing: true typeMismatch: true | zip patternMismatch: true form valid now: true
Checked against: WHATWG HTML Standard: form control infrastructure
Intermediate8. What is the difference between a normal script, defer and async?
A normal script blocks HTML parsing while it downloads and runs. A defer script downloads in parallel and runs after parsing finishes, in document order, before DOMContentLoaded. An async script also downloads in parallel but runs as soon as it is ready, in no fixed order, so it suits independent scripts such as analytics. The example uses a defer script and shows the order.
HTML<script>console.log('1 inline script while parsing');</script>
<script defer src="data:text/javascript,console.log('3 defer script, after parsing')"></script>
<script>document.addEventListener('DOMContentLoaded', () => console.log('4 DOMContentLoaded'));</script>
<p id="p">paragraph</p>
<script>console.log('2 inline script after the paragraph, paragraph exists:', !!document.getElementById('p'));</script>Output (Google Chrome 157.0.8103.40 headless)1 inline script while parsing 2 inline script after the paragraph, paragraph exists: true 3 defer script, after parsing 4 DOMContentLoaded
Checked against: WHATWG HTML Standard: scripting
Intermediate9. What happens to invalid nesting such as a div inside a p?
The HTML parser never gives up: it repairs the markup by fixed rules. A p cannot contain a div, so when the parser meets the div it closes the open p first, and the text after the div ends up outside any paragraph. The rendered DOM can therefore differ from the source, which is worth checking in developer tools when styles do not apply.
HTML<div id="box"><p>one<div>two</div>three</div>
<script>
console.log(document.getElementById('box').innerHTML);
</script>Output (Google Chrome 157.0.8103.40 headless)<p>one</p><div>two</div>three
Checked against: WHATWG HTML Standard: the HTML syntax
Intermediate10. What is the difference between textContent and innerHTML, and why does it matter for security?
textContent treats a string as text, so tags in it are shown, not run. innerHTML parses the string as HTML and creates elements, so untrusted input can inject markup and script (cross-site scripting). Use textContent for user data, and only use innerHTML with trusted or sanitized HTML.
HTML<div id="safe"></div><div id="unsafe"></div>
<script>
const input = '<b>Ava</b> <i>Lee</i>';
document.getElementById('safe').textContent = input;
document.getElementById('unsafe').innerHTML = input;
console.log('textContent: elements created =', document.getElementById('safe').children.length, '| markup kept as text:', document.getElementById('safe').innerHTML);
console.log('innerHTML: elements created =', document.getElementById('unsafe').children.length);
</script>Output (Google Chrome 157.0.8103.40 headless)textContent: elements created = 0 | markup kept as text: <b>Ava</b> <i>Lee</i> innerHTML: elements created = 2
Checked against: WHATWG HTML Standard: dynamic markup insertion, MDN Web Docs: Element.innerHTML, MDN Web Docs: Node.textContent
Advanced HTML interview questions
Advanced11. How does the dialog element work?
showModal() opens a dialog as modal: the rest of the page becomes inert, Escape closes it, and focus moves into it. show() opens it without blocking the page. A form with method="dialog" inside it closes the dialog on submit and sets returnValue to the value of the button used.
HTML<dialog id="d"><form method="dialog"><p>Delete this job?</p><button value="yes">Yes</button> <button value="no">No</button></form></dialog>
<script>
const d = document.getElementById('d');
d.addEventListener('close', () => console.log('closed, returnValue =', d.returnValue));
d.showModal();
console.log('open:', d.open, '| matches :modal:', d.matches(':modal'));
d.querySelector('button[value=yes]').click();
</script>Output (Google Chrome 157.0.8103.40 headless)open: true | matches :modal: true closed, returnValue = yes
Checked against: WHATWG HTML Standard: interactive elements
Advanced12. What does the sandbox attribute on an iframe do?
An empty sandbox attribute applies every restriction to the framed content: no scripts, no form submission, no popups, and a unique origin. Each allow-* keyword lifts one restriction, such as allow-scripts. Avoid combining allow-scripts and allow-same-origin for content from your own origin, because the framed page could then remove its own sandbox. In the example both frames try to send a message to the page; only the frame with allow-scripts can run its script.
HTML<iframe sandbox srcdoc="<script>parent.postMessage('the fully sandboxed frame ran a script', '*')</script>"></iframe>
<iframe sandbox="allow-scripts" srcdoc="<script>parent.postMessage('the frame with allow-scripts ran a script', '*')</script>"></iframe>
<script>
const got = [];
window.addEventListener('message', e => got.push(e.data));
window.addEventListener('load', () => setTimeout(() => console.log('messages received:', JSON.stringify(got)), 300));
</script>Output (Google Chrome 157.0.8103.40 headless)messages received: ["the frame with allow-scripts ran a script"]
Checked against: WHATWG HTML Standard: iframe and embedded content
Advanced13. How should images be written for accessibility and performance?
Give every img an alt attribute: a short text equivalent for informative images, and an empty alt="" for purely decorative ones so screen readers skip them. For responsive images, srcset lists candidate files and sizes tells the browser the display width, so it can download the smallest suitable file. loading="lazy" defers off-screen images; set width and height so the layout does not jump while images load.
Checked against: WHATWG HTML Standard: embedded content, WHATWG HTML Standard: images
Advanced14. Why add rel="noopener" to links that open in a new tab?
A page opened with target="_blank" could historically reach back to the opener through window.opener and, for example, redirect it to a phishing page. noopener prevents that (and noreferrer also hides the referrer). The current HTML standard applies noopener to target="_blank" links by default unless rel="opener" is set, and current browsers follow it; writing rel="noopener" explicitly is still common and protects older browsers.
Checked against: WHATWG HTML Standard: links
How to practice with this page
- Predict each output before reading it.
- Open the snippets in your browser and inspect the DOM the parser actually built.
- Run an accessibility checker on a page you built and fix what it reports.
- Practice explaining why a semantic element beats a div in one or two sentences.
Related interview prep and tools
Layout and animation, run in a real browser.
Open →QuestionsJavaScript interview questionsThe language of the browser, with real output.
Open →QuestionsDjango interview questionsServer-side templates, forms and security.
Open →RoleWeb developer interview questionsQuestions for web developer roles, beyond markup.
Open →Free toolResume summary generatorDraft a summary for a Web Developer role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a Web Developer search.
Open →Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.
Questions people ask
What HTML topics come up often in front-end interviews?
Were these examples run in a real browser?
Should I learn HTML before CSS and JavaScript?
How this page is built
TryHired wrote these questions and answers. Every snippet on this page was loaded as a page in Google Chrome 157.0.8103.40 headless when the page was built (2026-11-16), at an 800 × 600 viewport with all web requests blocked; the output is exactly what the snippet's script printed with console.log (the browser's own warnings are not included). Facts about HTML were checked against the WHATWG HTML Living Standard (and MDN Web Docs for Q10). The build stops if any example fails. The question list is general practice: it does not predict what a particular employer will ask.
- Interviewers ask different questions for different roles and levels.
- Answers are short explanations, not complete references.
- Output comes from one browser, Google Chrome. The answers follow the WHATWG HTML Living Standard; older browsers can differ, for example on noopener for target="_blank".
- WHATWG HTML Standard: form elements (read 2026-11-15)
- WHATWG HTML Standard: common microsyntaxes (read 2026-11-15)
- WHATWG HTML Standard: semantics, structure and APIs (read 2026-11-15)
- WHATWG HTML Standard: dynamic markup insertion (read 2026-11-15)
- WHATWG HTML Standard: embedded content (read 2026-11-15)
- WHATWG HTML Standard: form control infrastructure (read 2026-11-15)
- WHATWG HTML Standard: forms (read 2026-11-15)
- WHATWG HTML Standard: grouping content (read 2026-11-15)
- WHATWG HTML Standard: iframe and embedded content (read 2026-11-15)
- WHATWG HTML Standard: images (read 2026-11-15)
- WHATWG HTML Standard: user interaction (the hidden attribute) (read 2026-11-15)
- WHATWG HTML Standard: interactive elements (read 2026-11-15)
- WHATWG HTML Standard: links (read 2026-11-15)
- WHATWG HTML Standard: parsing (read 2026-11-15)
- WHATWG HTML Standard: scripting (read 2026-11-15)
- WHATWG HTML Standard: sections (read 2026-11-15)
- WHATWG HTML Standard: the HTML syntax (read 2026-11-15)
- MDN Web Docs: Element.innerHTML (read 2026-11-15)
- MDN Web Docs: Node.textContent (read 2026-11-15)