Definition:
In markup languages (like HTML or XML),
a tag is a piece of code used to define an element,
instructing the browser or system "how to interpret and structure content" rather than "how to present it visually."
Tag Structure
An HTML/XML element typically consists of three parts:
- Opening Tag (<tagname>): Marks the start of an element and can contain attributes.
- Content: The text, image, or other elements nested inside.
- Closing Tag (</tagname>): Marks the end of an element, denoted by a forward slash.
- <p class="intro"> = Opening tag with attribute (class="intro")
- Hello World = Content
- </p> = Closing tag
Example: <p class="intro">Hello World</p>
Essential Rules for Tags
- Correct Syntax: Standard tags require matching opening (<...>) and closing (</...>) tags.
- Self-Closing / Void Tags: Tags that do not wrap content do not require a closing tag (e.g., <img />, <br />, <input />).
-
Proper Nesting: Tags must be closed in the reverse order they were opened (First In, Last Out).
- Correct: <b><i>Text</i></b>
- Incorrect: <b><i>Text</b></i>
- Lowercase Conventions: HTML guidelines recommend writing all tag names and attributes in lowercase (e.g., <div>, not <DIV>).
- Quote Attribute Values: Attributes inside an opening tag should always have their values enclosed in quotation marks (e.g., href="[https://example.com](https://example.com)").
- Separation of Concerns: Tags should only be used to define structure and semantics, not presentation (styling should be handled via CSS).