HTML Syntax

HTML syntax refers to the set of rules and conventions for writing HTML code, which is the standard language used to create web pages. It includes the structure and format of elements, tags, attributes, and their relationships. Here’s a breakdown of the core components of HTML syntax:

HTML Elements

An HTML document is composed of elements, each defined by tags.


<p>This is a paragraph.</p>

Explanations:

Opening tag: <p>
Content: This is a paragraph.
Closing tag: </p>

HTML Tags

Tags are enclosed in angle brackets (< >) and are case-insensitive (though lowercase is standard).

Example:


<h1>Main Heading</h1>

Explanations:

<h1> is a heading tag.

Attributes

Attributes provide additional information about elements and are included in the opening tag.

Example:


<img src="image.jpg" alt="An image">

Explanations:

src: Specifies the image file location.
alt: Provides alternative text for the image.

Nesting

Elements can be nested inside one another. Proper nesting ensures valid HTML.

Correct example:


<div>
    <p>Text inside a div.</p>
</div>

Incorrect example:


<div>
    <p>Text inside a div.</div>
</p>

Document Structure

HTML documents follow a specific structure:


<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <h1>Welcome</h1>
    <p>This is a paragraph.</p>
</body>
</html>

Explanations:

<!DOCTYPE html>: Declares the document type.
<html>: Root element.
<head>: Contains metadata like title and styles.
<body>: Contains visible content.

Void Elements

Void elements do not have closing tags.

Examples:


<img src="image.jpg" alt="Image">
<br>
<input type="text">

HTML Syntax – Interview Questions

Q 1: What is HTML syntax?

Ans: HTML syntax defines how elements, tags, and attributes are written.

Q 2: What is a tag in HTML?

Ans: A tag is a keyword enclosed in angle brackets < >.

Q 3: Are HTML tags case-sensitive?

Ans: No, HTML tags are not case-sensitive.

Q 4: What is a closing tag?

Ans: A closing tag ends an element and starts with </.

Q 5: What is a self-closing tag?

Ans: Tags like <br> and <img> that do not need a closing tag.

HTML Syntax – Objective Questions (MCQs)

Q1. What does HTML stand for?






Q2. Which of the following is the correct way to start an HTML document?






Q3. How do you insert a comment in HTML?






Q4. Which of the following is the correct way to include a heading in HTML?






Q5. Which of the following is true about HTML tags?






Related HTML Syntax Topics