Interview
HTML Interview

html interview questions and answers

1. What is HTML?

Answer: HTML stands for HyperText Markup Language. It is the standard markup language used to create and structure web pages and web applications.

2. What are the new features in HTML5 compared to HTML4?

Answer: HTML5 introduced several new features and elements, including:

  • New semantic elements (<header>, <footer>, <article>, <section>, <nav>, <aside>, <main>, <figure>, <figcaption>, etc.)
  • New form input types (<input type="email">, <input type="date">, <input type="number">, <input type="range">, etc.)
  • Multimedia elements (<audio> and <video>)
  • Canvas and SVG for drawing graphics
  • Local storage (localStorage and sessionStorage)
  • WebSockets for real-time communication

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

Answer:

  • <div> is a block-level element used to group elements for styling purposes and to create layout divisions on the page.
  • <span> is an inline-level element used for styling small portions of text or inline elements.

4. Explain the difference between margin and padding.

Answer:

  • Margin: Clears an area outside the border. It is the space outside the element.
  • Padding: Clears an area inside the border. It is the space inside the element, between the content and the border.

5. What are semantic elements in HTML?

Answer: Semantic elements are those that clearly describe their meaning in a human- and machine-readable way. They convey the structure of the page and the relationships between different parts of the content. Examples include <header>, <footer>, <article>, <section>, <nav>, <main>, etc.

6. What is the purpose of the alt attribute in images?

Answer: The alt attribute in images specifies an alternate text for an image, which is displayed if the image cannot be loaded or for users who use screen readers. It is also important for SEO (Search Engine Optimization).

7. What is the difference between <script>, <script async>, and <script defer>?

Answer:

  • <script>: Loads and executes the script immediately, blocking HTML parsing until it is downloaded and executed.
  • <script async>: Loads the script asynchronously while continuing to parse the HTML. The script executes as soon as it is available, possibly before the page has finished parsing.
  • <script defer>: Loads the script asynchronously while continuing to parse the HTML. The script executes only after the HTML parsing is complete.

8. How can you create a hyperlink in HTML?

Answer: Hyperlinks are created using the <a> (anchor) element with the href attribute. For example:

<a href="https://example.com">Visit Example</a>

9. What is the doctype declaration?

Answer: The <!DOCTYPE> declaration is used to inform the web browser about the type and version of HTML used in the document. It should be placed at the very beginning of an HTML document, before the <html> tag.

10. What is the difference between <ol>, <ul>, and <dl>?

Answer:

  • <ol>: Ordered list. It represents a list of items in which the order of the items does matter (numbered list).
  • <ul>: Unordered list. It represents a list of items in which the order of the items does not matter (bulleted list).
  • <dl>: Description list. It consists of a list of terms and their associated descriptions.

11. What is the difference between block and inline elements?

Answer:

  • Block-level elements: They start on a new line and take up the full width available (e.g., <div>, <p>, <h1> - <h6>).
  • Inline elements: They do not start on a new line and only take up as much width as necessary (e.g., <span>, <a>, <strong>).

12. How can you embed a video in HTML?

Answer: Videos can be embedded using the <video> element. For example:

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

13. How can you comment in HTML?

Answer: Comments in HTML are written using <!-- -->. For example:

<!-- This is a comment -->

14. How do you create a table in HTML?

Answer: Tables in HTML are created using the <table>, <tr>, <th>, and <td> elements. For example:

<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John</td>
    <td>30</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>25</td>
  </tr>
</table>

15. What is the difference between <section>, <article>, and <div>?

Answer:

  • <section>: Defines a section in a document.
  • <article>: Represents an independent, self-contained content.
  • <div>: Used for grouping content and applying styles.

These are some common HTML interview questions and answers. Make sure to understand these concepts well to prepare for your HTML interview.