├── index.js ├── styles.css └── index.html /index.js: -------------------------------------------------------------------------------- 1 | // Toggle the display of answers when the button is clicked 2 | const toggleButtons = document.querySelectorAll('.toggle-answer'); 3 | 4 | toggleButtons.forEach(button => { 5 | button.addEventListener('click', function () { 6 | const answer = this.nextElementSibling; 7 | if (answer.style.display === 'block') { 8 | answer.style.display = 'none'; 9 | this.textContent = 'Show Answer'; 10 | } else { 11 | answer.style.display = 'block'; 12 | this.textContent = 'Hide Answer'; 13 | } 14 | }); 15 | }); 16 | 17 | // Dynamically set the current year in the footer 18 | const yearSpan = document.getElementById('year'); 19 | const currentYear = new Date().getFullYear(); 20 | yearSpan.textContent = currentYear; 21 | -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | /* Mobile-first styles */ 2 | * { 3 | margin: 0; 4 | padding: 0; 5 | box-sizing: border-box; 6 | } 7 | 8 | body { 9 | font-family: Arial, sans-serif; 10 | background-color: #f9f9f9; 11 | color: #333; 12 | padding: 10px; 13 | } 14 | 15 | header { 16 | text-align: center; 17 | margin-bottom: 20px; 18 | } 19 | 20 | h1 { 21 | font-size: 24px; 22 | color: #2c3e50; 23 | } 24 | 25 | main { 26 | margin: 0 auto; 27 | max-width: 600px; 28 | } 29 | 30 | .question { 31 | margin-bottom: 20px; 32 | border: 1px solid #ddd; 33 | border-radius: 5px; 34 | background-color: #fff; 35 | padding: 15px; 36 | } 37 | 38 | .question h2 { 39 | font-size: 18px; 40 | margin-bottom: 10px; 41 | } 42 | 43 | button.toggle-answer { 44 | padding: 10px; 45 | font-size: 14px; 46 | color: white; 47 | background-color: #3498db; 48 | border: none; 49 | border-radius: 5px; 50 | cursor: pointer; 51 | } 52 | 53 | button.toggle-answer:hover { 54 | background-color: #2980b9; 55 | } 56 | 57 | .answer { 58 | margin-top: 10px; 59 | display: none; 60 | } 61 | 62 | .answer pre { 63 | background-color: #f4f4f4; 64 | padding: 10px; 65 | border-radius: 5px; 66 | overflow-x: auto; 67 | } 68 | 69 | /* Footer styles */ 70 | footer { 71 | text-align: center; 72 | margin-top: 40px; 73 | padding: 10px; 74 | background-color: #2c3e50; 75 | color: white; 76 | } 77 | 78 | footer p { 79 | font-size: 14px; 80 | } 81 | 82 | /* Media Queries for larger devices */ 83 | @media (min-width: 768px) { 84 | h1 { 85 | font-size: 32px; 86 | } 87 | 88 | .question h2 { 89 | font-size: 22px; 90 | } 91 | 92 | button.toggle-answer { 93 | font-size: 16px; 94 | } 95 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Top 30 HTML Interview Questions 8 | 9 | 10 | 11 | 12 |
13 |

Top 30 Commonly Asked HTML Interview Questions

14 |
15 | 16 |
17 |
18 | 19 |

Easy

20 | 21 | 22 |
23 |

1. What is HTML?

24 | 25 |
26 |

HTML stands for HyperText Markup Language. It is used to create web pages and web applications. 27 | HTML describes the structure of a web page using markup. HTML elements represent different parts 28 | of the page, such as headings, paragraphs, links, and more.

29 |
30 |
31 | 32 | 33 |
34 |

2. How do you create a hyperlink in HTML?

35 | 36 |
37 |

You create a hyperlink using the <a> tag. The href attribute 38 | specifies the destination URL.

39 |
<a href="https://example.com">Visit Example</a>
40 |
41 |
42 | 43 | 44 |
45 |

3. How do you include CSS in an HTML document?

46 | 47 |
48 |

CSS can be included in an HTML document in three ways: inline styles, internal styles using the 49 | <style> tag, or external stylesheets using the <link> tag. 50 |

51 |
52 |
53 | 54 | 55 |
56 |

4. How do you add an image to an HTML page?

57 | 58 |
59 |

You can add an image using the <img> tag with the src attribute 60 | to specify the image source.

