HTML interview questions and answers: semantics, accessibility and forms
HTML interview questions test whether you write markup that browsers, screen readers and search engines understand: what the DOCTYPE does, which semantic element fits, how labels, buttons and ARIA make a page accessible, how forms validate and submit, what belongs in the head, and how script and image loading attributes affect speed. Fresher rounds stay with definitions; experienced rounds ask you to fix a broken form or an inaccessible component. Answer with the element first, then why it beats a div.
By Evin JohnPublished 8 min read
How HTML is tested in interviews
HTML rarely gets its own round, but it runs through every frontend, web developer and UI engineer interview. Freshers get quick definitions. Experienced candidates get a component to build or review: a login form, a modal, a navigation menu, a product card. The interviewer watches whether you reach for a <button> or a clickable <div>, whether every input has a label, and whether the page still works with a keyboard. Accessibility questions have become standard in frontend loops, so treat them as core, not bonus.
- Quick-fire basicsDOCTYPE, semantic tags, block versus inline, attributes, the head.
- Build or review a componentWrite markup for a form or card, or find the problems in someone else's markup.
- Accessibility and performance follow-upsScreen reader behavior, keyboard focus, loading attributes and metadata.
Basic HTML interview questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| What does <!DOCTYPE html> do? | Rendering modes | It tells the browser to use standards mode. Without it, browsers fall back to quirks mode, which emulates old layout bugs such as a different box model in some cases. |
| Why set lang on the html element? | Language metadata | Screen readers choose pronunciation from it, browsers use it for hyphenation and translation prompts, and search engines read it as a hint. |
| What is semantic HTML? | Meaning over looks | Elements that describe their content: header, nav, main, article, section, aside, footer, figure, time. They give assistive technology and crawlers structure that a pile of divs does not. |
| article versus section versus div? | Choosing elements | article is self-contained and could stand alone (a post, a comment). section is a thematic group, usually with a heading. div has no meaning and is for styling hooks only. |
| strong versus b, em versus i? | Semantics versus style | strong marks importance and em marks stress emphasis; b and i are stylistic offsets such as a keyword or a foreign term. Screen readers may treat them differently. |
| What are void elements? | Syntax | Elements with no content or closing tag, such as img, input, br, meta and link. The trailing slash is optional in HTML. |
| Block versus inline elements? | Default display | Block elements such as div and p start on a new line and take the full width; inline elements such as span and a flow within text. That is a default display value, which CSS can change. |
| id versus class? | Selectors and uniqueness | An id must be unique in the document and can be a fragment link target or a label's for value. Classes are reusable and are the normal styling hook. |
| What are data attributes, and how do you read them? | Custom data | data-* attributes store small pieces of string data on an element, such as data-product-id. JavaScript reads them through element.dataset.productId (camelCased); CSS can select on them. |
| Attribute or property? | DOM model | Attributes are in the markup; properties live on the DOM object. For an input, the value attribute is the initial value while the value property is what the user typed. |
| How do you add a comment and escape special characters? | Basics | <!-- comment -->, and entities such as < and & for characters that would otherwise be read as markup. |
Accessibility questions
- What should alt text say? What the image communicates in context. A product photo gets a description; a decorative flourish gets alt="" so screen readers skip it. Never leave alt off an img.
- Why is a div with an onclick handler not a button? It cannot receive keyboard focus, does not respond to Enter or Space, and is not announced as a button. A
<button>gives all of that for free. - When do you use a link and when a button? A link navigates to a URL; a button performs an action on the page. Styling one to look like the other is fine; swapping their roles is not.
- What is the first rule of ARIA? Do not use ARIA when a native element or attribute already does the job. ARIA changes what assistive technology announces; it adds no behavior.
- aria-label, aria-labelledby, aria-describedby: what is the difference? aria-label sets a name directly, aria-labelledby takes the name from other elements by id, and aria-describedby adds extra description, such as a hint or an error message.
- How do you announce a status update without moving focus? A live region: an element with aria-live="polite" (or role="status") whose text changes is read out when the screen reader is idle.
- What does tabindex do? 0 adds an element to the natural tab order, -1 makes it focusable from script but not by Tab, and positive values force an order that almost always breaks navigation.
- Why do headings need to be in order? Screen reader users jump between headings to scan a page. One h1 for the page topic and no skipped levels make the outline usable.
- What is a skip link? A link at the top of the page, usually visible on focus, that jumps to the main content so keyboard users do not tab through the whole navigation on every page.
- How would you build an accessible modal? The native dialog element opened with showModal() handles focus trapping, the Escape key and inert background in current browsers. Give it a label and return focus to the trigger when it closes.
Forms and validation
<form action="/signup" method="post" novalidate>
<fieldset>
<legend>Create your account</legend>
<label for="email">Work email</label>
<input id="email" name="email" type="email" autocomplete="email" required
aria-describedby="email-error">
<p id="email-error" class="error" hidden>Enter an email like name@company.com</p>
<label for="password">Password</label>
<input id="password" name="password" type="password"
autocomplete="new-password" minlength="12" required>
<label>
<input name="updates" type="checkbox" value="yes">
Send me product updates
</label>
</fieldset>
<button type="submit">Create account</button>
</form>| Question | What it tests | What a strong answer shows |
|---|---|---|
| Why does a field not appear in the submitted data? | Submission rules | It has no name attribute, it is disabled (disabled fields are not submitted, readonly ones are), or it sits outside the form without a form attribute. |
| What is the default type of a button inside a form? | Classic bug | submit. A "Show password" button without type="button" submits the form. Always set the type. |
| What does novalidate do? | Validation strategy | It turns off the browser's built-in validation UI so you can show your own messages, while keeping the constraint API (checkValidity, validity) available to script. |
| GET or POST? | HTTP semantics | GET puts fields in the URL, fine for searches and filters that should be bookmarkable. POST sends a body, used for changes and anything sensitive. |
| How do you upload a file? | Encoding | An input of type file inside a form with method post and enctype multipart/form-data, or a fetch request with FormData. |
| Why use specific input types? | Mobile and validation | email, tel, url, number and date bring the right mobile keyboard, built-in checks and better autofill. |
| Is client-side validation enough? | Security | No. It helps users; the server must validate again, because anyone can send requests without your form. |
| What does the autocomplete attribute do? | Autofill | Values such as email, current-password and one-time-code tell browsers and password managers what to fill, which also helps users with motor and memory difficulties. |
The head: metadata and SEO basics
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trail Running Shoes for Wet Terrain | Example Store</title>
<meta name="description" content="Grippy, waterproof trail shoes tested on mud and rock.">
<link rel="canonical" href="https://www.example.com/shoes/trail-wet">
<meta property="og:title" content="Trail Running Shoes for Wet Terrain">
<meta property="og:image" content="https://www.example.com/img/trail-wet.jpg">
<link rel="preconnect" href="https://cdn.example.com">
<link rel="stylesheet" href="https://www.example.com/css/site.css">
<script src="/js/app.js" defer></script>
</head>- Why must the charset meta come early? The browser needs the encoding before it reads text; it should sit within the first part of the document, ahead of the title.
- What does the viewport meta tag do? It tells mobile browsers to use the device width instead of a wide virtual canvas, which is what makes responsive CSS work.
- What is a canonical link? It names the preferred URL when the same content is reachable at several addresses (tracking parameters, sorting), so search engines consolidate signals there. It is a hint, not a command.
- Does the meta description affect ranking? It is not a ranking factor in itself, but search engines may show it as the snippet, so it affects whether people click.
- What are Open Graph tags for? The title, description and image that social platforms and chat apps show when someone shares the link.
- How do you add structured data? A script element of type application/ld+json containing schema.org data, such as Product or Article, that matches what the page shows.
- What does hreflang do? Alternate links with hreflang point search engines to translated versions of the same page, so each audience gets its language.
Loading, performance and storage
| Question | What it tests | What a strong answer shows |
|---|---|---|
| defer versus async on a script? | Parsing and order | Both download without blocking parsing. defer runs after parsing, in document order; async runs as soon as it arrives, in any order. Module scripts are deferred by default. |
| Why is a plain script in the head a problem? | Render blocking | Without defer or async, the parser stops until the script downloads and runs, delaying everything below it. |
| What does loading="lazy" do on an image? | Lazy loading | The browser delays fetching until the image is near the viewport. Do not lazy-load the main image at the top of the page, which is usually the largest paint. |
| Why set width and height on images? | Layout shift | The browser reserves space from the aspect ratio before the file arrives, so content below does not jump. |
| srcset, sizes and picture: when do you use each? | Responsive images | srcset with sizes lets the browser pick a resolution for the layout width. picture with source elements switches formats or art-directed crops. |
| preload versus preconnect? | Resource hints | preconnect opens a connection to an origin early; preload fetches a specific resource needed soon, such as a font or the main image. Overusing preload competes with more important requests. |
| Where can a page store data in the browser? | Storage options | data-* attributes for tiny per-element values, localStorage (persistent, synchronous, strings, per origin), sessionStorage (per tab), cookies (sent to the server with each request) and IndexedDB for larger structured data. |
| Is localStorage a good place for an auth token? | Security | Any script on the page, including injected ones, can read it. Many teams prefer an HttpOnly, Secure, SameSite cookie, which scripts cannot read, combined with CSRF protection. |
Security and advanced questions
- innerHTML or textContent? textContent inserts text safely. innerHTML parses markup, so user input placed there can run script; that is the root of many XSS bugs.
- What does the sandbox attribute on an iframe do? It removes scripts, forms, popups and same-origin access from embedded content unless you add back specific permissions with allow tokens.
- Why add rel="noopener" to links that open a new tab? It stops the new page from reaching back through window.opener. Modern browsers apply it by default for target="_blank", but adding it is harmless.
- What is the template element? Markup that the browser parses but does not render or run, used as a stamp for content that script clones later.
- canvas or SVG? SVG is retained vector markup in the DOM, accessible and styleable, good for icons and charts with fewer elements. canvas is a bitmap you draw with script, better for many moving objects.
- What are custom elements? Your own tags defined with JavaScript classes, optionally with shadow DOM for encapsulated markup and styles.
Fictional example
- Interviewer
- Here is our product card: a div with an onclick that opens the product, an img with no alt, the price in a span, and a heart icon div to save the item. What would you change?
- Candidate
- I would make the card an article, since each product stands on its own, with the product name as a heading at the right level for the listing page. Opening the product is navigation, so the name should be a real link to the product URL instead of a click handler on the div; if the whole card needs to be clickable, I would stretch that link's hit area with CSS rather than nesting interactive elements. The image needs alt text describing the product, or alt="" if the name right next to it already says the same. Saving is an action, so the heart becomes a button with type="button", an accessible name such as "Save Trail Runner 2 to wishlist" and aria-pressed to expose the state. I would add width and height to the image and loading="lazy" for cards below the fold. Then I would test it with the keyboard and a screen reader before calling it done.
One-week HTML practice plan
- Days 1 and 2: rebuild two real pages you use often with semantic elements only, no CSS, and check that the outline still makes sense.
- Day 3: build a sign-up form with labels, input types, constraints and custom error messages tied with aria-describedby. Submit it and inspect what the server receives.
- Day 4: navigate your pages with only the keyboard and then with a screen reader (VoiceOver on Mac, Narrator or NVDA on Windows). Fix every place you got stuck.
- Day 5: write a complete head for a product page, including canonical, Open Graph and JSON-LD, and run a performance audit to see the effect of defer, lazy images and dimensions.
- Days 6 and 7: practice reviewing markup out loud: take any component library example and list its problems in two minutes.
Styling questions, from the box model to container queries, are on the CSS interview questions page. For the full frontend loop, see frontend developer interview questions; if your role also builds APIs for the page, GraphQL interview questions covers the data side.
Markup reviews are a talk-while-you-read exercise. Natively, an AI interview assistant for Mac and Windows, keeps a live transcript of your mock frontend round on your computer, so afterwards you can see which problems you spotted and which you skipped. While practicing it can read a markup snippet or a design from a screenshot and suggest what to check, and with Pro or the free trial it can use the résumé and job description you add. Notes and transcript wait for you after the session.
Questions people ask
What HTML questions are asked in interviews?
Expect the DOCTYPE, semantic elements, block versus inline, data attributes, accessibility (alt text, labels, buttons versus links, ARIA, focus), forms and validation, head metadata, and script and image loading attributes such as defer, async and loading="lazy".
What is semantic HTML and why does it matter?
Using elements that describe their content, such as nav, main, article, button and label, instead of generic divs. It gives screen readers, keyboards and search engines structure and behavior that would otherwise need extra code.
What is the difference between defer and async in HTML?
Both download scripts without blocking the parser. defer runs scripts after the document is parsed, in order; async runs each script as soon as it downloads, in no guaranteed order.
What HTML questions are asked for freshers?
Mostly definitions: what the DOCTYPE does, semantic tags, block and inline elements, id versus class, form input types, the purpose of alt text and the meta viewport tag. Expect to write a small form or page structure.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
