Top HTML Interview Questions & Answers
Top HTML Interview Questions & Answers Collection
Here, you can find Frequently Asked HTML Interview Questions & Answers for jobs. These are top Interview Questions related to HTML basic tags, images, links, table, forms and other elements.
Tutorials Class covers Interview Questions for Freshers as well as experienced professionals.
What is HTML?
HTML (HyperText Markup Language) is the standard language for creating web pages. It uses tags to tell the browser what each part of the content is: a heading, a paragraph, an image, a link and so on.
<h1>Hello, World!</h1>
<p>This is a paragraph.</p>
Hello, World!
This is a paragraph.
HTML gives the structure, CSS gives the style and JavaScript gives the behaviour. HTML is a markup language, not a programming language. Learn more in HTML Introduction.
What is the difference between HTML tags and elements?
A tag is only the label written in angle brackets, like <p> and </p>. An element is the complete thing: the opening tag, the content and the closing tag together.
| Part | Example |
|---|---|
| Opening tag | <p> |
| Content | Hello |
| Closing tag | </p> |
| Element | <p>Hello</p> |
Empty elements like <br> have only one tag. In daily talk people use both words for the same thing. See HTML Elements and Tags.
What is the HTML page structure? Explain with example
Every HTML page has the same basic structure: a DOCTYPE, then the html element, which has a head (information about the page) and a body (the visible content).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Page</title>
</head>
<body>
<h1>Hello</h1>
<p>Page content.</p>
</body>
</html>
<!DOCTYPE html>tells the browser it is HTML5.<head>has the title, charset, styles and scripts. It is not shown on the page.<body>has everything the visitor sees.
More in HTML Page Structure.
What is an attribute in HTML?
An attribute gives extra information about an HTML element. It is written inside the opening tag as name="value".
<a href="https://example.com" target="_blank">Visit</a>
<img src="photo.jpg" alt="A photo" width="200">
Here href, target, src, alt and width are attributes. Some common ones are id, class, style and title. Boolean attributes like disabled and required have no value. See HTML Attributes.
What is the difference between DIV, SPAN and paragraph tag in HTML?
All three are used to hold content, but they are different.
| Tag | Type | Use |
|---|---|---|
<div> | Block | Generic container to group sections and for layout |
<span> | Inline | Generic container to style a small part of text |
<p> | Block | A paragraph of text, with a meaning |
<div class="card">
<p>My favourite colour is <span style="color:red">red</span>.</p>
</div>
My favourite colour is red.
div and span have no meaning of their own, while p tells that the text is a paragraph. See HTML Div and Span.
How to create a link to jump to a specific part of a page in HTML?
Give the target element an id, and put #id in the link. This works for the same page and for another page.
<!-- same page -->
<a href="#pricing">See pricing</a>
<h2 id="pricing">Pricing</h2>
<!-- another page -->
<a href="plans.html#pricing">Pricing on the plans page</a>
For a smooth scroll instead of a sudden jump, add this CSS.
<style>
html { scroll-behavior: smooth; }
</style>
See also links to sections within the same page and HTML class and id.
What are different tags to list items in HTML?
HTML has three kinds of lists.
| Tag | List type | Item tag |
|---|---|---|
<ul> | Unordered list (bullets) | <li> |
<ol> | Ordered list (numbers) | <li> |
<dl> | Description list | <dt> term and <dd> description |
<ul>
<li>Apple</li>
<li>Mango</li>
</ul>
<ol>
<li>Step one</li>
<li>Step two</li>
</ol>
<dl>
<dt>HTML</dt>
<dd>Structure of a page</dd>
</dl>
- Apple
- Mango
- Step one
- Step two
- HTML
- Structure of a page
Lists can be nested. See HTML Lists.
How do you insert an image into HTML?
Use the <img> tag. It is an empty element, so it has no closing tag. Two attributes are needed: src (the path of the image) and alt (a short text description).
<img src="images/photo.jpg" alt="A boy playing cricket" width="300" height="200">
srccan be a relative path likeimages/photo.jpgor a full URL.altis shown when the image cannot load, and read by screen readers.widthandheightset the size in pixels.
See HTML Images.
What is the difference between inline and block elements in HTML?
A block element starts on a new line and takes the full width. An inline element stays in the same line and takes only as much width as its content.
| Point | Block | Inline |
|---|---|---|
| New line | Yes | No |
| Width | Full width | Only the content |
| Examples | div, p, h1, ul, table | span, a, strong, img |
<p>Block one</p>
<p>Block two</p>
<span>Inline one</span> <span>Inline two</span>
Block one
Block two
Inline one Inline two
How do you insert a video in HTML?
Use the <video> tag for a video file, with the controls attribute to show the play button.
<video width="480" controls>
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
For a YouTube video, use the embed code in an <iframe>.
<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" title="Video" allowfullscreen></iframe>
See HTML5 Video and HTML Iframes.
What are void (empty) elements in HTML?
Void elements (also called empty elements) are HTML elements that have no content, so they have no closing tag. They only have attributes.
<br>
<hr>
<img src="photo.jpg" alt="Photo">
<input type="text" name="name">
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
Common void elements are br, hr, img, input, meta, link, source and col. In HTML5 you write them without the slash. In XHTML they were written as <br />. See HTML Elements and Tags.
What is the difference between HTML and HTML5?
HTML5 is the latest major version of HTML. It added new elements and features, so many things that needed plugins can now be done with plain HTML.
| Point | HTML 4 | HTML5 |
|---|---|---|
| Doctype | Long and complex | <!DOCTYPE html> |
| Layout tags | Only div | header, nav, main, footer and more |
| Media | Needed Flash | audio and video |
| Graphics | Images only | canvas and inline SVG |
| Forms | Few input types | email, date, number, range and more |
| Old tags | font, center, frame allowed | Removed. Use CSS |
| Storage | Cookies only | localStorage and sessionStorage |
Read HTML5 Introduction.
What are semantic elements in HTML5? Give examples
Semantic elements are tags whose name tells what the content is. For example <nav> holds navigation links and <footer> is the bottom part of a page. A <div> tells nothing.
header,nav,main,footersection,article,asidefigure,figcaption,time
<header>Site title</header>
<nav>Menu</nav>
<main>
<article>Blog post</article>
<aside>Related links</aside>
</main>
<footer>Copyright</footer>
They help SEO, accessibility and make the code easy to read. See HTML Semantic Elements.
What is the difference between id and class in HTML?
An id must be unique, so it identifies only one element on the page. A class can be used on many elements, and one element can have many classes.
| Point | id | class |
|---|---|---|
| Elements | Only one | Many |
| CSS selector | #name | .name |
| Many on one element | No | Yes, separated by a space |
<style>
#title { color: navy; }
.note { background: #fff3cd; }
</style>
<h1 id="title">Heading</h1>
<p class="note">Note one</p>
<p class="note">Note two</p>
An id is also used as the target of a link (#id). See HTML Class and ID.
What is the alt attribute in HTML and why is it important?
The alt attribute of the <img> tag gives a short text description of the image.
<img src="bike.jpg" alt="Red bicycle near a wall">
- It is shown when the image cannot load.
- Screen readers read it, so blind visitors know what the image is.
- Search engines use it to understand the image, so it helps SEO.
Write what the image shows, in a few words, and do not start with “image of”. For decorative images use an empty alt="". See HTML Images.
How do you create a table in HTML?
Use the <table> tag. Inside it, each row is a <tr>, header cells are <th> and data cells are <td>.
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>Robin</td>
<td>85</td>
</tr>
</table>
| Name | Marks |
|---|---|
| Robin | 85 |
Add borders with CSS. See HTML Tables.
What is the difference between GET and POST methods in an HTML form?
Both are values of the method attribute of a form.
| Point | GET | POST |
|---|---|---|
| Data goes | In the URL | In the body of the request |
| Visible to user | Yes | No |
| Bookmark | Possible | Not possible |
| Size limit | Limited by URL length | Much bigger |
| Use for | Search, filters | Login, 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 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-widthuses the real width of the device.initial-scale=1.0starts with no zoom.
Add it in the head of every page. It is the first step of responsive design.
What is the difference between canvas and SVG in HTML5?
Canvas draws pixels using JavaScript. SVG describes shapes as XML elements.
| Point | Canvas | SVG |
|---|---|---|
| Type | Raster (pixels) | Vector (shapes) |
| Quality when zoomed | Becomes blurry | Always sharp |
| Elements in the page | One canvas element only | Each shape is an element |
| CSS and events on shapes | No | Yes |
| Best for | Games, charts with lots of data, image effects | Icons, logos, simple charts |
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="orange" />
</svg>
See HTML5 Canvas.
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
%andrem, 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>