61 |
<img src="image.jpg" alt="Description of image">
62 |
63 |
64 | 65 | 66 |
67 |

5. How do you create a list in HTML?

68 | 69 |
70 |

HTML provides two types of lists: ordered lists (<ol>) and unordered lists 71 | (<ul>). List items are added using the <li> tag.

72 |
73 |
74 | 75 | 76 |

Medium

77 | 78 | 79 |
80 |

6. What is the purpose of the <meta> tag in HTML?

81 | 82 |
83 |

The <meta> tag provides metadata about the HTML document, such as charset, 84 | description, and keywords. It helps with SEO and rendering of the page.

85 |
86 |
87 | 88 | 89 |
90 |

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

91 | 92 |
93 |

The <div> tag is a block-level element, while the <span> 94 | tag is an inline element. <div> is used for grouping larger content, and 95 | <span> for smaller text within a line. 96 |

97 |
98 |
99 | 100 | 101 |
102 |

8. What does the <form> tag do?

103 | 104 |
105 |

The <form> tag is used to create a form for user input. It typically includes 106 | input fields, checkboxes, radio buttons, and a submit button.

107 |
108 |
109 | 110 | 111 |
112 |

9. How do you create a table in HTML?

113 | 114 |
115 |

A table is created using the <table> tag, with rows created using 116 | <tr>, and data cells within each row using <td>. 117 |

118 |
119 |
120 | 121 | 122 |
123 |

10. What are semantic HTML elements?

124 | 125 |
126 |

Semantic HTML elements clearly describe their meaning in a human- and machine-readable way, like 127 | <article>, <section>, and <footer>, as 128 | opposed to non-semantic elements like <div> or <span>. 129 |

130 |
131 |
132 | 133 | 134 |

Advanced

135 | 136 | 137 |
138 |

11. What is the purpose of the <!DOCTYPE html> declaration?

139 | 140 |
141 |

The <!DOCTYPE> declaration informs the browser about the version of HTML being 142 | used, ensuring proper rendering of the page.

143 |
144 |
145 | 146 | 147 |
148 |

12. What are HTML5 APIs and how do they work?

149 | 150 |
151 |

HTML5 APIs are a set of web technologies that allow for more dynamic interactions, including Web 152 | Storage, Geolocation, and Canvas APIs, enabling features like offline storage, location-based 153 | services, and drawing graphics.

154 |
155 |
156 | 157 | 158 |
159 |

13. How do you handle responsive design in HTML?

160 | 161 |
162 |

Responsive design in HTML is achieved using CSS media queries and viewport settings to make web 163 | pages adapt to different screen sizes and devices.

164 |
165 |
166 | 167 | 168 |
169 |

14. What is the difference between the id and class attributes, and when should each be used?

170 | 171 |
172 |

The id attribute is used for uniquely identifying a single element, while the 173 | class attribute can be used for multiple elements. id should be used 174 | for one-time identification, whereas class is used for applying styles or behaviors 175 | to a group of elements. 176 |

177 |
178 |
179 | 180 | 181 |
182 |

15. How do you implement HTML5 local storage and session storage?

183 | 184 |
185 |

Local storage and session storage are part of HTML5’s Web Storage API. Local storage persists 186 | data across sessions until explicitly deleted, while session storage stores data for the 187 | duration of the page session. You can use the localStorage.setItem() and 188 | sessionStorage.setItem() methods to store data. 189 |

190 |
191 |
192 | 193 | 194 | 195 | 196 |
197 |

16. What are custom data attributes and how are they used in HTML?

198 | 199 |
200 |

Custom data attributes start with data-. They are used to store extra information 201 | on an HTML element that doesn't have any visual impact. For example, <div 202 | data-user-id="123"> stores the user ID as a custom attribute. These 203 | attributes can 204 | be accessed via JavaScript using the dataset property.

205 |
206 |
207 | 208 | 209 |
210 |

17. How do you use the <picture> element for responsive images?

211 | 212 |
213 |

The <picture> element allows you to specify multiple image sources for 214 | responsive design. You can provide different image formats or resolutions that are used based on 215 | media conditions, such as screen size or pixel density. Inside the <picture> 216 | tag, you use <source> elements with a srcset attribute to define 217 | different image resources. For example:

218 |
<picture>
219 |       <source media="(min-width: 800px)" srcset="large.jpg">
220 |       <source media="(min-width: 400px)" srcset="medium.jpg">
221 |       <img src="small.jpg" alt="Responsive image">
222 |     </picture>
223 |
224 |
225 | 226 | 227 |
228 |

