Top 100 HTML Interview Questions and Answers 2023
Top 100 HTML Interview Questions and Answers 2023
2023
1. What is HTML and what is its role in web development?
3. In how many ways can we specify the CSS styles for the HTML
element?
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.
4. How do you create an accordion menu in HTML?
HTML:
<div class="accordion">
<div class="accordion-item">
<div class="accordion-content">
</div>
</div>
<div class="accordion-item">
<div class="accordion-content">
<p>Content for Section 2 goes here.</p>
</div>
</div>
</div>
CSS:
.accordion-header {
cursor: pointer;
.accordion-content {
display: none;
.accordion-item.active .accordion-content {
display: block;
}
JavaScript (jQuery):
$(document).ready(function() {
$('.accordion-header').click(function() {
$(this).parent('.accordion-item').toggleClass('active');
});
});
5. Explain the purpose of DOCTYPE in HTML.
The <meta> tag is a self-closing tag in HTML, meaning it does not require
a closing tag. It is used to specify metadata or additional information
about the HTML document. The <meta> tag supports various attributes
such as name, content, HTTP-equiv, etc. These attributes are used to
provide details such as the document’s character encoding, viewport
settings, author, description, keywords, and more.
It is used to group and style larger sections of HTML content. It is used to style smalle
It creates a line break before and after the element. It does not create line br
Some common HTML tags include <h1> to <h6> for headings, <p> for
paragraphs, <a> for links, <img> for images, and <div> and <span> for
grouping and styling elements.
The <div> tag is a block-level element used for grouping and styling
larger sections of content, while the <span> tag is an inline element used
for styling smaller sections or individual elements.
14. What is the purpose of the <img> tag and how is it used?
<strong> <b>
It carries semantic meaning, indicating strong importance. It does not carry any sema
Search engines may give more weight to the text inside it. It does not affect the weig
It should be used when the emphasis has importance. It should be used when th
17. What is the purpose of the <a> tag and its attributes?
The <a> tag, also known as the anchor tag, is used to create hyperlinks
in HTML. Its “href” attribute specifies the URL or location to link to, and it
can also have attributes like “target” for specifying the link behavior.
An ordered list in HTML can be created using the <ol> tag. Each list item
is defined with the <li> tag, and the numbering is automatically
generated by the browser.
Nesting tags in HTML means placing one HTML element inside another.
This hierarchical structure allows for organizing and structuring content
effectively.
A table in HTML can be created using the <table> tag. The table rows are
defined with the <tr> tag, and the cells within each row are defined with
the <td> tag (or <th> for table headers).
They only take up as much width as necessary for content. They take up the full availa
They cannot contain block-level elements within them. They can contain both inlin
Examples: <span>, <a>, <strong>, <em>, <img>. Examples: <div>, <p>, <h1
Semantic HTML elements are tags that provide meaning and context to
the content they enclose. Examples include <header> for page headers,
<nav> for navigation menus, <section> for sections of content, and
<footer> for page footers.
To embed an audio file in HTML, the <audio> tag is used. It requires the
“src” attribute to specify the audio source (URL or file path) and supports
additional attributes for controls, autoplay, etc.
25. Difference between the id and class attributes
id class
It must be unique within the HTML document. It can be used multiple times w
It is generally used when referring to a specific element. It is commonly used for stylin
26. Explain the purpose of the <form> tag and its attributes.
The <form> tag is used to create an HTML form that allows users to input
data. It includes input fields, buttons, checkboxes, etc. The “action”
attribute specifies the URL where the form data is submitted, and the
“method” attribute specifies the HTTP method used for submission (GET
or POST).
<ol> <ul>
Each list item is automatically numbered incrementally. Each list item is represen
<ol> <ul>
29. What is the purpose of the <input> tag and its different
types?
The <input> tag is used to create input fields within HTML forms. Its
“type” attribute specifies the type of input, such as text, password,
checkbox, radio button, email, etc.
A drop-down menu can be created in HTML using the <select> tag for the
dropdown container and <option> tags for each selectable item within the
dropdown.
It is used for normal text input fields. It is used for password input
The entered text is visible and readable. The entered text is hidden an
To embed a YouTube video in an HTML page, you can use the YouTube
embed code provided by YouTube. It involves using the <iframe> tag
with the source URL of the YouTube video.
35. What are HTML data attributes and how are they used?
HTML data attributes allow you to store extra information within an HTML
element. They start with “data-” followed by a custom attribute name and
can be accessed using JavaScript or CSS for dynamic functionality or
styling purposes.
<em> <i>
It carries meaning and should be used for emphasis. It does not carry any specific m
A navigation bar in HTML can be created using the <nav> tag to wrap the
navigation elements, such as links or a list of navigation items. CSS is
used to style and position the navigation bar.
The <canvas> tag in HTML5 provides a drawing surface that allows you to
render graphics, animations, or interactive elements using JavaScript. It
is often used for creating dynamic visuals.
<head> <body>
It contains metadata and information about the HTML document. It contains the visibl
<head> <body>
To include an external CSS stylesheet in HTML, you use the <link> tag
within the <head> section of the HTML document. The “href” attribute is
used to specify the path to the CSS file.
<label> <placeholder>
It is used to associate a text label with a form element. It is used to provide a temporary h
It improves accessibility and usability for form elements. It is typically used for text inputs,
It is visible even after the user inputs a value. It disappears when the user starts t
Here’s an example:
HTML:
<header id="sticky-header">
</header>
<main>
</main>
CSS:
#sticky-header {
position: sticky;
top: 0;
z-index: 100;
background-color: #f1f1f1;
JavaScript:
window.addEventListener('scroll', function() {
var header = document.getElementById('sticky-header');
if (window.pageYOffset > 0) {
header.classList.add('sticky');
} else {
header.classList.remove('sticky');
});
Comments in HTML are added using the <!– comment –> syntax. They
are used to add notes or explanations that are not displayed on the
rendered webpage.
<iframe> <object>
It is used to embed another HTML document within the current document. It is used to embed multim
To create a tooltip in HTML, you can use the “title” attribute on elements
like links or other interactive elements. When the user hovers over the
element, the tooltip text is displayed.
The tab index attribute in HTML is used to specify the tabbing order or
sequence of focusable elements on a webpage. It allows users to navigate
through interactive elements using the keyboard’s tab key.
The <video> tag in HTML5 is used to embed videos or audio clips within
an HTML document. It supports various attributes for controlling
playback, such as source, controls, autoplay, and more.
A responsive CSS grid layout can be created using CSS grid properties
and media queries. By defining grid areas, columns, and rows, along with
responsive breakpoints, the layout can adapt to different screen sizes.
57. Explain the concept of HTML entities and when to use them.
To define a line break in HTML, you can use the <br> tag. It does not
require a closing tag. For example: <br>.
The syntax for creating an unordered list in HTML is using the <ul> tag,
and each list item is defined with the <li> tag.
For example:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
To add an image in HTML, you can use the <img> tag with the src attribute
specifying the image file path.
For example: <img src="image.jpg" alt="Description">.
The syntax for creating a table in HTML involves using the <table> tag to
define the table, <tr> for table rows, <th> for table headers, and <td> for
table cells. Here’s an example:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
In HTML, you can define a comment using the <!-- --> syntax. Anything
placed between these tags will be treated as a comment and will not be
rendered on the webpage.
<label for="name">Name:</label>
</form><?pre>
HTML provides six levels of headings, ranging from <h1> (the highest
level) to <h6> (the lowest level).
68. What is the syntax for including an external CSS file in HTML?
To include an external CSS file in HTML, you use the <link> tag within
the <head> section of your HTML document. The href attribute specifies the
path to the CSS file. Here’s an example:
<head>
</head>
To create a progress bar, you can use the <progress> tag in HTML and
style it using CSS. Set the value attribute of the <progress> tag to
specify the progress percentage, and use CSS to customize the
appearance of the progress bar.
To embed a Google Map in an HTML page, you can use the Google Maps
Embed API. Go to the Google Maps website, search for the desired
location, click the “Share” button, and select the “Embed a map” tab.
Customize the map size and other options, copy the generated HTML
code, and paste it into your HTML page.
HTML validation is the process of checking if HTML code complies with the
HTML standards and rules. It ensures that the HTML structure is correct,
tags are used appropriately, and syntax errors are minimized. Validating
HTML code helps improve code quality, ensures cross-browser
compatibility, and assists with accessibility.
HTML5 drag and drop is a feature that allows users to interact with web
content by dragging elements from one location and dropping them onto
another. It enables intuitive user interfaces, such as dragging files to
upload, rearranging elements, or implementing complex interactions
using JavaScript event handlers.
85. How do you create a responsive navigation menu in HTML and
CSS?
87. How do you create a parallax scrolling effect in HTML and CSS?
91. How do you create a flip card effect in HTML and CSS?
97. How do you create a responsive login form in HTML and CSS
To create a responsive login form, structure the form using HTML <form>
and <input> elements. Apply CSS styles for layout, appearance, and
responsiveness. Use media queries to adjust the form’s behavior and
visibility for different screen sizes, such as repositioning or resizing the
form elements to fit smaller screens.