HTML Interview Questions
HTML Interview Questions
HTML consists of two components: the first is the content, and the second is the tag.
Together, they help display pages with the required information.
<head>
</head>
1. Menu list
2. Directory list
3. Ordered list
4. Unordered list
5. Definition list
10. How can we create links to different sections within the same web
page?
We can create several links to different sections within the same web page by using
the <a> tag along and using the # symbol.
Yes, we can keep list elements straight in the HTML file by using indents. The indent
can also be used for the sub-nested list for the parent list that contains it for creating
more lists and elements.
12. Which is the web page that is printed and pointed mostly as the
web address in the magazine?
The top-level page is the most popular web page that is printed and pointed mostly
as the web address in the magazine because it helps users browse all other pages
on the website from the same link.
15. Do you think that the hyperlink only applies to the text-only?
No, the hyperlink does not apply to the text-only but also to the images. This means
that we can convert an image into a link that can allow the users to gain access to
another page when clicked on it.
17. What are the two attributes of the number type in the middle of a
list?
The two attributes of the number type in the middle of a list include the type and
value. The type attribute is used to change the numbering type for any list item, while
the value attribute is used for changing the number index for any list item.
19. What are the different list types of the ordered list?
The different list types of the ordered list include roman numerals and alphabetical.
On the other hand, the unordered list attributes can be set through circle, square, or
disc.
1. Organizing checkboxes.
2. Applying particular names on the checkbox buttons to differentiate them easily
from a group.
3. Supporting the creation of a different group of checkboxes on a single
webpage with different names.
26. Can style sheets help align images and wrap text?
Yes, the style sheet can apply tables to position text and images to align them.
30. What are the similarities between the border and rule attributes?
If the border attribute is set to a non-zero value, then the default cell borders with the
thickness of one pixel will be added automatically. Similarly, if the rule attribute is
added to the <table> tag, then the border attribute will not be included, and the
default one-pixel border will be set and appear on the screen.
<!DOCTYPE html>
<html>
<body>
<p>Use the height and width attributes to specify the size of the iframe:</p>
<iframesrc="https://hackr.io/"height="400"width="600"></iframe>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</svg>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color: red;
h2{
color: blue;
h3{
color: green;
h4{
color: purple;
h5{
color: yellow;
h6{
color: orange;
</style>
</head>
<body>
</body>
<!DOCTYPE html>
<html>
<body>
<script>
var x = 5;
var y = 6;
var result = x + y;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<img src="tulip.jpeg">
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>😀</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text"id="fname"name="fname"><br>
<input type="text"id="lname"name="lname">
</form>
<p> </p>
<form>
<input type="radio"id="male"name="gender"value="male">
<label for="male">Male</label><br>
<input type="radio"id="female"name="gender"value="female">
<label for="female">Female</label><br>
<input type="radio"id="other"name="gender"value="other">
<label for="other">Other</label>
<br> </br>
<inputtype="submit"value="Submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<dl>
<dt>Coffee</dt>
<dt>Milk</dt>
</dl>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
No. HTML elements are defined by a starting tag, may contain some content and a
closing tag.For example, <h1>Heading 1</h1> is a HTML element but just <h1> is
a starting tag and </h1> is a closing tag.
Tags are the primary component of the HTML that defines how the content will be
structured/ formatted, whereas Attributes are used along with the HTML tags to
define the characteristics of the element. For example, <p align=” center”>Interview
questions</p>, in this the ‘align’ is the attribute using which we will align the
paragraph to show in the center of the view.
HTML elements which do not have closing tags or do not need to be closed are
Void elements. For Example <br />, <img />, <hr />, etc.
In HTML some characters are reserved like ‘<’, ‘>’, ‘/’, etc. To use these characters in
our webpage we need to use the character entities called HTML Entities. Below are
a few mapping between the reserved character and its respective entity character
to be used.
The class attribute is used to specify the class name for an HTML element. Multiple
elements in HTML can have the same class value. Also, it is mainly used to associate
the styles written in the stylesheet with the HTML elements.
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.
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.
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:
<section>: It is used inside the article block to define the basic structure of a
page.
<aside>: Sidebar content of the page.
To optimize website load time we need to optimize its asset loading and for that:
Strict Doctype
Transitional Doctype
Frameset Doctype
14. Please explain how to indicate the character set being used by a
document in HTML?
<!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.
<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.
Yes, we can display a web page inside another HTML web page. HTML provides a
tag <iframe> using which we can achieve this functionality.
Cell Spacing is the space or gap between two consecutive cells. Whereas, Cell
Padding is the space or gap between the text/ content of the cell and the edge/
border of the cell. Please refer to the above figure example to find the difference.
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.
Yes, it is possible using the “display” property with its value as “block”, to change
the inline element into a block-level element.
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.
2. block: using this, we can display any inline element as a block-level element.
3. 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.
4. flex: It displays the container and element as a flexible structure. It follows
flexbox property.
5. inline-flex: It displays the flex container as an inline element while its
content follows the flexbox properties.
6. grid: It displays the HTML elements as a grid container.
7. none: Using this property we can hide the HTML element.
Below are some of the display types which are rarely used:
1. table
2. inline-table
3. table-cell
4. table-column
5. table-row
6. inline-grid
7. list-item
8. inherit
9. initial
10. table-caption
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:
1. _self: This is a default value. It opens the document in the same window or
tab as it was clicked.
2. _blank: It opens the document in a new window or tab.
3. _parent: It opens the document in a parent frame.
4. _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>?
HTML provides a <script> tag using which we can run the javascript code and make
our HTML page more dynamic.
<!DOCTYPE html>
<html>
<body>
<h1>
<span>This is a demo for </span>
<u><span id="demo"></span></u>
</h1>
<script>
28. When to use scripts in the head and when to use scripts in the
body?
The HTML form is used to collect the user inputs. HTML provides a <form> tag to
create forms. To take input from the user we use the <input> tag inside the form
so that all collected user data can be sent to the server for processing. There are
different input types like ‘button’, ‘checkbox’, ‘number’, ‘text’, ‘password’, ‘submit’
etc.
<form action="/submit_data.php">
<label>Enter your name: </label>
<input type="text" name="name" />
<label>Enter Mobile number </label>
<input type="number" name="mobile_no"/>
<input type="submit" value="Submit">
</form>
HTML allows event trigger actions in browsers using javascript or JQuery. There are
a lot of events like ‘onclick’, ‘ondrag’, ‘onchange’, etc.
<!DOCTYPE html>
<html>
<body style="padding-top:50px">
<h3 id="event_demo">0</h3>
<input type="button" onclick="myFunction()" value="Click Me" />
<input type="reset" onclick="reset()" value="Reset" />
</body>
<script>
function myFunction() {
HTML5 provides two tags: <audio> and <video> tags using which we can add the
audio or video directly in the webpage.
Inline Block
Inline elements just take up the space
that is absolutely necessary for the Block elements start on a new line and
content and does not start from a new consume the full width of the page available.
line. Example:- <div>, <p>, <header>, <footer>,
Example:- <span>, <a>, <strong>, <h1>...<h6>, <form>, <table>, <canvas>,
<img>, <button>, <em>, <select>, <video>, <blockquote>, <pre>, <ul>, <ol>,
<abbr>, <label>, <sub>, <cite>, <figcaption>, <figure>, <hr>, <article>,
<abbr>, <script>, <label>, <i>, <section>, etc.
<input>, <output>, <q>, etc.
34. What is the difference between <figure> tag and <img> tag?
The <figure> tag specifies the self-contained content, like diagrams, images, code
snippets, etc. <figure> tag is used to semantically organize the contents of an
image like image, image caption, etc., whereas the <img> tag is used to embed the
picture in the HTML5 document.
To specify we can use <meta> tag which is a void tag,i.e., it does not have a closing
tag. Some of the attributes used with meta tags are name, content, http-equiv, etc.
The below image tells how to specify the metadata.
No. The <datalist> tag and <select> tag are different. In the case of <select> tag a
user will have to choose from a list of options, whereas <datalist> when used along
with the <input> tag provides a suggestion that the user selects one of the options
given or can enter some entirely different value.
Image Map lets a developer map/link different parts of images with the different
web pages. It can be achieved by the <map> tag in HTML5, using which we can link
images with clickable areas.
Semantic elements are those which describe the particular meaning to the browser
and the developer. Elements like <form>, <table>, <article>, <figure>, etc., are
semantic elements.
1, HTML, FrontEnd
2, CSS, FrontEnd
3, Python, BackEnd
40. What is the difference between <meter> tag and <progress> tag?
<progress> tag should be used when we want to show the completion progress of
a task, whereas if we just want a scalar measurement within a known range or
fraction value. Also, we can specify multiple extra attributes for <meter> tags like
‘form’, ‘low’, ‘high’, ‘min’, etc.
Yes, in HTML5 we can drag and drop an element. This can be achieved using the
drag and drop-related events to be used with the element which we want to drag
and drop.
SVG Canvas
It is Raster based i.e., composed
SVG is a vector based i.e., composed of shapes.
of pixels.
Canvas works better with a
SVG works better with a larger surface.
smaller surface.
Canvas can only be modified
SVG can be modified using CSS and scripts.
using scripts.
SVG is highly scalable. So we can print at high
It is less scalable.
quality with high resolution.
1. Mp3
2. WAV
3. Ogg
Introduction of new element tags to better structure the web page such as
<header> tag.
Forming a standard in cross-browser behavior and support for different
devices and platforms
Backward compatible with the older version HTML web pages
Introduction of basic interactive elements without the dependency of plugins
such as <video> tag instead of the flash plugin.
This web storage helps in storing some of the static data in the local storage of the
browser so that we do not need to fetch it from the server every time we need it.
There is a size limit based on different browsers. This helps in decreasing the load
time and a smooth user experience. There are two types of web storage that are
used to store data locally in HTML5:
Local Storage - This helps in storing data that will be retained even though
the user reopens the browser. It is stored for each webapp on different
browsers.
Session Storage - This is used for one session only. After the user closes the
browser this gets deleted.
It is used to help extract data for site crawlers and search engines. It is basically a
group of name-value pairs. The groups are called items, and each name-value pair
is a property. Most of the search engines like Google, Microsoft, Yandex, etc follow
schema.org vocabulary to extract this microdata.
The <output> tag is used for representing the result of a calculation. It has the
following attributes:
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.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<canvas width="300" height="100" style="border:2px solid;"></canvas>
</body>
</html>
SVG - Scalable Vector Graphics are used mostly for diagrams or icons. It
follows the XML format.
<!DOCTYPE html>
<html>
<body>
<svg width="400" height="110">
<rect width="300" height="100" style="fill:#FFF;stroke-
width:2;stroke:#000" />
</svg>
</body>
</html>
Both of the above examples produce this output and represent two different
approaches provided by HTML5 to implement graphical aspects in the webpage.
<form>
<div>
<label>Date:</label>
<input type="date" id="date" />
<br>
<label>Week:</label>
<input type="week" id="week" />
<br>
<label>Month:</label>
<input type="month" id="month" />
<br>
<label>Time:</label>
<input type="time" id="time" />
<br>
<label>Datetime:</label>
<input type="datetime" id="datetime" />
<br>
<label>Datetime Local:</label>
<input type="datetime-local" id="datetime-local" />
<br>
<label>Color:</label>
<input type="color" id="color"/>
<br>
<label>Email:</label>
<input type="email" id="email" placeholder="email address" />
<br>
<label>Number:</label>
<input type="number" id="number" />
<br>
<label>Search:</label>
<input type="search" id="search" />
<br>
<label>Phone:</label>
<input type="tel" id="phone" placeholder="Phone Number"
pattern="\d{10}$" />
<br>
<label>Range:</label>
<input type="range" id="range" />
<br>
<label>URL:</label>
<input type="url" id="url"/>
</div>
</form>
<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>
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>
<!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>
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.
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.
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.
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
To support old browsers instead of defining the resource of svg in src attribute of
<img> tag, it should be defined in srcset attribute and in src the fallback png file
should be defined.
Art direction - Using <picture> element the landscape image fully shown in
desktop layout can be zoomed in with the main subject in focus for a
portrait layout.
<picture>
<source media="(min-width: 650px)" srcset="img_cup.jpg">
<img src="img_marsh.jpg" style="width:auto;">
</picture>
Resolution switching - Instead of zoom and crop the images can be scaled
accordingly using vector graphics. Also, this can be further optimized to
serve different pixel density screens as well.
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
# 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>
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.
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.
These are used to create reusable custom elements which are very difficult in
traditional HTML. It consists of three technologies:
Custom elements - These are JavaScript APIs that help in defining custom
elements and their behavior.
Shadow DOM - These are JavaScript APIs that attach an encapsulated
shadow DOM tree to an element to keep the element’s features private and
unaffected by other parts.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>composed and composedPath demo</title>
<script src="main.js" defer></script>
</head>
<body>
<h1><code>composed</code> and <code>composedPath</code> demo</h1>
<open-shadow text="I have an open shadow root"></open-shadow>
<closed-shadow text="I have a closed shadow root"></closed-shadow>
</body>
</html>
customElements.define('open-shadow',
class extends HTMLElement {
constructor() {
super();
const pElem = document.createElement('p');
pElem.textContent = this.getAttribute('text');
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(pElem);
}
}
);
customElements.define('closed-shadow',
class extends HTMLElement {
constructor() {
super();
const pElem = document.createElement('p');
pElem.textContent = this.getAttribute('text');
const shadowRoot = this.attachShadow({mode: 'closed'});
shadowRoot.appendChild(pElem);
}
}
);
document.querySelector('html').addEventListener('click', e => {
console.log(e.composed);
console.log(e.composedPath());
});
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);