18. What is the purpose of the rel attribute in the <link> tag?

229 | 230 |
231 |

The rel attribute in the <link> tag specifies the relationship 232 | between the current document and the linked resource. For example, rel="stylesheet" 233 | tells the browser that the linked file is a CSS stylesheet. There are other values for 234 | rel, 235 | like rel="icon" for linking favicons and rel="nofollow" for links you 236 | don't want search engines to follow. 237 |

238 |
239 |
240 | 241 | 242 |
243 |

19. How do you use the srcset attribute in the <img> tag for responsive images?

244 | 245 |
246 |

The srcset attribute in the <img> tag is used to define a list of 247 | image sources with varying resolutions or sizes. The browser selects the appropriate image 248 | based on the user's device screen size and resolution. For example:

249 |
<img src="small.jpg" 
250 |       srcset="medium.jpg 768w, large.jpg 1280w" 
251 |       sizes="(max-width: 600px) 480px, 800px" alt="Responsive image">
252 |
253 |
254 | 255 | 256 |
257 |

20. What is the Shadow DOM and how can it be used in HTML?

258 | 259 |
260 |

The Shadow DOM is a web standard that allows you to encapsulate a section of the DOM tree in a 261 | separate "shadow" root. This is useful for building reusable components where the internal 262 | structure is isolated from the main document's styles and scripts. For example, custom elements 263 | can use shadow DOM to prevent style leakage. To create a shadow DOM, use JavaScript:

264 |
let shadow = element.attachShadow({ mode: 'open' });
265 |     shadow.innerHTML = '<p>This is shadow DOM content</p>';
266 |
267 |
268 | 269 | 270 |
271 |

21. How do you implement ARIA roles and attributes for accessibility in HTML?

272 | 273 |
274 |

ARIA (Accessible Rich Internet Applications) roles and attributes improve web accessibility for 275 | users with disabilities. ARIA roles, such as role="button" or 276 | role="navigation", 277 | define the purpose of elements for screen readers. ARIA attributes like aria-label 278 | and aria-hidden provide additional context or hide elements from assistive 279 | technologies. 280 | Example: 281 |

282 |
<button aria-label="Close window">X</button>
283 |
284 |
285 | 286 | 287 |
288 |

22. How can you optimize HTML performance for faster page load times?

289 | 290 |
291 |

To optimize HTML performance, you can follow best practices like minimizing the use of inline 292 | styles, reducing the number of HTTP requests, compressing images, using async or defer 293 | attributes 294 | for JavaScript, and using external stylesheets. Enabling caching and using a content delivery 295 | network (CDN) can also help speed up page load times.

296 |
297 |
298 | 299 | 300 |
301 |

23. What is the difference between <article>, <section>, and <aside> elements in 302 | HTML5?

303 | 304 |
305 |

The <article> element represents a self-contained composition that could be 306 | distributed independently, such as a blog post or news article. The <section> 307 | element defines a thematic grouping of content within a document. The <aside> 308 | element is used for content that is tangentially related to the main content, such as sidebars 309 | or 310 | related links.

311 |
312 |
313 | 314 | 315 |
316 |

24. How do you use the <template> element in HTML?

317 | 318 |
319 |

The <template> element is used to store HTML fragments that are not rendered 320 | when the page initially loads. The content inside the <template> can be 321 | cloned and inserted into the document later using JavaScript. This is useful for creating 322 | dynamic content. Example:

323 |
<template id="my-template">
324 |       <div>This content will be rendered later.</div>
325 |     </template>
326 |     
327 |     
332 |
333 |
334 | 335 | 336 |
337 |

25. What are the best practices for structuring an HTML5 document?

338 | 339 |
340 |

Best practices for structuring an HTML5 document include using semantic HTML tags like 341 | <header>, <footer>, <article>, and 342 | <section> to define meaningful sections of a page. Always include a DOCTYPE 343 | declaration, and use <meta> tags for character encoding and viewport settings 344 | to ensure proper display on all devices. 345 |

346 |
347 |
348 | 349 | 350 |
351 |

26. What is the purpose of the <!DOCTYPE html> declaration?

352 | 353 |
354 |

The <!DOCTYPE html> declaration tells the browser that the document is 355 | written in HTML5. It ensures that the browser renders the page in standards mode, which 356 | avoids quirks mode where old or incorrect HTML rendering might occur.

