HTML Interview
HTML Interview
8. What is the difference between the ‘id’ attribute and the ‘class’ attribute of
HTML elements?
Multiple elements in HTML can have the same class value, whereas a value of id
attribute of one element cannot be associated with another HTML element.
9. Define multipart form data?
Multipart form data is one of the values of the enctype attribute. It is used to send the
file data to the server-side for processing. The other valid values of the enctype attribute
are text/plain and application/x-www-form-urlencoded.
10. Describe HTML layout structure.
Every web page has different components to display the intended content and a specific
UI. But still, there are few things which are templated and are globally accepted way to
structure the web page, such as:
• <header>: Stores the starting information about the web page.
• <footer>: Represents the last section of the page.
• <nav>: The navigation menu of the HTML page.
• <article>: It is a set of information.
• <section>: It is used inside the article block to define the basic structure of a
page.
• <aside>: Sidebar content of the page.
14. Please explain how to indicate the character set being used by a document in
HTML?
The character set is defined in <meta> tag inside <head> element.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
...
...
</head>
...
</html>
15. What is the difference between <strong>, <b> tags and <em>, <i> tags?
The effect on a normal webpage of the tags <strong>, <b> and <em>, <i> is the same.
<b> and <i> tags stands for bold and italic. These two tags only apply font styling and
bold tag <b>, just adds more ink to the text, these tags don't say anything about the text.
Whereas, <strong> and <em> tags represent that the span of text is of strong
importance or more importance and emphatic stress respectively than the rest of the
text. These tags have semantic meaning.
16. What is the significance of <head> and <body> tag in HTML?
<head> tag provides the information about the document. It should always be enclosed
in the <html> tag. This tag contains the metadata about the webpage and the tags
which are enclosed by head tag like <link>, <meta>, <style>, <script>, etc. are not
displayed on the web page. Also, there can be only 1 <head> tag in the entire Html
document and will always be before the <body> tag.
<body> tag defines the body of the HTML document. It should always be enclosed in
the <html> tag. All the contents which needs to be displayed on the web page like
images, text, audio, video, contents, using elements like <p>, <img>, <audio>,
<heading>, <video>, <div>, etc. will always be enclosed by the <body> tag. Also, there
can be only 1 body element in an HTML document and will always be after the <head>
tag.
17. Can we display a web page inside a web page or Is nesting of webpages
possible?
Yes, we can display a web page inside another HTML web page. HTML provides a tag
<iframe> using which we can achieve this functionality.
<iframe src=”url of the web page to embed” />
19. How can we club two or more rows or columns into a single row or column in
an HTML table?
HTML provides two table attributes “rowspan” and “colspan” to make a cell span to
multiple rows and columns respectively.
21. In how many ways can we position an HTML element? Or what are the
permissible values of the position attribute?
There are mainly 7 values of position attribute that can be used to position an HTML
element:
1. static: Default value. Here the element is positioned according to the normal flow
of the document.
2. absolute: Here the element is positioned relative to its parent element. The final
position is determined by the values of left, right, top, bottom.
3. fixed: This is similar to absolute except here the elements are positioned relative
to the <html> element.
4. relative: Here the element is positioned according to the normal flow of the
document and positioned relative to its original/ normal position.
5. initial: This resets the property to its default value.
6. inherit: Here the element inherits or takes the property of its parent.
22. In how many ways you can display HTML elements?
7. inline: Using this we can display any block-level element as an inline element.
The height and width attribute values of the element will not affect.
8. block: using this, we can display any inline element as a block-level element.
9. inline-block: This property is similar to inline, except by using the display as
inline-block, we can actually format the element using height and width values.
10. flex: It displays the container and element as a flexible structure. It follows
flexbox property.
11. inline-flex: It displays the flex container as an inline element while its content
follows the flexbox properties.
12. grid: It displays the HTML elements as a grid container.
13. none: Using this property we can hide the HTML element.
Below are some of the display types which are rarely used:
14. table
15. inline-table
16. table-cell
17. table-column
18. table-row
19. inline-grid
20. list-item
21. inherit
22. initial
23. table-caption
23. What is the difference between “display: none” and “visibility: hidden”, when
used as attributes to the HTML element.
When we use the attribute “visibility: hidden” for an HTML element then that element will
be hidden from the webpage but still takes up space. Whereas, if we use the “display:
none” attribute for an HTML element then the element will be hidden, and also it won’t
take up any space on the webpage.
24. How to specify the link in HTML and explain the target attribute?
HTML provides a hyperlink - <a> tag to specify the links in a webpage. The ‘href’
attribute is used to specify the link and the ‘target’ attribute is used to specify, where do
we want to open the linked document. The ‘target’ attribute can have the following
values:
24. _self: This is a default value. It opens the document in the same window or tab
as it was clicked.
25. _blank: It opens the document in a new window or tab.
26. _parent: It opens the document in a parent frame.
27. _top: It opens the document in a full-body window.
25. In how many ways can we specify the CSS styles for the HTML element?
There are three ways in which we can specify the styles for HTML elements:
• Inline: Here we use the ‘style’ attribute inside the HTML element.
• Internal: Here we use the <style> tag inside the <head> tag. To apply the style
we bind the elements using ‘id’ or ‘class’ attributes.
• External: Here we use the <link> tag inside <head> tag to reference the CSS file
into our HTML code. Again the binding between elements and styles is done
using ‘id’ or ‘class’ attributes.
26. Difference between link tag <link> and anchor tag <a>?
The anchor tag <a> is used to create a hyperlink to another webpage or to a certain part
of the webpage and these links are clickable, whereas, link tag <link> defines a link
between a document and an external resource and these are not clickable.
28. When to use scripts in the head and when to use scripts in the body?
If the scripts contain some event-triggered functions or jquery library then we should use
them in the head section. If the script writes the content on the page or is not inside a
function then it should be placed inside the body section at the bottom. In short, follow
below three points:
28. Place library scripts or event scripts in the head section.
29. Place normal scripts that do not write anything on the page, in the head section
until there is any performance issue.
30. Place scripts that render something on the web page at the bottom of the body
section.
<script>
function myFunction() {
var value = document.getElementById("event_demo").innerHTML
value = parseInt(value) + 1;
document.getElementById("event_demo").innerHTML = value;
}
function reset() {
document.getElementById("event_demo").innerHTML = 0;
}
</script>
</html>
HTML5 Interview Questions
31. What are some of the advantages of HTML5 over its previous versions?
Some advantages of HTML5 are:-
• It has Multimedia Support.
• It has the capabilities to store offline data using SQL databases and application
cache.
• Javascript can be run in the background.
• HTML5 also allows users to draw various shapes like rectangles, circles,
triangles, etc.
• Included new Semantic tags and form control tags.
32. How can we include audio or video in a webpage?
HTML5 provides two tags: <audio> and <video> tags using which we can add the audio
or video directly in the webpage.
47. Which tag is used for representing the result of a calculation? Explain its
attributes.
The <output> tag is used for representing the result of a calculation. It has the following
attributes:
• for - It defines the relationship between the elements used in calculation and
result.
• form - This is used to define the form the output element belongs to.
• name - The name of the output element.
<form oninput = "result.value=parseInt(n1.value)+parseInt(n2.value)">
<input type = "number" name = "n1" value = "1" /> +
<input type = "number" name = "n2" value = "2" /><br />
The output is: <output name = "result"></output>
</form>
The above example looks like
48. What is new about the relationship between the <header> and <h1> tags in
HTML5?
As HTML5 was all about better semantics and arrangements of the tags and elements,
the <header> tag specifies the header section of the webpage. Unlike in previous
version there was one <h1> element for the entire webpage, now this is the header for
one section such as <article> or <section>. According to the HTML5 specification, each
<header> element must at least have one <h1> tag.
</form>
51. What are the New tags in Media Elements in HTML5?
• <audio> - Used for sounds, audio streams, or music, embed audio content
without any additional plug-in.
• <video> - Used for video streams, embed video content etc.
• <source> - Used for multiple media resources in media elements, such as audio,
video, etc.
• <embed> - Used for an external application or embedded content.
• <track> - Used for subtitles in the media elements such as video or audio.
<label>
Video:
</label>
<video width="320" height="240" controls>
<source src="video.mp4" type="video/mp4">
<track src="subtitles.vtt" kind="subtitles" srclang="en"
label="English">
</video>
<br>
<label>
Embed:
</label>
<embed type="video/webm"
src="https://www.youtube.com/embed/MpoE6s2psCw" width="400"
height="300">
<br>
<label>
Audio:
</label>
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
</audio>
52. Why do you think the addition of drag-and-drop functionality in HTML5 is
important? How will you make an image draggable in HTML5?
The drag and drop functionality is a very intuitive way to select local files. This is similar
to what most of the OS have copy functionality thus making it very easy for the user to
comprehend. Before the native drag and drop API, this was achievable by writing
complex Javascript programming or external frameworks like jQuery.
To enable this functionality there is a draggable attribute in the <img> tag and need to
set ondrop and ondragover attribute to an eventhandler available in scripts.
<!DOCTYPE HTML>
<html>
<head>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drop(ev) {
...
}
</script>
</head>
<body>
...
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"
style="border: 1px solid #aaaaaa; width:350px; height: 70px;"></div>
<br>
<img id="drag1" src="img_logo.gif" draggable="true" width="336"
height="69">
...
</body>
</html>
53. Why do we need the MathML element in HTML5?
MathML stands for Mathematical Markup Language. It is used for displaying
mathematical expressions on web pages. For this <math> tag is used.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<math>
<mrow>
<mrow>
<msup>
<mi> a </mi>
<mn> 2 </mn>
</msup>
<mo> + </mo>
<msup>
<mi> b </mi>
<mn> 2 </mn>
</msup>
<mo> + </mo>
<mn> 2 </mn>
<mn> a </mn>
<mn> b </mn>
</mrow>
<mo> = </mo>
<mn> 0 </mn>
</mrow>
</math>
</body>
</html>
This displays the equation a2 + b2 + 2ab = 0.
54. What are the server-sent events in HTML5?
The events pushed from the webserver to the browsers are called server-sent events.
DOM elements can be continuously updated using these events. This has a major
advantage over straight-up polling. In polling, there is a lot of overhead since every time
it is establishing an HTTP connection and tearing it down whereas, in server-sent
events, there is one long-lived HTTP connection. To use a server-sent event,
<eventsource> element is used. The src attribute of this element specifies the URL from
which sends a data stream having the events.
<eventsource src = "/cgi-bin/myfile.cgi" />
55. What are Web Workers?
These are added to bring parallelism and async capability. It runs in the background to
do the computationally expensive tasks without yielding to make the page responsive. It
is achieved by starting a separate thread for such tasks. These are not meant to
perform UI operations. There are three types of web workers:
• Dedicated Workers - These are workers that are utilized by a single script.
• Shared Workers -These are workers that are utilized by multiple scripts running
in different windows, IFrames, etc.
• Service Workers - These act as proxy servers between web applications, the
browser, and the network. Mostly used for push notifications and sync APIs.
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
}
}
function stopWorker() {
w.terminate();
w = undefined;
}
</script>
56. What is the usage of a novalidate attribute for the form tag that is introduced
in HTML5?
Its value is a boolean type that indicates whether or not the data being submitted by the
form will be validated beforehand. By making this false, forms can be submitted without
validation which helps users to resume later also.
<form action = "" method = "get" novalidate>
Name:<br><input type="name" name="sname"><br>
Doubt:<br><input type="number" name="doubt"><br>
<input type="submit" value="Submit">
</form>
57. What are raster images and vector images?
Raster Images - The raster image is defined by the arrangement of pixels in a grid with
exactly what color the pixel should be. Few raster file formats include PNG(.png),
JPEG(.jpg), etc.
Vector Images - The vector image is defined using algorithms with shape and path
definitions that can be used to render the image on-screen written in a similar markup
fashion. The file extension is .svg
</svg>
60. What is a manifest file in HTML5?
The manifest file is used to list down resources that can be cached. Browsers use this
information to make the web page load faster than the first time. There are 3 sections in
the manifest file
• CACHE Manifest - Files needs to be cached
• Network - File never to be cached, always need a network connection.
• Fallback - Fallback files in case a page is inaccessible
CACHE MANIFEST
# 2012-06-16 v1.0.0
/style.css
/logo.gif
/main.js
NETWORK:
login.php
FALLBACK:
/html/ /offline.html<!DOCTYPE HTML>
<html manifest="tutorial.appcache">
...
...
</html>
61. What is the Geolocation API in HTML5?
Geolocation API is used to share the physical location of the client with websites. This
helps in serving locale-based content and a unique experience to the user, based on
their location. This works with a new property of the global navigator object and most of
the modern browsers support this.
var geolocation = navigator.geolocation;
62. Write HTML5 code to demonstrate the use of Geolocation API.
<!DOCTYPE html>
<html>
<body>
<p>Click "try it" button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation functionality is not supported
by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
The above example asks for user permission for accessing the location data via
geolocation API and after clicking the button the coordinates of the physical location of
the client get displayed.
});
Here 2 custom elements are defined <open-shadow> and <closed-shadow> which
takes their text content and inserts them into a shadow DOM as content of a <p>
element.
• HTML templates - The markup templates are written using <template> and
<slot> elements which can be reused multiple times as the basis of a custom
element's structure.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple template</title>
<script src="main.js"></script>
</head>
<body>
<h1>Simple template</h1>
<template id="my-paragraph">
<style>
p {
color: white;
background-color: #666;
padding: 5px;
}
</style>
<p><slot name="my-text">My default text</slot></p>
</template>
<my-paragraph>
<span slot="my-text">Let's have some different text!</span>
</my-paragraph>
<my-paragraph>
<ul slot="my-text">
<li>Let's have some different text!</li>
<li>In a list!</li>
</ul>
</my-paragraph>
</body>
</html>customElements.define('my-paragraph',
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById('my-paragraph');
const templateContent = template.content;
this.attachShadow({mode: 'open'}).appendChild(
templateContent.cloneNode(true)
);
}
}
);
const slottedSpan = document.querySelector('my-paragraph span');
console.log(slottedSpan.assignedSlot);
console.log(slottedSpan.slot);