MongoDB Atlas is built for every app.

Start Building
HTML

HTML

HTML Elements

HTML (Hypertext Markup Language) is the foundation of web pages. It uses elements to organize and structure content, allowing browsers to understand and display the content correctly. Each HTML element consists of tags and content that define its behavior and appearance on the page.

Example:

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

In this example, the <p> tag creates a paragraph element that holds the text inside.

What Are HTML Elements?

An HTML element is made up of three essential parts:

  1. Opening Tag – Indicates the start of the element.
  2. Content – The actual text or data between the tags.
  3. Closing Tag – Marks the end of the element.

Structure:

<opening-tag> content </closing-tag>

Some elements, like <br> and <img>, are self-closing, meaning they don’t have any content or </closing-tag>.

HTML Elements vs. Tags Comparison

Here’s a quick breakdown of the difference between HTML tags and elements to help clarify how they work:

Feature HTML Tag HTML Element
Definition A tag is the identifier that marks the start or end of an HTML element. An element includes both the tags and the content they enclose.
Example <p> (Opening tag) and </p> (Closing tag) <p>This is a paragraph.</p> (Complete element)
Purpose Tags define where an element starts and ends. Elements contain the content and the tags, providing structure and meaning to the webpage.
Content Contains only the start or end instruction. Contains both the tags and the text or nested elements inside.
Self-Closing Tags can be self-closing (e.g., <img />). Elements can be empty but still self-contained, like <img src="image.jpg" />.

Tags are just the boundaries, while elements are the entire package, including the content and tags.

Nested HTML Elements

HTML elements can be nested, meaning you can place one element inside another. This allows you to structure your content more effectively and build complex layouts.

Element Example Description
<div> <div><p>Text</p></div> A block-level container for content.
<p> <p><span>Text</span></p> Paragraphs can contain inline elements.
<ul> <ul><li>Item</li></ul> Lists containing list items.

Example:

<div>
  <p>This is a nested element.</p>
</div>

In the example above, the <p> element is nested inside the <div> element, showing how HTML elements can be combined.

The Importance of Closing Tags

Closing tags are crucial in HTML. Forgetting to close an element can result in unexpected layout issues or broken functionality. Always ensure your elements are properly closed.

Incorrect Example:

<p>This is wrong

Correct Example:

<p>This is correct.</p>

Leaving out closing tags can confuse the browser and make your layout unpredictable.

HTML Empty Elements

Some elements don’t require a closing tag, as they are self-contained. These are known as empty elements. Common examples include <br> for line breaks and <img> for images.

Example:

<img src="sitepoint-logo.jpg" alt="SitePoint Logo">

In this case, the <img> tag is self-closing, as it does not wrap around any content.

Here’s a quick reference table of common HTML empty elements that don’t have closing tags:

Element Description
<br> Creates a line break in the content.
<img> Embeds an image into the webpage.
<hr> Inserts a horizontal rule to separate content.
<input> Defines an input field for forms.
<meta> Provides metadata about the document (e.g., charset).
<link> Links to an external resource, like a CSS file.
<source> Specifies multiple media resources for media elements.
<area> Defines a clickable area within an image map.
<col> Defines column properties for table formatting.

These elements are self-contained and don’t need a closing tag—just plug them into your HTML and they do their job.

Case Sensitivity in HTML Elements

HTML tags are not case-sensitive, so <div> and <DIV> are treated the same. However, it's a best practice to use lowercase tags for readability and consistency across your codebase.

Block-Level HTML Elements

Block-level elements take up the full width of their parent container and always start on a new line. They are used to structure major sections of content on a webpage.

Element Example Description
<div> <div>Block content</div> Creates a division for block-level content.
<h1> <h1>Heading</h1> Represents a top-level heading.
<p> <p>Paragraph content</p> Defines a block of text.

Inline HTML Elements

Unlike block-level elements, inline elements only take up as much width as their content needs and do not force new lines. These are typically used within blocks of text to add functionality or styling.

Element Example Description
<span> <span>Inline text</span> Used to style or manipulate inline text.
<a> <a href="#">Link</a> Represents a hyperlink.
<img> <img src="image.jpg"> Embeds an image.

FAQs on HTML Elements

What Does an HTML Element Contain?

An HTML element is made up of three main parts: an opening tag, the content, and a closing tag. Together, these structure and display the content on your webpage. In the case of empty elements, only the opening tag is needed, as there is no content to enclose.

Example of a Complete Element:

<p>This is a paragraph.</p>
  • Opening tag: <p>
  • Content: This is a paragraph.
  • Closing tag: </p>

Example of an Empty Element:

<img src="sitepoint-sample.jpg " alt="Sample SitePoint Image">

Only the opening tag is used since the <img> element doesn't enclose content.

These parts work together to define and display HTML content properly.

What Is the Difference Between HTML Elements and Tags?

HTML tags are the markers that define the beginning and end of an element. They tell the browser what type of content is being displayed. On the other hand, an HTML element includes everything: the opening tag, the content inside (if there is any), and the closing tag.

While tags are just the structural markers, elements are the full package—tags plus the content they enclose. Tags mark where the element starts and ends, but the element itself is what gets rendered on the page.

Why Use HTML Elements?

HTML elements are fundamental to organizing and structuring web content. They provide the framework that helps browsers interpret, style, and display content correctly. By defining headings, paragraphs, images, links, and more, HTML elements ensure that content is presented in a meaningful and accessible way. They also contribute to search engine optimization (SEO) and enhance accessibility for screen readers, making websites more user-friendly and inclusive across various devices and platforms. Without HTML elements, web pages would be unstructured and difficult to navigate or read.

What Are the Basic Elements of HTML?

Basic HTML elements form the core structure of any webpage. They include essential tags like:

  • <html>: Defines the root of the HTML document.
  • <head>: Contains meta-information, links to stylesheets, and scripts.
  • <title>: Sets the title of the webpage that appears in the browser tab.
  • <body>: Holds all the visible content on the page.
  • <p>: Defines paragraphs of text.
  • <a>: Creates hyperlinks to other pages or resources.

These elements serve as the building blocks of any HTML document, providing structure, formatting, and functionality.

Subscribe to our newsletter

Get the freshest news and resources for developers, designers and digital creators in your inbox each week

© 2000 – 2025 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.