All HTML Interview Questions & Answers

All HTML interview questions with simple answers. Covers tags and elements, attributes, links, images, lists, tables, forms, meta tags, semantic elements, HTML5 and web accessibility.

What is the use of colspan and rowspan in HTML?

colspan merges a cell with the cells next to it (across columns), and rowspan merges it with the cells below (down rows).

<table>
  <tr>
    <th colspan="2">Report</th>
  </tr>
  <tr>
    <td rowspan="2">Marks</td>
    <td>Maths: 90</td>
  </tr>
  <tr>
    <td>Science: 85</td>
  </tr>
</table>

Remember to remove the extra cells that are covered by the merged cell. See Rowspan and Colspan.

What is the difference between GET and POST methods in an HTML form?

Both are values of the method attribute of a form.

PointGETPOST
Data goesIn the URLIn the body of the request
Visible to userYesNo
BookmarkPossibleNot possible
Size limitLimited by URL lengthMuch bigger
Use forSearch, filtersLogin, registration, file upload
<form action="search.php" method="get">...</form>
<form action="login.php" method="post">...</form>

Never send a password with GET, because it stays in the URL and the browser history. See HTML Forms.

What is the difference between div and section in HTML?

<div> is a generic container with no meaning. It is used when you only need a box for styling or layout. <section> is a semantic element for a group of related content, like a chapter, and it should normally have a heading.

<section>
  <h2>Our Services</h2>
  <div class="card">Web design</div>
  <div class="card">SEO</div>
</section>

Use section when the content makes a real topic block. Use div for the small styling wrappers inside it. See semantic elements and div and span.

How to open a link in a new tab in HTML?

Add target="_blank" to the <a> tag. It is good to add rel="noopener" too, so that the new page cannot control your page through window.opener.

<a href="https://www.example.com" target="_blank" rel="noopener">Open in new tab</a>

Use it only when it is really needed, for example for external websites. Many visitors prefer to decide themselves. See HTML Links.

What is the use of the label tag in HTML?

The <label> tag gives a name to a form field. Connect it with the field by writing the field’s id in the for attribute.

<label for="email">Email:</label>
<input type="email" id="email" name="email">
  • Clicking the label puts the cursor in the field, or ticks the checkbox. This makes small checkboxes easy to use on phones.
  • Screen readers read the label when the visitor enters the field.
  • A placeholder is not a replacement for a label.

You can also put the input inside the label and skip for. See HTML Forms.

What is an iframe in HTML and what is it used for?

An iframe (inline frame) shows another web page inside your page. It is used to embed YouTube videos, Google Maps, payment forms and widgets.

<iframe src="https://www.example.com" width="600" height="400" title="Example"></iframe>
  • Always give a title, width and height.
  • Some sites block embedding, and then the frame stays blank.
  • Use sandbox and loading="lazy" for safety and speed.

See HTML Iframes.

What is the viewport meta tag in HTML?

The viewport meta tag tells a mobile browser how to size the page. Without it, phones pretend the screen is about 980 pixels wide and show a tiny zoomed-out page.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • width=device-width uses the real width of the device.
  • initial-scale=1.0 starts with no zoom.

Add it in the head of every page. It is the first step of responsive design.

What are meta tags in HTML?

Meta tags give information (metadata) about the page to browsers and search engines. They go inside the <head> and are not shown on the page.

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Learn HTML step by step.">
<meta name="robots" content="noindex">

The description is often used by search engines under the page title. The old keywords meta tag is ignored by Google. See Meta Tag in HTML.

What is the difference between canvas and SVG in HTML5?

Canvas draws pixels using JavaScript. SVG describes shapes as XML elements.

PointCanvasSVG
TypeRaster (pixels)Vector (shapes)
Quality when zoomedBecomes blurryAlways sharp
Elements in the pageOne canvas element onlyEach shape is an element
CSS and events on shapesNoYes
Best forGames, charts with lots of data, image effectsIcons, logos, simple charts
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="orange" />
</svg>
Output in the browser

See HTML5 Canvas.

What are HTML entities?

HTML entities are special codes used to show characters that have a special meaning in HTML, or that are not on the keyboard. They start with & and end with ;.

