HTML Elements and Tags

HTML elements and tags are the fundamental components of web pages. They define the structure and content of a webpage, allowing browsers to render text, images, links, and other media correctly. Understanding how HTML elements and tags work is essential for anyone learning web development.


What Are HTML Elements and Tags?

An HTML element consists of an opening tag, content (optional), and a closing tag. Tags are keywords enclosed in angle brackets (<>), instructing the browser on how to display content.

Example of a basic HTML element:

<p>This is a paragraph.</p>
  1. <p> → Opening tag
  2. This is a paragraph. → Content inside the element
  3. </p> → Closing tag

Types of HTML Tags

1. Paired Tags (Container Elements)

Most HTML tags come in pairs with an opening (<tag>) and a closing (</tag>) tag, enclosing the content between them.

Examples:

<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<a href="https://meripariksha.com">Click here</a>
  1. <h1>: Defines a heading.
  2. <p>: Defines a paragraph.
  3. <a>: Defines a hyperlink, with the href attribute specifying the destination.

2. Self-Closing Tags (Void Elements)

Some HTML elements do not contain any content and do not require a closing tag. These are called self-closing or void elements.

Examples:

<img src="image.jpg" alt="Sample Image">
<br>
<hr>
<input type="text">
  1. <img>: Displays an image, using src to specify the file location.
  2. <br>: Inserts a line break.
  3. <hr>: Creates a horizontal line.
  4. <input>: Defines an input field in a form.

HTML Elements and Attributes

HTML elements can have attributes, which provide additional information about the element. Attributes are always added inside the opening tag and follow the format:

<tag attribute="value">Content</tag>

Common HTML Attributes

AttributeDescriptionExample
idUnique identifier for an element<div id="header">
classDefines a CSS class for styling<p class="text-large">
srcSpecifies the source of an image or script<img src="image.jpg">
hrefDefines the destination URL for a link<a href="https://example.com">
altProvides alternative text for images<img src="photo.jpg" alt="A sunset">

Nesting HTML Elements

HTML elements can be nested, meaning one element can be placed inside another. However, elements must be properly closed to maintain correct structure.

✅ Correct Nesting:

<div>
<h1>Welcome to My Website</h1>
<p>This is a paragraph inside a div.</p>
</div>

❌ Incorrect Nesting:

<p>This is a paragraph <div>inside a div</p></div> <!-- Incorrect -->

In the incorrect example, the <p> tag is opened first but closed after the <div>, which breaks HTML structure.


Block-Level vs. Inline Elements

HTML elements can be categorized as block-level or inline elements based on how they affect the page layout.


Block-Level Elements:

Block elements start on a new line and take up the full width of their parent container.

Examples:

<div></div>
<h1></h1>
<p></p>
<ul></ul>

Inline Elements:

Inline elements do not start on a new line and only take up as much width as necessary.

Examples:

<a></a>
<span></span>
<strong></strong>
<img>

Conclusion

HTML elements and tags are the foundation of web pages, allowing developers to structure and display content effectively. Tags can be paired or self-closing, and elements can contain attributes to enhance functionality. Understanding the correct use of nesting, block-level vs. inline elements, and attributes is essential for writing clean, efficient HTML code.


By mastering these basics, you’ll be well on your way to building professional and interactive web pages! 🚀

How-To Guides for HTML

No How-To Guide is available right now.

Keep Learning & Level Up!

Enhance your HTML skills with our in-depth tutorials and interactive quizzes.

Explore HTML Quiz