357 |
358 |
359 | 360 | 361 | 362 |
363 |

27. How do you add comments in HTML?

364 | 365 |
366 |

Comments in HTML are added using <!-- and -->. Anything written 367 | between these tags will not be rendered on the webpage. Comments are useful for annotating code 368 | and leaving notes for yourself or other developers. Example:

369 |
<!-- This is a comment -->
370 |
371 |
372 | 373 | 374 |
375 |

28. What are the differences between block-level and inline-level elements?

376 | 377 |
378 |

Block-level elements, like <div>, <p>, and 379 | <h1>, take up the full width of their container and always start on a new 380 | line. 381 | Inline-level elements, like <span> and <a>, take up only 382 | as 383 | much width as necessary and do not start on a new line. Block elements can contain inline 384 | elements, 385 | but inline elements cannot contain block elements. 386 |

387 |
388 |
389 | 390 | 391 |
392 |

29. What is the purpose of the <meta> tag in HTML?

393 | 394 |
395 |

The <meta> tag is used to provide metadata about the HTML document. Metadata 396 | is 397 | information about the data itself and is not displayed on the page. Common uses include 398 | specifying the character set (e.g., UTF-8), the viewport for responsive design, and descriptions 399 | for 400 | search engine optimization (SEO). Example:

401 |
<meta charset="UTF-8">
402 | <meta name="viewport" content="width=device-width, initial-scale=1">
403 | <meta name="description" content="This is a description of the page.">
404 |
405 |
406 | 407 | 408 |
409 |

30. What is semantic HTML, and why is it important?

410 | 411 |
412 |

Semantic HTML refers to using HTML elements according to their intended meaning rather than 413 | for their appearance. For example, using <article> for content that could 414 | be distributed independently, rather than a <div>. This enhances 415 | accessibility, SEO, and code readability, making it easier for browsers and assistive 416 | technologies 417 | to interpret the content.

418 |
419 |
420 | 421 | 422 |
423 |

31. What is the difference between <ol> and <ul> elements in HTML?

424 | 425 |
426 |

The <ol> element creates an ordered list, where items are numbered 427 | automatically. The <ul> element creates an unordered list, where items 428 | are usually marked with bullet points. Both elements use <li> tags to 429 | represent list items. Example:

430 |
<ul>
431 |   <li>Item 1</li>
432 |   <li>Item 2</li>
433 | </ul>
434 | 
435 | <ol>
436 |   <li>Item 1</li>
437 |   <li>Item 2</li>
438 | </ol>
439 |
440 |
441 | 442 | 443 |
444 |

32. What is the purpose of the <head> section in an HTML document?

445 | 446 |
447 |

The <head> section contains metadata and links to resources that the browser 448 | uses to render the page, but it is not visible to users. It usually includes elements such as 449 | <meta> tags, <title>, stylesheets, and scripts. The 450 | content of the <head> is essential for performance, SEO, and usability. 451 |

452 |
453 |
454 | 455 | 456 |
457 |

33. How do you create a table in HTML?

458 | 459 |
460 |

A table in HTML is created using the <table> element, with rows defined by 461 | <tr>, and data cells defined by <td>. Table headers 462 | use <th>. Example: 463 |

464 |
<table>
465 |   <tr>
466 |     <th>Name</th>
467 |     <th>Age</th>
468 |   </tr>
469 |   <tr>
470 |     <td>John</td>
471 |     <td>25</td>
472 |   </tr>
473 | </table>
474 |
475 |
476 | 477 | 478 |
479 |

34. What is the purpose of the <iframe> tag in HTML?

480 | 481 |
482 |

The <iframe> tag is used to embed another HTML document inside the current 483 | document. This is often used for embedding external content, such as videos or maps, from 484 | other websites. Example:

485 |
<iframe src="https://example.com" width="600" height="400">
486 |   <p>Your browser does not support iframes.</p>
487 | </iframe>
488 |
489 |
490 | 491 | 492 |
493 |

35. How do you create a hyperlink in HTML?

494 | 495 |
496 |

A hyperlink is created using the <a> tag. The href attribute 497 | specifies the URL of the page the link points to. The link text is placed between the 498 | opening and closing <a> tags. Example:

499 |
<a href="https://example.com">Visit Example.com</a>
500 |
501 |
502 | 503 |
504 |
505 | 506 | 509 | 510 | 511 | 512 | 513 | --------------------------------------------------------------------------------