CodeShows
&lt;<
&gt;>
&amp;&
&nbsp;A non-breaking space
&copy;©
&euro;€

Without &lt; the browser would read <p> as a real tag. See HTML Symbols.

How do you make a web page responsive using HTML and CSS?

Do these four things.

  • Add the viewport meta tag in the head.
  • Make images flexible with max-width: 100%; height: auto;.
  • Use relative units like % and rem, not fixed pixels.
  • Use media queries to change the layout on small screens.
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
  img { max-width: 100%; height: auto; }
  .column { width: 50%; float: left; }
  @media (max-width: 600px) {
    .column { width: 100%; float: none; }
  }
</style>

See HTML Responsive Design.

What is the difference between inline, internal and external CSS?

There are three ways to add CSS to HTML.

TypeWhereBest for
Inlinestyle attribute on one elementQuick tests, one-off change
Internal<style> tag in the headOne page only
ExternalSeparate .css file linked with <link>Whole websites. Best choice
<!-- inline -->
<p style="color:red;">Text</p>

<!-- internal -->
<style>
  p { color: red; }
</style>

<!-- external -->
<link rel="stylesheet" href="style.css">

External CSS is the best for real sites, because one file styles all the pages and the browser keeps it in the cache. See HTML Styles.

What are data attributes in HTML5?

Data attributes let you store your own extra information in any HTML element. The name must start with data-.

<button data-product-id="105" data-price="499">Add to cart</button>

<script>
  const btn = document.querySelector("button");
  console.log(btn.dataset.productId);   // 105
  console.log(btn.dataset.price);        // 499
</script>

In JavaScript they are read from element.dataset, and a name like data-product-id becomes productId. They are not shown on the page. See HTML Attributes.

What is the difference between localStorage, sessionStorage and cookies?

All three store data in the visitor’s browser, but they work differently.

PointlocalStoragesessionStorageCookies
LifetimeUntil deletedUntil the tab is closedUntil the expiry date
SizeAbout 5 MBAbout 5 MBAbout 4 KB
Sent to server with every requestNoNoYes
Use forSettings, theme, saved draftsData of one tab sessionLogin sessions, tracking
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));   // dark

sessionStorage.setItem("step", "2");

Never store passwords or other secrets in web storage, because any JavaScript on the page can read it. Web storage is a part of HTML5.

What is the difference between async and defer in the script tag?

A normal <script src="..."> stops the browser from reading the HTML until the script is downloaded and run. async and defer both download the script in the background, but they run it at different times.

Pointasyncdefer
DownloadIn the backgroundIn the background
RunsAs soon as downloaded, can interrupt the HTMLAfter the whole HTML is read
Order of scriptsNot guaranteedSame as in the page
Use forIndependent scripts, like analyticsScripts that need the page or each other
<script src="analytics.js" async></script>
<script src="app.js" defer></script>

For most of your own scripts, defer in the head is the best choice.

What is the difference between absolute and relative URL in HTML?

An absolute URL is the complete address with protocol and domain. A relative URL is the path from the page you are on.

<!-- absolute -->
<a href="https://www.example.com/contact.html">Contact</a>

<!-- relative -->
<a href="contact.html">Contact</a>
<a href="pages/about.html">About</a>
<a href="../index.html">Home</a>
<a href="/images/logo.png">Root relative</a>
  • Use absolute URLs for other websites.
  • Use relative URLs for your own pages, so the site works on any domain, even on your computer.
  • ../ goes one folder up. A path starting with / starts from the root of the site.

See HTML Links.

What is web accessibility and how does HTML help with it?

Web accessibility means that everyone can use your page, including people who are blind, deaf, cannot use a mouse, or have other disabilities. Good HTML does most of this work.

  • Use semantic tags (nav, main, button) so screen readers know what each part is.
  • Write the alt text for images.
  • Connect label with form fields.
  • Use headings in the right order (h1 to h6).
  • Add lang to the html tag.
  • Use ARIA attributes, like aria-label, only when plain HTML cannot say what you mean.
<button aria-label="Close menu">X</button>

The first rule of ARIA is: if a real HTML tag does the job, use it. Related: the alt attribute.