FrontendDeveloper.in

HTML Interview Questions

  • Question 1

    What is semantic HTML and why does it matter?

    Semantic HTML uses elements that describe meaning, such as header, main, article, nav, and footer, instead of generic wrappers everywhere.

    It matters because:

    • It improves accessibility for screen readers.
    • It helps search engines understand page structure.
    • It makes code easier to maintain for developers.
  • Question 2

    What is the difference between `id` and `class`?

    id should be unique in a document and is best for one specific element. class can be reused across many elements.

    Use class for styling patterns and component states. Use id for unique relationships like label associations or in-page anchors.

  • Question 3

    What is the purpose of the `doctype` declaration?

    <!doctype html> tells the browser to render the page in standards mode.

    Without it, some browsers may switch to quirks mode, which can change layout behavior and cause inconsistent rendering.

  • Question 5

    What are `data-*` attributes used for?

    data-* attributes let you store custom data directly on HTML elements.

    Example:

    <button data-product-id="sku_123">Buy</button>
    

    This is useful for connecting markup with JavaScript behavior without creating invalid custom attributes.

  • Question 6

    What is the purpose of the `meta viewport` tag?

    The viewport tag controls responsive scaling on mobile devices.

    Typical usage:

    <meta name="viewport" content="width=device-width, initial-scale=1" />
    

    This ensures the layout width maps to device width and avoids zoomed-out mobile rendering.

  • Question 8

    Why is alt text important for images?

    alt text provides a text alternative for users who cannot see images and is announced by assistive technologies.

    It also helps when images fail to load and gives search engines context about image content.

  • Question 9

    What is ARIA and when should you use it?

    ARIA adds accessibility metadata when native HTML alone cannot express needed semantics.

    Rule of thumb: use native semantic elements first. Add ARIA only when necessary for custom widgets.

  • Question 10

    What is the difference between `button` and `a`?

    Use button for actions (open modal, submit form, toggle state). Use a for navigation to another URL.

    Using the right element improves keyboard behavior and accessibility by default.

  • Question 11

    What does the `required` attribute do in forms?

    required enforces client-side validation so users cannot submit until the field has a value.

    It improves UX, but server-side validation is still mandatory for security and data integrity.

  • Question 15

    What are void elements in HTML?

    Void elements do not have closing tags because they cannot contain children, e.g., img, input, br, hr, meta, link.

    Writing closing tags for void elements is invalid in HTML.

Get LinkedIn Premium at Rs 399