Previous

Elements in HTML

Next

In HTML, elements are the basic building blocks that define the structure and content of web pages. An element typically consists of a start tag, content, and an end tag (if the element is not self-closing). Elements can contain other elements nested within them, creating a hierarchical structure.

Anatomy of an Element:

  1. Start Tag: The start tag marks the beginning of an element and is enclosed in angle brackets (< >). It contains the name of the element, which defines its type and functionality.

    Example: <p>, <a>, <div>

  2. End Tag: The end tag marks the end of an element and is also enclosed in angle brackets (</ >). It includes a forward slash (/) followed by the element name.

    Example: </p>, </a>, </div>

  3. Content: The content of an element is the information or nested elements enclosed between the start and end tags. It can include text, other elements, or multimedia content.

Self-Closing Elements:

Some elements do not have closing tags because they don't contain any content or they are self-contained. They are self-closed with a forward slash before the closing angle bracket in the start tag.

Example: <img src="image.jpg" alt="Description">, <input type="text">

Nested Elements:

Elements can be nested inside one another to create a hierarchical structure. This nesting defines the relationship between different parts of the content on a web page.
Example

<div>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph with a <a href="#">link</a>.</p>
</div>

In this example:

  • <div> is a container element.
  • <h1> and <p> are nested inside the <div> element.
  • <a> is nested inside the <p> element.

Tags in HTML

Tags are used to mark up elements within an HTML document. They define the purpose and behavior of elements and provide instructions to web browsers on how to render content. Tags are written using angle brackets (< >) and are case-insensitive in HTML (though lowercase is conventional).

Types of Tags:

  1. Opening Tags: <tag> marks the beginning of an element and includes the element name.

  2. Closing Tags: </tag> marks the end of an element and includes the element name preceded by a forward slash (/).

  3. Self-Closing Tags: Some tags do not require a closing tag because they do not contain any content. They are self-closed with a forward slash before the closing angle bracket (/>).

Examples of Tags:

  • Paragraph Tag: <p> (opening) and </p> (closing).
  • Anchor Tag: <a> (opening) and </a> (closing).
  • Image Tag: <img> (self-closing).

Attributes:

Tags can also include attributes, which provide additional information about an element or modify its behavior. Attributes are specified within the start tag and are written as name-value pairs (name="value").

Example:

<a href="https://example.com" title="Visit Example">Link to Example</a>

    href attribute: Specifies the destination URL for the link.

    title attribute: Provides additional information about the link (tooltip text).

Semantic Meaning:

HTML5 introduced semantic tags that provide more meaning to the content, aiding accessibility and SEO. Examples include <header>, <footer>, <nav>, <article>, <section>, <aside>.