HTML Interview Questions and Answer
HTML Interview Questions and Answer
Basic Questions
1. What is HTML?
Answer: HTML stands for Hyper Text Markup Language. It is the standard language for creating web
pages and web applications, describing the structure of a web page.
Answer: HTML Elements are the building blocks of HTML that describe the structure and content of a
web page. They generally consist of a start tag, content, and end tag.
Answer: HTML5 is the latest version of HTML, introducing new elements (e.g., <header>, <footer>,
<article>, <section>), attributes, and APIs (e.g., local storage, geolocation).
Answer: The HTML head element consists of HTML elements that define the metadata of an HTML
document and the HTML body element consists of the content that should be displayed on the web
page.
Answer: DOCTYPE is a declaration at the beginning of an HTML document specifying the HTML version.
It helps browsers render the page correctly. For HTML5, it is <!DOCTYPE html>.
Answer: Semantic HTML elements clearly describe their meaning in a human- and machine-readable
way. Examples include <article>, <section>, <aside>, and <footer>.
Answer: Block-level elements (e.g., <div>, <p>) start on a new line and take up the full width available.
Inline elements (e.g., <span>, <a>) do not start on a new line and only take up as much width as
necessary.
Answer: Attributes provide additional information about HTML elements, specified within the opening
tag. Examples include href in <a>, src in <img>, and class in most tags.
Answer: <meta> tags provide metadata about the HTML document, such as character set, author,
description, and viewport settings for responsive design.
10. What is nested HTML?
Answer: HTML elements can be nested. It means that the HTML element can be placed inside another
HTML element.
Answer: The <form> tag is used to create an HTML form for user input, containing elements like
<input>, <textarea>, <button>, and <select>.
Answer: We use the HTML anchor elements to navigate to other web resources or a specific HTML
element within the HTML document. They are also called as Hyperlinks/ Links.
13. What are the differences between HTML div and span elements?
Answer: The HTML div element is a block-level element. It can be used to wrap around other HTML
elements and apply CSS styles to many elements at once. The HTML span element is an inline element. It
can be used to wrap a small portion of text, etc and add CSS styles to it.
Answer: The action attribute specifies the URL where the form data should be sent when the form is
submitted.
Answer: The HTML container element (div) defines a container. It can be used to wrap a group of
elements and apply CSS styles to many elements at once.
Answer: A hyperlink is created using the <a> tag with the href attribute specifying the URL. Example:
<a href="https://example.com">Link Text</a>.
Answer: id is a unique identifier for an element, used for styling and scripting, while class is a way to
group elements for styling.
Answer: The alt attribute provides alternative text for an image if it cannot be displayed, improving
accessibility and SEO.
Answer: The HTML script element can be used to embed the JavaScript code. It can contain scripting
statements, or it points to an external script file through the src attribute.
20. How to add line breaks to an HTML paragraph element?
Answer: The HTML br element is used to add a line break to an HTML paragraph element. It breaks the
text and continue it in the next line. It is useful in writing poems, addresses, etc.
Answer: We can display images on the web page using the HTML img element. Generally, the HTML img
element requires two main HTML attributes. src specifies the path to the image. alt specifies an
alternate text for the image.
22. What is the difference between HTML anchor element and HTML link element?
Answer: HTML anchor element: We use the HTML anchor element to navigate to other web resources
or a specific element within the HTML document.
HTML link element:We use the HTML link element to link the HTML and CSS files.
Answer: HTML Horizontal Rule Element: The HTML hr element inserts a horizontal line and helps to
separate the content.
Answer: The HTML href attribute is the most important attribute of the HTML anchor element. It
specifies the URL/path of the page the link goes to
Answer: No. There shouldn't be more than one HTML element with the same id. The id must be unique
within the HTML document.
Advanced Questions
Answer: The data-* attributes are used to store custom data private to the page or application.
Example: <div data-user-id="12345">.
Answer: Ordered lists are created with <ol> and <li> for list items, and unordered lists with <ul> and
<li>.
Answer: <iframe> embeds another HTML page within the current page, while <embed> is used for
embedding external content like multimedia (e.g., videos).
Answer: Self-closing tags do not have a closing tag and end with />. Examples include <img />,
<input />, <br />, <hr />.
Answer: <canvas> is used to draw graphics on the fly via JavaScript, allowing for dynamic, scriptable
rendering of 2D shapes and images.
Answer: The viewport meta tag controls the layout on mobile browsers. Example: <meta
name="viewport" content="width=device-width, initial-scale=1.0">.
Answer: The HTML style element contains style information for an HTML document, or part of an HTML
document. It contains CSS, which is applied to the contents of the HTML document.
Answer: Custom data attributes (data-*) allow embedding custom data attributes on all HTML
elements. They are used to store extra information that doesn’t have any visual representation.
Answer: The srcset attribute provides a set of images with different resolutions, allowing the browser
to choose the best image for the current viewport and display density.
10. How do you create a responsive image in HTML?
Answer: Use the srcset attribute and the <picture> element with different <source> tags for
different screen sizes.
11. What is the purpose of the rel attribute in the <link> tag?
Answer: The rel attribute specifies the relationship between the current document and the linked
resource. Common values are stylesheet and icon.
Answer: HTML comments are created using <!-- Comment text -->.
Answer: The lang attribute specifies the language of the document, which helps search engines and
browsers. Example: <html lang="en">.
Answer: Microdata is a way to embed metadata within existing content on web pages, using the
itemscope, itemtype, and itemprop attributes.
Answer: The draggable attribute specifies whether an element is draggable. Example: <div
draggable="true">Drag me!</div>.
17. What is the difference between the nav and aside tags?
Answer: The <nav> tag defines a section for navigation links, while the <aside> tag defines content
aside from the main content (e.g., sidebars).
Answer: Multi-column layouts can be defined using CSS3, specifically the column-count and column-
gap properties.
Answer: The HTML video element represents a video. It is used to play audio files with captions, videos or
movies.
Answer: The HTML controls attribute is used to set controls in HTML video element. It is a boolean
attribute. When this attribute is present, it specifies that video controls should be displayed. Video
controls include: Play, Pause, Seeking, Volume, Fullscreen toggle, Captions/Subtitles (when available),
Track (when available)
Answer: The HTML picture element is a container to provide multiple alternative sources for the HTML
img element. It is useful in different display/device scenarios. It can contain zero or more source
elements and one HTML img element.
Answer: The HTML non-semantic elements don't have any meaning. Examples: div and span. We
cannot exactly find out the type of content within the given element unlike semantic elements like h1, p,
etc.
Answer: Global attributes are attributes common to all HTML elements. They can be used on all
elements, though they may not affect some elements. Examples: class, id, title, etc.
Answer: Selected Attribute is a boolean attribute. It specifies that an option should be pre-selected
when the page loads. Multiple Attribute is a boolean attribute. It specifies whether the user is allowed
to provide more than one value in an input field. It can be used in the HTML select element.
Answer: The HTML required attribute is a boolean attribute. If present, it specifies that an input field
must be filled out before submitting the form.
Example:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required />
<input type="submit" />
</form>