Progressive Full Stack Application Development with Live Projects

Mock InterviewWeb Essentials

HTML Interview for Experienced Developers

HTML Mock Interview
  • HTML Mock Interview Questions and Answers for Experienced Developers

This Interview Post lists some advanced HTML interview questions, along with their answers, tailored for 5+ years experienced candidates. These questions and answers should give you a good overview of the types of topics you might encounter in an advanced HTML interview.


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

The <!DOCTYPE html> declaration is used to define the document type and version of HTML. It helps the browser render the page in standards mode, ensuring that the HTML document is parsed and displayed correctly.

If this declaration is improper or omitted then the web page is run in quirks mode. Quirks mode is a web browser setting that allows older web pages to render correctly by emulating the behavior of older browsers.


2. What is the role of the meta tag in HTML?

The <meta> tags provide additional details about the web page. This metadata is provided in the head tag and can include information such as character set, author, description, viewport settings, search engine data and more.

Examples:

  • Character encoding: <meta charset="UTF-8">
  • Viewport settings for responsive design: <meta name="viewport" content="width=device-width, initial-scale=1.0">
  • Document description: <meta name="description" content="A description of the page">

3. What are the new features introduced in HTML5?

HTML5 introduced several new features and APIs, including:

  • New semantic elements: <header>, <footer>, <section>, <article>, <nav>, <aside>, etc.
  • Form enhancements: New input types (date, email, range, etc.) and attributes (placeholder, required, etc.).
  • Multimedia support: Native support for audio and video with <audio> and <video> elements.
  • Canvas API: <canvas> element for drawing graphics.
  • Local Storage: localStorage and sessionStorage for client-side storage.
  • Geolocation API: Access to geographical location of the user.
  • WebSockets: Real-time communication between client and server.

4. What is data- attribute in HTML?

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 without affecting the validity of the HTML. These attributes are accessible via JavaScript using the dataset property.

Example:

<div data-user-id="12345" data-role="admin">User Info</div>

Accessing the data in JavaScript:

const element = document.querySelector('div');
console.log(element.dataset.userId); // "12345"
console.log(element.dataset.role); // "admin"

5. Explain the shadow DOM and its advantages.

Shadow DOM is a web standard that enables encapsulation of HTML, CSS, and JavaScript into a single component. It allows for the creation of components with their own isolated DOM subtree, which is not affected by the main document’s styles or scripts.

Advantages:

  • Encapsulation: Styles and scripts inside the shadow DOM do not affect the outer DOM and vice versa.
  • Reusability: Components can be reused without worrying about style conflicts or script issues.
  • Maintainability: It helps in creating self-contained components that are easier to maintain and debug.

Example:

<my-component></my-component>

<script>
  class MyComponent extends HTMLElement {
    constructor() {
      super();
      const shadow = this.attachShadow({ mode: 'open' });
      shadow.innerHTML = `
        <style>
          p { color: red; }
        </style>
        <p>Hello from Shadow DOM!</p>
      `;
    }
  }
  customElements.define('my-component', MyComponent);
</script>

6. What are ARIA roles and why are they important?

ARIA (Accessible Rich Internet Applications) roles are attributes added to HTML elements to improve accessibility for users with disabilities. They provide information about the role or purpose of an element in a web application.

Importance:

  • Accessibility: ARIA roles help assistive technologies (like screen readers) understand and interact with dynamic content.
  • Enhanced User Experience: They ensure that users with disabilities can navigate and use web applications effectively.

Example:

<button role="button" aria-label="Close">X</button>

7. Describe the difference between <section>, <article>, and <div> elements.

<section>: Represents a thematic grouping of content, typically with a heading. It is used to group related content into sections, which can be standalone or part of a larger document.

<article>: Represents a self-contained piece of content that can be distributed independently, such as a blog post, news article, or forum post.

<div>: A generic container with no semantic meaning. It is used for grouping elements for styling or scripting purposes.

Example:

<section>
  <h1>Section Title</h1>
  <p>Section content...</p>
</section>

<article>
  <h2>Article Title</h2>
  <p>Article content...</p>
</article>

<div>
  <p>Non-semantic grouping...</p>
</div>

8. How can you optimize the performance of a web page in terms of HTML?

Optimizing the performance of a web page can be approached from multiple angles:

  • Minimize HTML size: Remove unnecessary whitespace, comments, and use minification tools.
  • Use semantic HTML: Semantic elements like <header>, <footer>, <main>, etc., improve the structure and efficiency of the document.
  • Optimize rendering: Ensure the HTML structure supports efficient rendering by avoiding deeply nested elements and using modern layout techniques (like Flexbox or Grid).
  • Lazy loading: Use attributes like loading="lazy" for images and iframes to defer loading until they are in the viewport.
  • Defer parsing: Use defer or async attributes for script tags to prevent blocking HTML parsing.

Example:

<img src="large-image.jpg" loading="lazy" alt="Large Image">

9. What is the Content-Security-Policy (CSP) and how does it work?

The Content-Security-Policy (CSP) is a security feature that helps prevent various types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. It allows web developers to specify which content sources are considered safe.

How it works:

  • Policy Definition: CSP is defined using a Content-Security-Policy header or <meta> tag, specifying allowed sources for content like scripts, styles, images, etc.
  • Enforcement: The browser enforces the policy by blocking resources that do not match the allowed sources.

Example:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://apis.example.com">

10. What is rel attribute in the <link> tag?

The rel (or relation) attribute in the <link> tag specifies the relationship between the current document and the linked resource. It influences how browsers handle and cache linked resources.

Common values for rel:

  • stylesheet: Links to an external stylesheet.
  • icon: Specifies an icon for the page.
  • preload: Preloads resources to improve performance.

Example:

<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
<link rel="preload" href="important-script.js" as="script">

Leave a Reply