HTML & CSS Interview Questions and Answers

Semantics, accessibility, layout systems, responsive design and modern CSS.

Practise 10 random 156 peer-reviewed questions
HTML & CSS Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level HTML & CSS interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 What is the CSS box model? Easy

Every element is a box made of four layers: content, padding, border and margin. By default width applies to the content box, so padding and border are added on top (content-box). box-sizing: border-box makes width include padding and border, which is why most codebases set it globally.

*, *::before, *::after { box-sizing: border-box; }

Margin collapsing, percentage widths relative to the containing block, and negative margins are common follow-up questions.

2 What are semantic HTML elements and why do they matter? Easy

Semantic elements describe their meaning rather than only their appearance: header, nav, main, article, section, aside, footer, figure, time, button.

Benefits:

  • Accessibility: screen readers announce landmarks and roles, letting users navigate by region.
  • SEO: crawlers understand document structure and importance.
  • Maintainability: code is self-documenting.
  • Free behaviour: button, a, input and form come with keyboard handling and built-in semantics, whereas clickable divs require manual ARIA and key handlers.

Rule: use the native element that matches the meaning; only add ARIA when no native element exists, and never override correct native semantics.

3 What is HTML and what are it's basic components? Easy

HTML (Hyper Text Markup Language) is the standard markup language for creating web pages. It's basics components includes element, tags and attribute.

4 What is the purpose of Doctype in HTML? Easy

The Doctype declaration specifies the type of document being used and tell the web browser how to interpret the pages content. It is located at the top of the HTML document.

5 What is semantic HTML? Easy

Semantic HTML uses specific HTML elements to provide additional information about the structure and content of the page, making it more accessible and easy to read.

6 What is the difference between div element and span element? Easy

The div element is a block-level element that is used to group and organize other HTML elements while span element is an inline-element that is used to apply style or attribute to a specific part of a block-level element.

7 What is the difference between <b> and <strong> tags in HTML? Easy

The <B> tag is used to apply bold formatting to text, while the <strong> tag is used to indicate that the text is important and carries stronger semantic meaning than <b>.

8 What is the difference between a GET request and POST request? Easy

The GET request is used to retrieve data from a web server, while a POST request is used to submit data to a web server. GET requests are less secure and have limit on the amount of data that can be sent while POST request have no limit and are more secure.

9 How do you create a hyperlink in HTML? Easy

Hyperlinks are created using the anchor element (<a>) paired with the essential href (Hypertext Reference) attribute:

<!-- External Link with Security Hardening -->
<a href="https://hirextech.com/categories" target="_blank" rel="noopener noreferrer">
  Explore Technical Interview Categories
</a>

<!-- Internal Anchor Navigation -->
<a href="#salary-insights">Jump to Salary Section</a>

<!-- Email and Telephone Triggers -->
<a href="mailto:support@hirextech.com">Email Support</a>
<a href="tel:+18005550199">Call Support</a>

### Critical Security Best Practice:
Whenever using target="_blank" to open a new browser tab, you must include rel="noopener noreferrer". Without this, the newly opened tab can access the original window's window.opener object, introducing vulnerability to reverse tabnabbing phishing attacks.

10 What is the purpose of the alt attribute in HTML? Easy

The alt attribute is used to provide a text description of an image for users who are unable in to see the image, such as those who use screen readers.

11 What is the difference between <link> tag and <a> tag? Easy

The anchor tag <a> is used to create a hyperlink to another web page or to a certain part of a web page and these links are clickable, whereas, link tag <link> defines a link between a document and external resource and these are not clickable.

12 What is the canvas element in HTML5? Easy

The <canvas> element provides a scriptable bitmap drawing surface in HTML. It acts as an empty resolution-dependent drawing container that JavaScript manipulates via procedural drawing APIs:

### Rendering Contexts:

  • 2D Context (getContext('2d')): Used for drawing paths, boxes, text, gradients, and 2D games.
  • WebGL / WebGL2 (getContext('webgl')): Hardware-accelerated 3D graphics rendering powered by the client GPU (used by Three.js and Babylon.js).

