HTML

Introduction to Web and HTML

How the Web Works What is HTML Why Learn HTML

HTML Structure and Syntax

HTML Syntax

HTML, short for Hypertext Markup Language, is a markup language used to structure content on the web. To effectively create web pages, it's essential to understand HTML's syntax—the rules and conventions that dictate how HTML documents are written. Let's delve into the core aspects of HTML syntax:

1. HTML Tags

HTML documents are composed of elements, and each element is defined using HTML tags. Tags are enclosed in angle brackets and come in pairs: an opening tag and a closing tag. The opening tag marks the beginning of an element, and the closing tag marks its end. For example, the paragraph element is defined using the <p> tag:

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

2. Nesting Elements

HTML elements can be nested within each other to create a hierarchy of content. It's essential to follow a specific order when nesting elements. Elements should be opened and closed in the same order they were opened. For example, in the following code, the <strong> element is nested within the <p> element:

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

3. Attributes

HTML tags can also have attributes that provide additional information about the element. Attributes are typically added to the opening tag and are specified in a name-value pair format. For example, the <img> element uses the src attribute to specify the image source:

<img src="image.jpg" alt="A sample image">

4. Case Insensitivity

HTML is not case-sensitive, meaning that the tags and attribute names can be written in uppercase or lowercase, or a combination of both. However, it's a widely adopted convention to use lowercase for HTML tags and attribute names to improve code readability.

<p>This is a paragraph.</P> <!-- Valid, but not recommended -->

5. Self-Closing Tags

Some HTML elements do not have a closing tag because they are self-contained. They are often referred to as self-closing tags or void elements. For example, the line break element, <br>, doesn't have a closing tag:

<p>This is a <br> line break.</p>

6. Comments

You can add comments to your HTML code for documentation or to make notes. Comments are not visible on the web page and are enclosed in <!-- and -->. For example:

<!-- This is a comment -->

7. Document Structure

Every HTML document should have a standard structure, which includes the following elements:

  • <!DOCTYPE html>: This declaration specifies the document type and version of HTML.
  • <html>: The root element that wraps the entire web page.
  • <head>: Contains metadata about the page, such as the title and links to external resources.
  • <meta>: Provides character encoding and other information about the document.
  • <title>: Sets the title of the web page, which appears in the browser's title bar or tab.
  • <body>: Contains the main content of the web page.
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Page Title</title>
  </head>
  <body>
    <!-- Content goes here -->
  </body>
</html>

8. Whitespace

HTML ignores extra white spaces and line breaks, so you can use them to format your code for better readability. However, excessive use of white space should be avoided to maintain clean and efficient code.

9. Character Encoding

To ensure that your web pages display text correctly, it's crucial to specify the character encoding. This is typically done with the <meta> tag within the <head> section:

<meta charset="UTF-8">

10. HTML is Forward-Compatible

HTML is designed to be forward-compatible, meaning that new versions of HTML should be backward-compatible with older versions. This allows you to create web pages that continue to function as the language evolves.

In summary, understanding HTML syntax is vital for creating well-structured and valid web pages. By following these rules and conventions, you can write clean and maintainable HTML code that browsers can interpret accurately.

Previous Why Learn HTML
Next HTML Tags