Home / Learn HTML / HTML Interview Questions and Answers
HTML Interview Questions and Answers
32 common HTML interview questions with short, clear answers. Basic, intermediate and advanced.
Short answers you can say in an interview. Click a question to open the answer. Practice saying each answer in your own words.
Basic questions
What is HTML?
HTML (HyperText Markup Language) is the language that gives a web page its structure. It uses elements written with tags to mark headings, paragraphs, links, images, lists and forms. Browsers read the HTML and show the page.
What is the difference between an element, a tag and an attribute?
A tag is the text in angle brackets, such as <p>. An element is the start tag, the content and the end tag together. An attribute is extra information inside the start tag, such as href in <a href="...">.
What does the doctype do?
<!doctype html> tells the browser to use standards mode and the current HTML version. Without it, browsers can switch to quirks mode and show the page differently.
What is the difference between <div> and <span>?
div is a block element: it starts on a new line and takes the full width. span is inline: it stays inside a line of text. Both have no meaning of their own and are used to group content for CSS or JavaScript.
What are void (empty) elements?
Void elements have no content and no end tag. Examples are img, br, hr, input, meta and link.
What is the difference between id and class?
An id must be unique on a page and names one element. A class can be used on many elements and one element can have several classes. CSS and JavaScript use both to find elements.
What is the difference between <ol> and <ul>?
ol is an ordered (numbered) list and ul is an unordered (bullet) list. Both contain li items. dl is a third kind, for terms and descriptions.
How do you open a link in a new tab, and what should you add?
Use target="_blank". Also add rel="noopener" so the new page cannot control the old one through window.opener (modern browsers do this by default for _blank, but writing it is still good practice).
What is the alt attribute for?
alt gives a text description of an image. Screen readers read it, it shows when the image fails to load, and search engines use it. Decorative images should use an empty alt="".
Why do we need <meta name="viewport">?
It tells mobile browsers to use the real device width instead of a wide desktop layout. Without it, responsive CSS does not work properly on phones.
Intermediate questions
What are semantic elements? Give examples.
Semantic elements describe their meaning: header, nav, main, article, section, aside and footer. They help screen readers, search engines and other developers understand the page.
What is the difference between <section> and <article>?
article is a self-contained piece that makes sense alone, such as a blog post or a news item. section is a themed group of content, usually with its own heading.
What is the difference between GET and POST in a form?
GET puts the form data in the URL, so it can be bookmarked and is limited in size. Use it for searches. POST sends the data in the request body, so use it for passwords, uploads and anything that changes data.
How does the label element help users?
A label tells what an input is for. When it is connected with for and id, clicking the label focuses the input, and screen readers announce the label.
What is the difference between <b> and <strong>?
b only makes text bold. strong means the text is important, and browsers usually show it bold. Screen readers can give strong more emphasis. The same applies to i and em.
What is the use of the <iframe> element and what are its risks?
iframe shows another page inside your page. Risks are slower loading and security issues, so use the sandbox attribute, give it a title and only embed sources you trust.
How do you make a page responsive using only HTML and CSS?
Add the viewport meta tag, use flexible units and max-width:100% for images, use CSS Grid or Flexbox for layout, and add media queries for different screen sizes.
What is the difference between localStorage and sessionStorage?
Both store data in the browser. localStorage stays until it is cleared. sessionStorage is erased when the tab is closed. Cookies are different: they are sent to the server with each request.
What are data-* attributes?
Custom attributes that start with data-, such as data-id="5". They store extra information on an element and JavaScript can read them through the dataset property.
Why should headings be in order?
Headings give the outline of the page. Screen reader users jump between headings, and search engines use them to understand structure. Use one h1 and do not skip levels, for example from h2 to h4.
What does the defer attribute do on a script?
defer downloads the script while the HTML is parsing and runs it after the page is parsed, in order. async runs the script as soon as it is downloaded, in any order. Both avoid blocking the page.
What is the difference between block, inline and inline-block?
Block elements start on a new line and take the full width. Inline elements flow inside text and ignore width and height. inline-block flows like inline but accepts width and height.
Advanced questions
What is the difference between HTML and HTML5?
HTML5 is the current version of the standard. It added semantic elements, audio and video, canvas, SVG, local storage, new input types, form validation and many APIs, without plugins like Flash. Today it is simply called the HTML Living Standard.
What is the critical rendering path?
It is the steps a browser takes to show a page: it parses HTML into the DOM, parses CSS into the CSSOM, combines them into a render tree, lays out the boxes and paints the pixels. Blocking CSS and scripts delay it.
How do you improve page loading with HTML?
Use loading="lazy" on images below the fold, set width and height on images to avoid layout shift, use preload for key resources, defer scripts, and serve modern image formats with picture and srcset.
What is srcset and the picture element?
srcset lets the browser pick the best image size for the screen. The picture element lets you give different sources by media query or format, such as WebP with a JPEG fallback.
What are ARIA attributes and when should you use them?
ARIA (role, aria-label, aria-expanded and so on) adds accessibility information when HTML alone cannot. The first rule of ARIA: use the correct native element first, such as button, and add ARIA only when needed.
What is the shadow DOM and what are web components?
Web components let you create your own reusable tags. Custom elements define the tag, the shadow DOM hides the internal markup and styles from the rest of the page, and templates hold reusable markup.
How does a browser handle invalid or unclosed HTML?
Browsers do not stop with an error. They follow the HTML parsing rules to guess the intent and repair the markup, so the same bad HTML can produce odd results. Use a validator to catch such errors.
What is the difference between the DOM and the HTML source?
The source is the text file. The DOM is the live tree of objects the browser builds from it. JavaScript changes the DOM, so what you see in the inspector can differ from View Source.
How do you make HTML good for SEO?
Use a unique title and meta description, one h1, semantic structure, descriptive link text, alt text, canonical links, structured data (JSON-LD) and fast, mobile friendly pages.
What is the Content Security Policy (CSP)?
CSP is a header or meta tag that limits where scripts, styles and other resources can load from. It reduces the damage of cross-site scripting (XSS) attacks.