### Code Example:

<canvas id="myCanvas" width="400" height="200"></canvas>

<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');
  
  // Draw rectangle
  ctx.fillStyle = '#2563eb';
  ctx.fillRect(20, 20, 150, 100);
</script>

*Difference from SVG:* Canvas renders pixels onto a raster bitmap (faster for thousands of moving particles or video frame manipulation), whereas SVG is an XML DOM tree of vector shapes (infinitely scalable and individually inspectable in the DOM).

13 What is an HTML form? Easy

An HTML form (<form>) is a document container designed to collect user input and transmit it securely to a web server or client-side JavaScript handler.

### Core Form Anatomy:

<form action="/api/feedback" method="POST" enctype="application/x-www-form-urlencoded">
  <label for="username">Full Name:</label>
  <input type="text" id="username" name="username" required>
  
  <button type="submit">Submit Feedback</button>
</form>

### Essential Form Attributes:

  • action: Target URL endpoint where data is sent upon submission.
  • method: HTTP verb used for transmission:
  • GET: Appends form data as URL query parameters (?name=John), suitable for search queries.
  • POST: Packages data in the HTTP request body, required for sensitive data or database mutations.
  • enctype: Encoding format (multipart/form-data is mandatory when uploading files).
14 What is the difference between HTML form's "action" and "method" attributes? Easy

The "action" attribute specifies the URL to which the form data is submitted, while the "method" attribute specifies the HTTP method when submitting the form data(either "GET" or "POST").

15 What is the purpose of <head> tag? Easy

The <head> tag in HTML is used to provide metadata about document, such as title of page, links and other information that is not directly displayed on the web page. This information is used by browser and search engine to understand the content and structure of the web page.

16 What is the purpose of the <meta> tag in HTML? Easy

The <meta> tag is used to provide additional information about the webpage, such as author, keywords, description which is used by search engines to understand the content of the page.

17 What is the purpose of the <script> tag in HTML? Easy

The <script> tag is either used to embed client-side script(Javascript), or it points to an external script file through the src attribute.

18 What is the purpose of the viewport meta tag? Easy

The viewport meta tag informs mobile browsers how to control the page's dimensions and scaling relative to the physical device screen:

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

### Why It Is Mandatory for Responsive Web Design:

  1. Disables Desktop Zoom Emulation:

Without this tag, mobile browsers assume the website is built for a 980px desktop monitor and scale the page down, resulting in microscopic, unreadable text.

  1. width=device-width: Sets the width of the virtual viewport to match the physical screen width of the device in CSS pixels.
  2. initial-scale=1.0: Establishes a 1:1 ratio between CSS pixels and device-independent pixels upon initial page load.

*Accessibility note:* Avoid setting user-scalable=no or maximum-scale=1.0 as doing so prevents visually impaired users from zooming in, violating WCAG accessibility guidelines.

19 What is the purpose of the <noscript> tag in HTML? Easy

The <noscript> tag in used to display text for those browsers which does not support script tag or the browser disabled the script by user.

20 What is the purpose of the <fieldset> tag in HTML? Easy

The <fieldset> tag is used in HTML forms to logically group related form controls and labels together, both visually and semantically:

<fieldset>
  <legend>Billing Address Information</legend>
  
  <label for="street">Street Address:</label>
  <input type="text" id="street" name="street">
  
  <label for="zip">Postal Code:</label>
  <input type="text" id="zip" name="zip">
</fieldset>

### Key Advantages:

  1. Accessibility (Screen Readers):

The child <legend> provides context. Screen readers announce the legend before each input within the fieldset, which is crucial for grouped radio buttons or complex shipping forms.

  1. Mass Disabling:

Setting <fieldset disabled> automatically disables all form controls nested inside it simultaneously, eliminating the need to set disabled on every input individually.

Showing 20 of 156 questions

Frequently Asked Questions About HTML & CSS Interviews

What do hiring managers evaluate in HTML & CSS technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing HTML & CSS questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.