HTML Elements

Learn about tags, complete elements, and block-level and inline behavior.

When learning HTML, two of the most important concepts are tags and elements. Every webpage is built using them. Understanding how they work will help you create and organize content on a webpage correctly.

What Are HTML Tags?

HTML tags are special keywords enclosed inside angle brackets. They tell the browser how to display different types of content.

HTML
<h1>Welcome</h1>

In this example:

  • <h1> is the opening tag.
  • </h1> is the closing tag.

The browser understands that the text between these tags should be displayed as a large heading.

Common HTML Tags

HTML
<h1>Main Heading</h1>
<p>This is a paragraph.</p>
<a href="https://www.devbrainbox.com/">Visit Website</a>
<button>Click Me</button>

Each tag has a specific purpose:

  • <h1> creates a heading.
  • <p> creates a paragraph.
  • <a> creates a link.
  • <img> displays an image.

What Are HTML Elements?

An HTML element includes the opening tag, the content, and the closing tag.

HTML
<p>Hello World!</p>

The entire line above is called a paragraph element. It consists of:

  • Opening tag: <p>
  • Content: Hello World!
  • Closing tag: </p>

Think of an element as a complete package, while tags are the parts that define it.

Opening and Closing Tags

Most HTML elements use both opening and closing tags.

HTML
<strong>Important Text</strong>

The opening tag starts the element, and the closing tag ends it. The closing tag contains a forward slash.

HTML
<tagname>
</tagname>

This helps browsers understand where an element begins and ends.

Empty Elements

Some HTML elements do not contain content and therefore do not need a closing tag. These are called empty elements.

HTML
<img src="photo.jpg" alt="Profile Photo" />
<br />
<hr>

Here:

  • <img> displays an image.
  • <br> creates a line break.
  • <hr> creates a horizontal line.

Since these elements do not wrap content, they do not require closing tags.

Nested Elements

HTML elements can be placed inside other elements. This is called nesting.

HTML
<p>
    Welcome to <strong>HTML Learning</strong>.
</p>
  • The strong element is nested inside the paragraph element.
  • The text "HTML Learning" will appear bold.

Think of nesting like putting a smaller box inside a larger box.

Best Practices

When working with HTML tags and elements:

  • Always close tags when required.
  • Use meaningful tags for their intended purpose.
  • Keep your code properly indented.
  • Make sure nested elements are closed in the correct order.

Correct:

HTML
<p>This is <strong>important</strong> text.</p>

Incorrect:

HTML
<p>This is <strong>important</p></strong>

Key Takeaways

  • Elements Tags is an important topic to understand.
  • Start with small examples and practice one step at a time.
  • Use the idea in simple projects so it becomes easier to remember.

HTML elements are commonly described as either block-level or inline. This distinction explains how an element uses the available space and how it appears beside other elements.

What Are Block-Level Elements?

A block-level element starts on a new line and normally stretches across the full available width of its parent container.

HTML
<h2>Course Topics</h2>
<p>Learn HTML one concept at a time.</p>
<div>This is a block container.</div>

Common block-level elements:

  • <div>
  • <p>
  • <h1> to <h6>
  • <section>, <article>, and <main>
  • <header>, <footer>, and <nav>
  • <ul>, <ol>, and <form>

What Are Inline Elements?

An inline element stays in the current line and uses only the width its content needs. It does not force the content after it onto a new line.

HTML
<p>
  Learn <strong>HTML</strong> with a
  <a href="/html">complete tutorial</a>.
</p>

Common inline elements:

  • <span>
  • <a>
  • <strong> and <em>
  • <code>, <mark>, and <small>
  • <img>

Block vs Inline

FeatureBlock-levelInline
New lineStarts on a new lineStays on the same line
WidthUsually fills available widthUses only the needed width
Typical purposePage structure and sectionsText-level content
Examplesdiv, p, sectionspan, a, strong

The div and span Elements

The <div> Element

The <div> element is a generic block container. Use it to group larger sections when no more meaningful semantic element fits.

HTML
<div class="product-card">
  <h3>Wireless Mouse</h3>
  <p>Price: $25</p>
</div>

The <span> Element

The <span> element is a generic inline container. It is useful for styling or identifying a small part of text.

HTML
<p>Price: <span class="price">$25</span></p>

Can CSS Change the Display Type?

Yes. CSS can change how an element participates in layout with thedisplay property. An element's HTML meaning does not change when its visual display changes.

CSS
.navigation-link {
  display: block;
}

.card {
  display: inline-block;
}

inline-block keeps elements beside one another while also allowing width, height, and vertical padding to behave more like a block.

Important Nesting Rule

Do not decide valid nesting from appearance alone. Follow HTML content rules and use elements for their meaning. For example, a paragraph cannot contain a <div>.

HTML
<!-- Invalid -->
<p>Text <div>Block content</div></p>

<!-- Valid -->
<div>
  <p>Text in a paragraph</p>
</div>

Key Takeaways

  • Block-level elements usually begin on a new line and fill available width.
  • Inline elements stay within the current line and use only needed space.
  • Use <div> for generic block grouping and <span> for generic inline grouping.
  • Choose elements by meaning first, then control their layout with CSS.