HTML
HTML Interview Questions and Answers
1. What is HTML?
A: HTML (HyperText Markup Language) is the standard markup language for creating webpages.
2. What is the latest version of HTML?
A: HTML5.
3. What does HTML stand for?
A: HyperText Markup Language.
4. What are HTML tags?
A: Tags are keywords enclosed in angle brackets (e.g. <p>
) that define elements in HTML.
5. What is an HTML attribute?
A: Attributes provide additional information about elements. Example:
6. What is the difference between HTML elements and tags?
- Tag: The markup like
<p>
. - Element: Everything from the start tag to end tag including content:
<p>
Hello</p>
.
7. What are void (self-closing) elements in HTML?
A: Elements that don’t need a closing tag.
Examples: <img>
, <br>
, <hr>
, <input>
.
8. What is the use of <!DOCTYPE html>
?
A: Declares the document type and version of HTML to the browser.
9. What is semantic HTML?
A: Using tags that convey meaning (like <article>
, <header>
) instead of just presentation.
10. What is the purpose of comments in HTML?
A: To leave notes for developers.
11. What tags define the basic structure of an HTML document?
12. What is the <head>
tag used for?
A: Metadata, title, links to CSS, scripts.
13. What goes inside the <body>
tag?
A: The content that displays on the webpage.
14. What is the <title>
tag used for?
A: Sets the title shown in the browser tab.
15. What is the difference between <head>
and <body>
?
<head>
: metadata, not directly visible.<body>
: content shown to users.
16. How do you create headings in HTML?
17. How do you create a paragraph?
18. Difference between <strong>
and <b>
?
<strong>
: semantic, emphasizes importance.<b>
: purely bold visual style.
19. Difference between <em>
and <i>
?
<em>
: emphasizes text (semantic).<i>
: italicizes visually.
20. How to insert a line break?
21. How to create horizontal lines?
22. How to make text subscript or superscript?
23. How do you create a blockquote?
24. What is the <pre>
tag?
A: Preserves whitespace and line breaks.
25. How do you display code snippets?
26. How to create a hyperlink?
27. What is the target attribute?
A: Defines where to open the link.
28. Difference between absolute and relative URLs?
- Absolute: full path (https://site.com/page).
- Relative: from current location (/page).
29. How to create an email link?
30. How to make a phone call link?
31. Types of lists in HTML?
- Ordered (
<ol>
) - Unordered (
<ul>
) - Definition (
<dl>
)
32. How to create an ordered list?
33. How to create an unordered list?
34. How to create a definition list?
35. Can lists be nested?
A: Yes.
36. How to insert an image?
37. What is the alt attribute?
A: Text shown if image can’t load (important for accessibility).
38. How to add a clickable image?
39. How to embed a video?
40. How to embed audio?
41. How to display SVG?
or embed SVG code directly.
42. What is <canvas>
used for?
A: Drawing graphics via JavaScript.
43. How do you create a table?
44. What are <thead>
, <tbody>
, <tfoot>
?
A: Structure tables into
<thead>
– header,<tbody>
– body,<tfoot>
- footer sections.
45. What is <caption>
?
A: Title of the table.
46. How to merge cells?
47. Difference between <th>
and <td>
?
<th>
: header cell (bold, centered by default)<td>
: regular data cell.
48. How to create a form?
49. Common form elements?
<input>
, <textarea>
, <select>
, <button>
.
50. What is action?
A: URL where form data is sent.
51. What is method?
- GET: appends data to URL.
- POST: sends data in body.
52. How to create a text input?
53. How to create radio buttons?
54. How to create checkboxes?
55. How to create dropdown?
56. How to create multi-select?
57. How to create a submit button?
58. What is a hidden input?
59. What is <label>
for?
A: Associates text with a form control.
60. How to group with <fieldset>
and <legend>
?
61. New HTML5 input types?
email
,tel
,url
,number
,date
,range
,color
.
62. How to create a date picker?
63. How to create a color picker?
64. How to validate an email?
65. How to validate with pattern?
66. What is <meta>
?
A: Defines metadata like charset, viewport, description.
67. How to set charset?
68. How to set viewport for mobile?
69. How to set description?
70. How to redirect with meta?
71. Examples of semantic tags?
<article>
, <section>
, <aside>
, <nav>
, <header>
, <footer>
.
72. What is <section>
for?
A: Defines a thematic grouping of content.
73. What is <article>
for?
A: Independent content, like a blog post.
74. What is <aside>
for?
A: Sidebar or additional info.
75. What is <nav>
for?
A: Navigation links.
76. What is <header>
and <footer>
?
A: Top and bottom sections of a page or section.
77. How to improve accessibility?
- Use alt text for images.
- Use ARIA roles.
- Use semantic tags.
78. How to embed YouTube?
79. What is <iframe>
?
A: Embeds another webpage.
80. How to embed PDF?
81. How to include JavaScript?
82. How to include CSS?
83. <script>
in head vs body?
- Head: blocks parsing.
- Body (bottom): loads after content.
84. What does defer do?
A: Runs script after HTML is parsed.
85. What does async do?
A: Loads script asynchronously.
86. What is class?
A: Groups elements for CSS/JS.
87. What is id?
A: Uniquely identifies an element.
88. What is style?
A: Inline CSS.
89. What is title?
A: Tooltip text on hover.
90. What is data-*
?
A: Custom data attributes.
91. What is local storage?
A: Stores data with no expiration.
92. What is session storage?
A: Stores data for one session.
93. Difference localStorage vs sessionStorage?
localStorage
: persists after closing browser.sessionStorage
: cleared on tab close.
94. What is Geolocation API?
A: Gets user’s location via JavaScript.
95. What are Web Workers?
A: Run scripts in background threads.
96. What is Drag and Drop API?
A: Native support for draggable interfaces.
97. What is the History API?
A: Manipulate browser session history.
98. What are WebSockets?
A: Bi-directional communication.
99. How to defer image loading?
100. How to optimize for SEO?
- Use semantic tags.
- Use alt text.
- Proper heading structure.
- Meta description.
HTML Elements & Attributes (Extended)
101. Can you nest a <p>
tag inside another <p>
?
A: No, <p>
cannot contain other block elements including another <p>
.
102. What does the lang attribute on <html>
do?
A: Specifies the language of the page (useful for screen readers & SEO).
103. How can you make text appear preformatted (preserve spaces)?
A: Using <pre>
tag.
104. What does the dir attribute do?
A: Sets text direction (ltr or rtl).
105. What does translate="no"
do?
A: Hints to translation tools not to translate this element.
106. What is the contenteditable attribute?
A: Makes an element’s content editable in browser.
107. What is the spellcheck attribute?
A: Enables or disables spellchecking.
108. What is the hidden attribute?
A: Hides the element from rendering.
109. What does the <mark>
tag do?
A: Highlights text.
110. What is the <small>
tag used for?
A: Renders smaller text, often for disclaimers.
111. What is the <abbr>
tag?
A: Indicates abbreviation or acronym.
112. What does <time>
represent?
A: Defines a date/time.
113. What is <cite>
used for?
A: Reference to a creative work.
114. What does <address>
do?
A: For contact information.
115. What does <dfn>
represent?
A: The defining instance of a term.
116. What does <kbd>
mean?
A: Indicates user input (like keyboard input).
117. What does <samp>
mean?
A: Sample output from a computer program.
118. What does <var>
tag do?
A: Represents a variable.
119. What is <bdi>
?
A: Isolates part of text to override its bidirectional directionality.
120. What is <bdo>
?
A: Overrides text direction.
121. What is the tabindex attribute?
A: Controls tab order of elements.
122. What does accesskey do?
A: Defines keyboard shortcut to activate/focus an element.
123. What does draggable do?
A: Makes elements draggable.
124. What is the aria- family of attributes for?
A: ARIA (Accessible Rich Internet Applications) improves accessibility for assistive technologies.
125. What does role attribute do?
A: Defines the role of an element (like role="button").
126. How do you create a progress bar in HTML?
127. How to use <meter>
?
128. What is the <details>
tag?
A: Creates a disclosure widget.
129. What is <summary>
used for?
A: Summary of <details>
, always visible.
130. What is <dialog>
used for?
A: Native modal dialogs.
131. What is autocomplete on <form>
or <input>
?
A: Tells browser if it should autofill fields.
132. What is the novalidate attribute?
A: Skips form validation on submit.
133. What is the autofocus attribute?
A: Puts focus on input when page loads.
134. What is readonly?
A: Makes input not editable but submits value.
135. What is disabled?
A: Makes input uneditable and excluded from submission.
136. What does required do?
A: Marks input mandatory.
137. What is min and max?
A: For number, date inputs to restrict values.
138. What does step do?
A: Specifies increment steps.
139. What is placeholder?
A: Gives a hint inside input box.
140. What is multiple for <input type="file">
?
A: Allows multiple file selection.
141. How do you disable resizing of <textarea>
?
142. How to make <input>
accept only images?
143. How to specify maximum length?
144. What is formnovalidate on a submit button?
A: Bypasses validation when clicked.
145. What is formenctype?
A: Specifies encoding type for form data (like multipart/form-data).
146. What is formmethod?
A: Overrides <form>
’s method.
147. What is formaction?
A: Overrides <form>
’s action for that button.
148. What is list attribute for <input>
?
A: Links to <datalist>
for autocomplete options.
149. How to make a search field?
150. How to make a password field?
151. What is the <colgroup>
tag?
A: Groups columns for styling.
152. What is the <col>
tag?
A: Defines column properties inside <colgroup>
.
153. How to add scope to <th>
?
154. What does headers attribute do in <td>
?
A: Associates data cell with header cells.
155. How to add summary to a table for accessibility?
A: Using aria-describedby or explaining with surrounding text. No native summary attribute in HTML5.
156. What does <source>
tag do?
A: Provides multiple media sources.
157. What does <track>
do?
A: Adds subtitles/captions.
158. What is poster on <video>
?
A: Specifies an image to show before the video plays.
159. How to loop audio/video?
160. How to autoplay audio/video?
(Note: muted required for autoplay to work in most browsers.)
161. What is <object>
tag?
A: Embeds external resources like PDF or Flash (older practice).
162. What is <embed>
tag?
A: Embeds external content (like plugin content).
163. Difference between <embed>
and <object>
?
<embed>
is self-contained.<object>
can have fallback content.
164. How to embed Google Maps?
A: Using an <iframe>
with embed code from Google.
165. What is aria-label?
A: Gives an accessible label to screen readers.
166. What is aria-hidden?
A: Hides element from assistive tech.
167. What is aria-live?
A: Tells screen readers to announce changes.
168. How to label an icon-only button?
169. What is aria-expanded?
A: Indicates expandable state (like dropdowns).
170. What is aria-controls?
A: Identifies the element a widget controls.
171. What is defer vs async script loading difference?
defer
: waits for HTML parsing, preserves order.async
: loads immediately, order not guaranteed.
172. What does rel="noopener noreferrer"
on links do?
A: Improves security when opening links in new tabs.
173. What is preload in <link>
?
A: Instructs browser to load resource early.
174. What is prefetch?
A: Loads resources for likely future navigation.
175. What is dns-prefetch?
A: Resolves domain names early.
176. How to make a favicon?
177. How to add an app manifest?
178. What does canonical tag do?
A: Indicates preferred URL to search engines.
179. How to link an alternate language page?
180. How to preload fonts?
181. What is hreflang used for?
A: Indicates language & region of page content for search engines.
182. What does nofollow do in a link?
A: Tells search engines not to follow the link.
183. How to prevent indexing of a page?
184. What is robots.txt?
A: A file that tells search bots which pages to crawl or avoid.
185. What is the viewport meta tag for?
A: Controls how page scales on different devices.
186. Why is semantic HTML important for SEO?
A: Helps search engines understand page structure.
187. What is lazy loading?
A: Deferring loading images/videos until needed.
188. How to embed CSS inside HTML?
189. How to use inline CSS?
190. How to import external CSS?
191. Can <style>
be in <body>
?
A: Valid but discouraged; best placed in <head>
.
192. What is scoped attribute on <style>
?
A: Was proposed to scope styles to its parent element but not widely supported.
193. How to run inline JavaScript?
194. How to call a function on button click?
195. How to load JS from a file?
196. Should scripts be at bottom or top?
A: Bottom of <body>
for faster page load (unless using defer or async).
197. How to prevent script execution if JS disabled?
198. What is dir="auto"
?
A: Lets browser guess direction (ltr or rtl).
199. How to provide alternative language versions?
200. What does aria-labelledby do?
A: Points to element whose text labels this one.
201. How to make sure form controls are accessible?
A: Always use <label for="id">
.
202. What does aria-describedby do?
A: Points to element providing description.
203. How to indicate toggle state?
204. What does <link rel="manifest">
do?
A: Links to web app manifest for PWA.
205. What is theme-color meta tag?
Controls browser UI color.
206. How to specify icons for devices?
207. What is a service worker?
A: A script that runs in background for caching/offline.
208. What is the File API?
A: Reads file data from <input type="file">.
209. What is Web Storage API?
A: localStorage and sessionStorage for client-side storage.
210. What is the Web Notifications API?
A: Shows system notifications from the browser.
211. What is the Clipboard API?
A: Lets you read/write clipboard text.
212. What is the Fullscreen API?
A: Makes elements take up entire screen.
213. What is WebRTC?
A: Enables real-time peer-to-peer communication.
214. What is user-scalable=no
in viewport?
Prevents zoom.
215. What is viewport-fit=cover
?
A: Helps display on devices with notches.
216. What is apple-mobile-web-app-capable
?
Launch site in standalone mode on iOS.
217. What is sandbox on <iframe>
?
A: Applies extra restrictions (like blocking scripts).
218. What does allowfullscreen do?
A: Lets iframe content go fullscreen.
219. What is srcdoc in <iframe>
?
A: Inline HTML content inside iframe.
220. What is loading="lazy"
on <iframe>
?
A: Delays loading until in viewport.
221. What does data-*
do?
A: Stores custom data for JavaScript.
222. Can data-*
be styled by CSS?
A: Yes, via attribute selectors.
223. What is <picture>
tag for?
A: For responsive images.
224. What is srcset on <img>
?
A: Lists multiple images for different resolutions.
225. What is sizes attribute?
A: Informs browser how much space image will use, for better resource choice.
226. What is <output>
tag for?
A: Displays calculation results.
227. What is <template>
tag?
A: Holds HTML that isn’t rendered but can be cloned via JavaScript.
- What is
<slot>
? A: Used in Web Components for placeholder content.
229. What is <shadow>
?
A: Deprecated shadow DOM element, replaced by JS APIs.
230. How to provide fallback for <video>
?
231. How to handle unsupported HTML features?
A: Use polyfills or provide alternative content.
232. What is rel="noopener"
important for?
A: Prevents the opened page from controlling the opener’s window object.
233. Why avoid inline JavaScript?
A: More prone to XSS attacks.
234. What is CSP (Content Security Policy)?
A: HTTP header to control resources browser is allowed to load.
235. What is HTML “quirks mode”?
A: Browser mode triggered by missing <!DOCTYPE html>
causing non-standard rendering.
236. What does “strict mode” in HTML mean?
A: Using correct <!DOCTYPE html>
ensures standards mode.
237. Is HTML case sensitive?
A: No, tags and attributes are case-insensitive.
238. Can you omit closing tags?
A: For some elements like <li>
, <p>
, <td>
, browsers auto-close them.
239. What does meta http-equiv="X-UA-Compatible"
do?
A: Forces IE to use latest rendering engine.
240. How to write conditional comments for IE?
(Deprecated, but historic.)
241. What is feature detection?
A: Using JS or CSS to check if a browser supports a feature before using it.
242. How can Modernizr help?
A: JS library that detects HTML5/CSS3 features.
243. How to make a slider input?
244. How to make input for month?
245. How to make input for week?
246. How to make input for time?
247. How to enforce numeric input?
248. How to add search clear button on mobile?
Use type="search" which often triggers native clear UI.
249. How to provide fallback fonts?
250. What is “graceful degradation” in HTML?
A: Building with advanced features but ensuring content still works without them.
- How to specify input pattern for 5 digit zip code?
252. What is autocapitalize attribute?
A: Suggests capitalization to mobile keyboards.
253. What does inputmode attribute do?
A: Hints to device on keyboard type.
254. What is nomodule in <script>
?
A: Tells browser to ignore script if it supports ES modules.
255. What is <noscript>
typically used for?
A: Content shown if JS is disabled.
256. How to embed Google Fonts?
257. What is the integrity attribute?
A: Ensures CDN files haven’t been tampered.
258. What is crossorigin attribute?
A: Controls CORS requests for images/scripts.
259. What is manifest attribute on <html>
?
A: Used with old AppCache spec (now deprecated).
260. What is the title attribute on <link>
?
A: Used for alternate stylesheets.
261. What is Microdata in HTML?
A: A way to embed machine-readable data using attributes like itemscope, itemtype, itemprop.
262. How to mark up a person using Microdata?
263. What is JSON-LD?
A: A way to include linked data (structured data) in a JSON script for SEO, often for rich results.
264. Difference between Microdata and JSON-LD?
- Microdata: embedded in HTML tags.
- JSON-LD: separate
<script>
, cleaner, recommended by Google.
265. What is AMP HTML?
A: A streamlined form of HTML for faster mobile pages.
266. How to declare an AMP page?
267. Why use AMP?
A: Improves loading speed and can increase visibility on Google.
268. What is amp-img
?
A: AMP’s replacement for <img>
, requires explicit width & height.
269. What does lang attribute help with beyond screen readers?
A: Helps browsers pick correct hyphenation, fonts, and text-to-speech.
270. What is translate attribute?
A: Tells Google Translate or similar whether to translate content.
271. What is aria-busy
?
A: Indicates an element is updating, prevents announcements until finished.
272. What does meta name="format-detection"
do?
A: Controls auto-detection of phone numbers on iOS.
273. What does meta name="referrer"
do?
A: Controls Referer HTTP header.
274. What is meta http-equiv="refresh"
?
A: Redirects after a delay.
275. What is meta property="og:title"
?
A: Part of Open Graph for social sharing previews.
276. What is type="module" in <script>
?
A: Uses ES6 modules, allowing import & export.
277. What is nomodule for?
A: Loads legacy script if browser doesn't support modules.
278. Can you put async on type="module"
?
A: Modules are deferred by default. async changes to run ASAP, not recommended.
279. How to provide print styles?
280. How to ensure links look good when printed?
Use CSS like:
281. How to add closed captions to video?
282. What is <wbr>
tag?
A: Suggests a line break opportunity.
283. What does <ruby>
do?
A: Annotation for East Asian typography.
284. What is <rt>
?
A: Gives pronunciation inside <ruby>
.
285. What is <rp>
?
A: Fallback text for browsers that don't support <ruby>
.
286. What is <menu>
?
A: Intended for context menus or toolbars (rarely used today).
287. What is <menuitem>
?
A: Proposed for interactive menu items, mostly unsupported.
288. What does <!—
vs <!--
signify?
A: <!—
is invalid, needs to be <!--
for comments.
289. Can you use emoji in HTML content?
A: Yes, natively supported.
290. What does <noscript>
do inside <head>
?
A: Can provide fallback CSS for users with JS disabled.
291. How to dynamically add HTML with JS?
292. What is innerHTML
vs textContent
?
innerHTML
: parses HTML tags.textContent
: inserts plain text.
293. How to prevent XSS with dynamic content?
A: Sanitize input, avoid innerHTML when possible.
294. What does ondragstart do?
A: Handles start of a drag operation.
295. What is dataTransfer?
A: JS object to hold drag data during drag-and-drop.
296. What is aria-checked?
A: State of checkboxes or menu items.
297. What is aria-selected?
A: Marks selected elements in a list/grid.
298. What is aria-haspopup?
A: Indicates an element opens a popup.
299. What is aria-pressed?
A: Indicates toggle button state.
300. How to ensure good tab order?
A: Use tabindex, logical DOM order, and avoid removing focusable elements.