HTML Interview Questions

HTML interview questions along with their answers that are commonly asked by companies:

  1. 1. What is HTML?

    • Answer: HTML, or HyperText Markup Language, is the standard language for creating and designing web pages.

  2. 2. Explain the structure of an HTML document.

    • Answer: An HTML document consists of the following elements:
<!DOCTYPE html>
    <title>Page Title</title>
    <!-- Content goes here -->

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

    • Answer: It specifies the HTML version being used, and in this case, it indicates HTML5.

  2. 4. What is the difference between HTML and XHTML?

    • Answer: XHTML is a stricter and more XML-based version of HTML. It requires well-formed documents and adheres to XML syntax rules.

  3. 5. Explain the difference between <div> and <span> elements.

    • Answer: <div> is a block-level element used to group other HTML elements, while <span> is an inline element used for applying styles or scripting to a specific section of text.

  4. 6. What is semantic HTML?

    • Answer: Semantic HTML involves using HTML elements that carry meaning about the structure of the page, such as <header>, <footer>, <nav>, and <article>.

  5. 7. What is the purpose of the alt attribute in an <img> tag?

    • Answer: The alt attribute provides alternative text for an image, which is displayed if the image cannot be loaded. It also improves accessibility for users with visual impairments.

  6. 8. Explain the difference between GET and POST methods in HTML forms.

    • Answer: GET is used to request data from a specified resource, and parameters are included in the URL. POST is used to submit data to be processed to a specified resource, and the data is sent in the body of the request.

  7. 9. What is the purpose of the <meta charset="UTF-8"> tag?

    • Answer: It specifies the character encoding for the HTML document, ensuring proper display of text characters.

  8. 10. How does the <iframe> tag differ from the <frame> tag?

    • Answer: <iframe> is used to embed another document within the current HTML document, while <frame> is used in frameset documents to define individual frames.
  1. 11. Explain the purpose of the <meta name="viewport"> tag.

    • Answer: The <meta name="viewport"> tag is used to control the viewport settings, such as the width and initial scale of the viewport. It is crucial for responsive web design.

  2. 12. What is the significance of the defer attribute in the <script> tag?

    • Answer: The defer attribute is used to indicate that the script should be executed after the HTML document has been parsed. This can improve page loading performance.

  3. 13. Differentiate between the <section>, <article>, and <div> elements.

    • Answer: <section> is used to group related content, <article> is used for self-contained content that can be distributed and reused independently, and <div> is a generic container for grouping content without any specific meaning.

  4. 14. How can you create a hyperlink that opens in a new tab?

    • Answer: You can use the target="_blank" attribute in the <a> tag to open the link in a new tab:

    • <a href="" target="_blank">Visit</a>
  1. 15. What is the purpose of the HTML5 data-* attribute?

    • Answer: The data-* attribute is used to store custom data private to the page or application. It allows developers to embed custom data attributes on HTML elements.

  2. 16. How can you create a numbered list in HTML?

    • Answer: You can use the <ol> (ordered list) element along with the <li> (list item) elements:
      <ol> <li>First item</li> <li>Second item</li> <li>Third item</li> </ol>
  1. 17. What is the purpose of the <figcaption> element in HTML?

    • Answer: The <figcaption> element is used to provide a caption or legend for the content within a <figure> element, typically used for images or diagrams.

  2. 18. How can you embed a video in an HTML document?

    • Answer: You can use the <video> element and specify the video file or source within the element:

    • <video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> Your browser does not support the video tag. </video>
  1. 19. What is the purpose of the colspan and rowspan attributes in an HTML table?

    • Answer: colspan is used to define the number of columns a cell should span, and rowspan is used to define the number of rows a cell should span.

  2. 20. How can you include comments in HTML?

    • Answer: Comments in HTML are written using <!-- comment goes here -->.