Top HTML Interview Questions and Answers
Question: What is the difference between HTML and XHTML?
Answer:
HTML (HyperText Markup Language) and XHTML (Extensible Hypertext Markup Language) are both markup languages used for creating web pages, but they have key differences related to their syntax, structure, and how they are processed by browsers. Here’s a breakdown of the key differences:
1. Syntax and Strictness:
-
HTML:
- HTML has a relaxed syntax where certain rules can be ignored, such as missing closing tags or unquoted attribute values. Browsers will attempt to correct errors and still display the page as intended.
- Examples:
- Tags like
<li>
and<p>
can be left unclosed, and browsers will fix them. - Attribute values can be written without quotes, e.g.,
<input type=button>
.
- Tags like
-
XHTML:
- XHTML is more strict than HTML and follows XML (eXtensible Markup Language) syntax rules. It requires that all tags must be properly closed, attribute values must be quoted, and the document must be well-formed.
- Examples:
- Every tag must be properly closed, e.g.,
<br />
instead of<br>
. - Attribute values must always be enclosed in quotes, e.g.,
<input type="button" />
.
- Every tag must be properly closed, e.g.,
2. Case Sensitivity:
-
HTML:
- HTML is case-insensitive. Tag names and attributes can be written in any case, though the convention is to use lowercase.
- Example:
<div>
,<DIV>
, and<DiV>
are all valid in HTML.
-
XHTML:
- XHTML is case-sensitive. All tag names and attributes must be written in lowercase letters.
- Example:
<div>
, but<DIV>
is invalid in XHTML.
3. Document Structure:
-
HTML:
- HTML documents can be less strict about structure. Certain tags like
<html>
,<head>
, and<body>
can be optional in some cases, though their usage is highly recommended.
- HTML documents can be less strict about structure. Certain tags like
-
XHTML:
- XHTML documents must have a well-defined structure. For instance, all HTML elements must be properly nested and closed.
- The
<!DOCTYPE>
declaration in XHTML specifies the XML version and schema.
4. Error Handling:
-
HTML:
- HTML is lenient when it comes to errors. Browsers will attempt to display the page even if there are errors or missing tags. This is called “quirks mode.”
- Browsers generally attempt to fix small issues on the fly.
-
XHTML:
- XHTML is strict in error handling. If an error occurs in the document, the entire page may fail to load or render correctly, and a browser will often not display the content at all. This is because XHTML documents need to be well-formed XML.
5. Doctype Declaration:
-
HTML:
- The DOCTYPE declaration in HTML is simple and specifies the version of HTML being used, e.g.,
<!DOCTYPE html>
for HTML5.
- The DOCTYPE declaration in HTML is simple and specifies the version of HTML being used, e.g.,
-
XHTML:
- XHTML requires a more detailed DOCTYPE declaration, often including references to XML schemas. For example,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
.
- XHTML requires a more detailed DOCTYPE declaration, often including references to XML schemas. For example,
6. MIME Type:
-
HTML:
- HTML files are served with the MIME type
text/html
.
- HTML files are served with the MIME type
-
XHTML:
- XHTML files should ideally be served with the MIME type
application/xhtml+xml
. However, older browsers may not support this MIME type and may default totext/html
, causing potential issues with rendering.
- XHTML files should ideally be served with the MIME type
7. Self-closing Tags:
-
HTML:
- In HTML, self-closing tags (like
<br>
,<img>
, and<input>
) do not require a closing slash.
- In HTML, self-closing tags (like
-
XHTML:
- In XHTML, self-closing tags must always end with a closing slash, e.g.,
<br />
,<img />
, and<input />
.
- In XHTML, self-closing tags must always end with a closing slash, e.g.,
8. Compatibility:
-
HTML:
- HTML is compatible with almost all browsers, including older ones, since it is more lenient in handling errors and inconsistencies.
-
XHTML:
- XHTML requires modern browsers that can handle XML properly. Older browsers may not support the strict parsing rules of XHTML, leading to rendering issues if served with
application/xhtml+xml
.
- XHTML requires modern browsers that can handle XML properly. Older browsers may not support the strict parsing rules of XHTML, leading to rendering issues if served with
Summary of Differences:
Feature | HTML | XHTML |
---|---|---|
Syntax | Relaxed, forgiving | Strict, follows XML syntax |
Case Sensitivity | Case-insensitive | Case-sensitive (all lowercase) |
Document Structure | Flexible | Well-formed structure required |
Error Handling | Lenient (quirks mode) | Strict (fails on errors) |
Doctype Declaration | Simple (e.g., <!DOCTYPE html> ) | Detailed and strict (XML-based) |
MIME Type | text/html | application/xhtml+xml |
Self-closing Tags | Not required (<br> ) | Required (<br /> ) |
Compatibility | Works in all browsers | Requires modern browsers |
Conclusion:
- HTML is more lenient, flexible, and widely supported, while XHTML is stricter, more consistent with XML rules, and aims for well-formed documents but requires more effort and is less forgiving with errors.
- XHTML was intended to replace HTML by offering more stringent rules, but with the advent of HTML5, many of XHTML’s advantages have been incorporated into HTML5, making XHTML less commonly used today.
Question: What is the difference between id
and class
attributes in HTML?
Answer:
The id
and class
attributes are both used to identify and apply styles to HTML elements, but they have distinct characteristics and use cases.
1. Uniqueness:
-
id
:- An
id
is unique within a page. Eachid
value must be used only once in an HTML document. - This makes the
id
suitable for targeting a specific element on the page, such as for styling, JavaScript manipulation, or anchor linking. - Example:
<div id="header">This is a unique header</div>
- An
-
class
:- A
class
is not unique. Multiple elements can share the sameclass
value. - This makes the
class
useful for applying the same styles or behavior to multiple elements. - Example:
<div class="box">First box</div> <div class="box">Second box</div>
- A
2. Use Cases:
id
:- Unique identification: Ideal for identifying a specific element (like a particular section or form) that needs individual styling or JavaScript manipulation.
- Anchor targeting: It can be used in URLs (with the
#
symbol) to link to a specific section within a page. - JavaScript: In JavaScript, you can use
document.getElementById()
to target a single element by itsid
.
class
:- Shared styling: Useful when you want to apply the same styles to multiple elements.
- Reusability: Since multiple elements can have the same
class
, it’s ideal for grouping elements that share the same design or behavior. - JavaScript: In JavaScript, you can select elements by
class
using methods likedocument.getElementsByClassName()
ordocument.querySelectorAll()
.
3. CSS Styling:
-
id
:- In CSS, an
id
is referenced with a hash symbol (#
). - Example: To style an element with the
id="header"
:#header { font-size: 24px; color: blue; }
- In CSS, an
-
class
:- A
class
is referenced with a dot (.
). - Example: To style elements with the
class="box"
:.box { border: 1px solid #000; padding: 10px; }
- A
4. Specificity in CSS:
-
id
:- In CSS,
id
selectors are more specific thanclass
selectors. This means that if there are conflicting styles, the style applied using anid
will take precedence over the style applied using aclass
. - Example:
In this case, an element with the#header { color: red; } .header { color: blue; }
id="header"
will have red color, even if it also has theclass="header"
, because theid
selector is more specific.
- In CSS,
-
class
:class
selectors have lower specificity thanid
selectors. When both are applied to the same element, theid
style will override theclass
style if there’s a conflict.
5. Accessibility and Semantics:
-
id
:- Single-purpose: Since
id
values are unique, they can help with accessibility, such as providing an accessible name for screen readers. - JavaScript and URL targeting:
id
can be used in JavaScript to select specific elements and is also used in anchor links (#idName
) to navigate to specific sections of a page.
- Single-purpose: Since
-
class
:- Multipurpose: The
class
attribute is generally used for styling purposes and grouping elements with similar styles or behaviors. - Less specific for accessibility:
class
doesn’t provide unique identification, so it’s less useful for direct accessibility or specific navigation.
- Multipurpose: The
6. Example Usage:
Using id
:
<!-- Unique element, can be targeted individually -->
<div id="header">Welcome to My Website</div>
<p>This is some text.</p>
Using class
:
<!-- Multiple elements with the same class, sharing common styling -->
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<p class="box">Box styled paragraph</p>
Summary of Differences:
Feature | id | class |
---|---|---|
Uniqueness | Unique within a page (one use) | Can be used on multiple elements |
CSS Reference | #id | .class |
JavaScript Selection | getElementById() | getElementsByClassName() |
Reusability | No, it’s specific to one element | Yes, can be shared across multiple elements |
Specificity in CSS | More specific | Less specific than id |
Conclusion:
- Use
id
when you need to target a specific element uniquely in your page. - Use
class
when you want to apply the same styles or behaviors to multiple elements across your page.
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as HTML interview questions, HTML interview experiences, and details about various HTML job positions. Click here to check it out.
Tags
- HTML
- HTML interview questions
- HTML5
- HTML tags
- Block level elements
- Inline elements
- DOCTYPE
- XHTML vs HTML
- Id vs class attributes
- Semantic HTML
- Alt attribute
- Link vs a tags
- Meta tag
- Viewport meta tag
- Local storage vs session storage
- Data attribute
- Form tag
- GET vs POST
- Defer attribute
- Action attribute
- Inline CSS
- Internal CSS
- External CSS
- Figure and figcaption
- Section vs div
- HTML form handling
- Web development
- HTML structure