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">