HTML for Web Development

0% completed

Previous
Next
HTML Elements

In this lesson, you'll learn about HTML elements, the fundamental building blocks that make up the structure of a web page. Understanding HTML elements is crucial for creating well-organized and functional websites.

What Are HTML Elements?

HTML elements are the individual components that form the structure and content of a web page. Each element is defined by a tag, which consists of an opening tag and a closing tag, and the content enclosed between them. Elements can represent anything from sections and lists to tables and forms.

Syntax of HTML Elements

The general syntax for an HTML element is as follows:

Image
<tagname attribute1="value1" attribute2="value2">Content goes here</tagname>
  • <tagname>: The opening tag that defines the start of the element.
  • attribute1="value1" attribute2="value2": Optional attributes that modify the element's behavior or provide additional information.
  • Content goes here: The content inside the element.
  • </tagname>: The closing tag that defines the end of the element.

Example Explanation:

<div class="container" id="main-container">This is a container div.</div>
  • <div>: The opening tag for a division or section.
  • class="container" and id="main-container": Attributes that assign a class and an ID to the div.
  • This is a container div.: The content inside the div.
  • </div>: The closing tag for the div.

Rules and Characteristics of HTML Elements

  • Proper Tag Pairing: Ensure that every opening tag has a corresponding closing tag.

    <div>Content</div>
  • Case Insensitivity: HTML tags are not case-sensitive, but it's a best practice to use lowercase letters.

    <div>Content</div> <!-- Preferred --> <DIV>Content</DIV> <!-- Valid but not recommended -->
  • Nesting Elements: Elements can be nested within other elements to build complex structures.

    <div> <ul> <li>List Item 1</li> <li>List Item 2</li> </ul> </div>
  • Self-Closing Elements: Some elements do not require a closing tag and are self-closing (e.g., <img>, <br>).

    <img src="logo.png" alt="Company Logo" />
  • Attributes Placement: Attributes are added within the opening tag to provide additional information about the element.

    <div class="container" id="main-container">Content goes here</div>

Examples of HTML Elements

Let's explore some common HTML elements with examples.

1. <div> Element

The <div> element is a block-level container used to group other HTML elements together. It doesn't inherently represent anything and is primarily used for styling and layout purposes.

Syntax:

<div style="property1: value1; property2: value2;">Content goes here</div>
  • <div>: The opening tag for the division.
  • style="property1: value1; property2: value2;": The style attribute adds inline CSS to style the div.
  • Content goes here: The content inside the div.
  • </div>: The closing tag for the division.

Example: Creating a Styled Container

HTML

. . . .

Explanation:

  • The <div> element creates a container with a light blue background, padding, and a blue border.
  • The style attribute applies CSS properties directly to the div, enhancing its appearance.

2. <span> Element

The <span> element is an inline container used to group text or other inline elements. Like <div>, it doesn't inherently represent anything and is used for styling purposes.

Syntax:

<span style="property1: value1; property2: value2;">Inline content here</span>
  • <span>: The opening tag for the span.
  • style="property1: value1; property2: value2;": The style attribute adds inline CSS to style the span.
  • Inline content here: The content inside the span.
  • </span>: The closing tag for the span.

Example: Styling Specific Text

HTML

. . . .

Explanation:

  • The <span> element wraps the text "TechGrind.io" and applies red color and bold font weight to it.
  • This allows specific styling of a portion of text within a paragraph without affecting the entire paragraph.

Nested Elements

Nesting elements means placing one HTML element inside another. This technique allows you to create more complex and organized structures within your web pages.

Syntax of Nested Elements

The general syntax for nesting elements is as follows:

<parentElement attribute1="value1"> <childElement attribute2="value2">Content goes here</childElement> </parentElement>
  • <parentElement>: The opening tag for the parent element.
  • attribute1="value1": Attributes for the parent element.
  • <childElement>: The opening tag for the child element nested inside the parent.
  • attribute2="value2": Attributes for the child element.
  • Content goes here: The content inside the child element.
  • </childElement>: The closing tag for the child element.
  • </parentElement>: The closing tag for the parent element.

Example: Creating a Nested Structure with <div> and <span>

HTML

. . . .

Explanation:

  • The outer <div> element serves as a container with a light gray background, padding, and a border.
  • Inside the <div>, there's a <span> element that styles the text "Nested Span Text" with green color and a larger font size.
  • Additionally, a <p> (paragraph) element is nested within the same <div>, demonstrating how multiple elements can be organized within a single container.

Key Points:

  • Parent and Child Relationship: The <div> is the parent element, and the <span> and <p> are child elements nested within it.
  • Styling: Both parent and child elements can have their own style attributes, allowing for targeted styling of different parts of the content.
  • Organization: Nesting helps in organizing content logically, making it easier to manage and style different sections of your web page.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next