Introduction
This document focuses on essential HTML questions for interviews, aiming to strengthen your foundational knowledge of web development. Each question is followed by a concise answer and explanation, ensuring a comprehensive understanding.
1. What is HTML, and why is it used?
-
Answer: HTML (HyperText Markup Language) is a standard markup language used to create the structure of web pages. It defines elements like headings, paragraphs, images, and links that browsers render.
Explanation: HTML forms the backbone of every webpage and works in tandem with CSS for styling and JavaScript for interactivity.
2. What are semantic elements in HTML?
-
Answer: Semantic elements clearly describe their meaning to both developers and browsers. Examples include
<header>
,<footer>
,<article>
, and<nav>
.Explanation: Semantic elements improve code readability, SEO, and accessibility by explicitly defining the role of each section.
3. How do you create a hyperlink in HTML?
-
Answer: Use the
<a>
tag with thehref
attribute:<a href="https://example.com">Visit Example</a>
Explanation: The
href
attribute specifies the URL the link points to, while the text between the opening and closing<a>
tags serves as the clickable link.
4. What is the difference between inline and block-level elements?
-
Answer: Block-level elements start on a new line and occupy the full width (e.g.,
<div>
,<p>
), while inline elements appear within a line of text (e.g.,<span>
,<a>
).Explanation: Understanding the behavior of these elements is crucial for structuring layouts and styling content.
5. How do you add an image to a webpage?
-
Answer: Use the
<img>
tag withsrc
andalt
attributes:<img src="image.jpg" alt="Description of the image">
Explanation: The
src
attribute specifies the image path, and thealt
attribute provides alternative text for accessibility and when the image cannot load.
6. What is the purpose of the <head>
element?
-
Answer: The
<head>
contains metadata about the document, such as the title, character encoding, and links to stylesheets and scripts.Explanation: Content in the
<head>
is not displayed on the page but plays a critical role in its functionality and SEO.
7. What is the <meta>
tag used for?
-
Answer: The
<meta>
tag provides metadata, such as the character set, viewport settings, and SEO information. Example:<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
Explanation: Metadata helps browsers and search engines understand the page better, enhancing performance and accessibility.
8. How do you create a list in HTML?
-
Answer:
-
Unordered list:
<ul> <li>Item 1</li> <li>Item 2</li> </ul>
-
Ordered list:
<ol> <li>First Item</li> <li>Second Item</li> </ol>
Explanation: Unordered lists use bullet points, while ordered lists use numbers or letters.
-
9. What is the role of the id
and class
attributes?
-
Answer: The
id
attribute uniquely identifies an element, while theclass
attribute applies shared styles to multiple elements.Explanation: IDs are unique within a page and used for JavaScript or CSS targeting. Classes group elements for styling or scripting.
10. How do you include an external CSS file?
-
Answer: Use the
<link>
tag inside the<head>
section:<link rel="stylesheet" href="styles.css">
Explanation: The
href
attribute specifies the path to the CSS file, andrel="stylesheet"
identifies the link as a style sheet.
11. What are void elements in HTML?
-
Answer: Void elements are self-closing tags that do not require a closing tag. Examples include
<img>
,<input>
,<br>
, and<hr>
.Explanation: These elements are typically used for inserting content or structure that does not need child elements.
12. How do you create a table in HTML?
-
Answer: Use the
<table>
,<tr>
(row),<th>
(header), and<td>
(data) tags:<table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> </tr> </table>
Explanation: Tables structure data into rows and columns, ideal for presenting tabular data.
13. What is the difference between <b>
and <strong>
?
-
Answer:
<b>
makes text bold without semantic meaning, while<strong>
conveys semantic emphasis along with bold formatting.Explanation: Use
<strong>
for important content to enhance accessibility and SEO.
14. How do you create a form in HTML?
-
Answer: Use the
<form>
element with input fields:<form action="/submit" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name"> <button type="submit">Submit</button> </form>
Explanation: Forms collect user input, and the
action
attribute specifies where the form data is sent.
15. What is the <iframe>
element used for?
-
Answer: Embeds another HTML document within the current page:
<iframe src="https://example.com" width="600" height="400"></iframe>
Explanation:
<iframe>
allows you to display external content, such as videos or maps, directly within your page.