View All HTML Tutorials

HTML Elements, Tags, and Attributes Explained

Learn the difference between HTML elements, tags, and attributes, including how opening tags, closing tags, and empty elements work together. This beginner-friendly guide explains HTML structure, nesting, common attributes like `href` and `src`, and the mistakes to avoid when writing clean, reliable HTML.

Visual guide explaining HTML elements, tags, and attributes.

Introduction

HTML is made from small building blocks called elements. These elements tell the browser what different parts of a web page are.

For example, HTML can tell the browser:

HTML
<h1>This is a heading</h1>
HTML
<p>This is a paragraph.</p>
HTML
<a href=”https://www.example.com”>This is a link</a>

At first, HTML can look like a collection of symbols, angle brackets, and words. Once you understand elements, tags, and attributes, the structure becomes much easier to read.

This guide explains the difference between HTML elements, opening tags, closing tags, empty elements, and attributes.

What Is an HTML Element?

An HTML element is a complete piece of HTML that describes part of a web page.

A typical element has three parts:

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

Those three parts are:

HTML
<p>

The opening tag.

TEXT
This is a paragraph.

The content.

HTML
</p>

The closing tag.

Together, these create one complete paragraph element:

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

The browser reads this and understands that the text should be treated as a paragraph.

What Is an HTML Tag?

A tag is the part of an HTML element written inside angle brackets.

For example:

HTML
<p>

and:

HTML
</p>

are both tags.

The opening tag starts the element.

The closing tag ends the element.

The full element includes the tags and the content:

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

People sometimes use the words “tag” and “element” as if they mean the same thing. In casual conversation, this is common. However, there is a useful difference:

A tag is the markup inside angle brackets.

An element is the complete structure, including the opening tag, content, and closing tag.

Opening Tags

An opening tag marks the start of an HTML element.

For example:

HTML
<h1>

This opening tag starts a main heading.

HTML
<p>

This opening tag starts a paragraph.

HTML
<a>

This opening tag starts a hyperlink.

An opening tag is written using the element name inside angle brackets:

HTML
<tagname>

For example:

HTML
<p>

means “start a paragraph here”.

Closing Tags

A closing tag marks the end of an HTML element.

It looks similar to the opening tag, but it includes a forward slash /.

For example:

HTML
</h1>

ends a heading.

HTML
</p>

ends a paragraph.

HTML
</a>

ends a hyperlink.

The pattern is:

HTML
</tagname>

For example:

HTML
</p>

means “end the paragraph here”.

Opening and Closing Tags Together

Most HTML elements use both an opening tag and a closing tag.

For example:

HTML
<h1>Welcome to My Website</h1>

The opening tag is:

HTML
<h1>

The content is:

TEXT
Welcome to My Website

The closing tag is:

HTML
</h1>

Here is another example:

HTML
<p>HTML is used to structure content on a web page.</p>

The opening tag starts the paragraph.

The closing tag ends the paragraph.

The text between them is the paragraph content.

Nesting HTML Elements

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

For example:

HTML
<p>This is a <strong>very important</strong> message.</p>

In this example, the <strong> element is inside the <p> element.

The paragraph element is:

HTML
<p>This is a <strong>very important</strong> message.</p>

The strong element is:

HTML
<strong>very important</strong>

The browser treats “very important” as important text inside the paragraph.

When you nest elements, close them in the correct order.

Correct:

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

Incorrect:

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

The first element opened on the outside should usually be the last one closed.

Empty Elements

Not all HTML elements have content between an opening and closing tag.

Some elements are empty. This means they do not wrap around text or other content.

For example:

HTML
<br>

The <br> element creates a line break.

It does not need a closing tag.

You should write:

HTML
<br>

Not:

HTML
<br></br>

Another common empty element is <img>, which adds an image:

HTML
<img src=”photo.jpg” alt=”A person walking in a park”>

The image element does not contain text between an opening and closing tag. Instead, it uses attributes to tell the browser which image to show and how to describe it.

Another example is <hr>, which creates a thematic break:

HTML
<hr>

Empty elements are sometimes called void elements.

Empty Elements vs Normal Elements

A normal element usually has an opening tag, content, and a closing tag:

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

An empty element does not have content inside it:

HTML
<br>
HTML
<hr>
HTML
<img src=”photo.jpg” alt=”A useful description”>

Use normal elements when you need to wrap content.

Use empty elements when the element represents something that does not contain content of its own.

What Are Attributes?

Attributes give extra information to an HTML element.

They are written inside the opening tag.

For example:

HTML
<a href=”https://www.example.com”>Visit Example</a>

In this link, the attribute is:

HTML
href=”https://www.example.com”

The href attribute tells the browser where the link should go.

Without the href attribute, the browser would not know the destination of the link.

Attribute Names and Values

Most attributes have a name and a value.

The pattern is:

HTML
name=”value”

For example:

HTML
href=”https://www.example.com”

The attribute name is:

HTML
href

The attribute value is:

HTML
https://www.example.com

Here is another example:

HTML
<img src=”logo.png” alt=”Company logo”>

This image element has two attributes:

HTML
src=”logo.png”

and:

HTML
alt=”Company logo”

The src attribute tells the browser where the image file is.

The alt attribute gives alternative text for the image.

Attributes Go in the Opening Tag

Attributes are placed inside the opening tag.

Correct:

HTML
<a href=”https://www.example.com”>Visit Example</a>

Incorrect:

HTML
<a>Visit Example</a href=”https://www.example.com”>

For an image, the attributes also go inside the tag:

HTML
<img src=”photo.jpg” alt=”A landscape photo”>

Because <img> is an empty element, all the important information is provided through attributes.

Common HTML Attributes

Different elements use different attributes.

The <a> element commonly uses href:

HTML
<a href=”about.html”>About Us</a>

The <img> element commonly uses src and alt:

HTML
<img src=”team.jpg” alt=”Our team standing outside the office”>

The <html> element commonly uses lang:

HTML
<html lang=”en”>

The lang attribute tells the browser the main language of the page.

Many elements can use the class attribute:

HTML
<p class=”intro”>This is the introduction.</p>

The class attribute is often used with CSS to style elements.

Many elements can also use the id attribute:

HTML
<h2 id=”contact”>Contact Us</h2>

The id attribute gives an element a unique identifier on the page.

Multiple Attributes

An element can have more than one attribute.

For example:

HTML
<a href=”https://www.example.com” target=”_blank” rel=”noopener noreferrer”>  Visit Example</a>

This link has three attributes:

HTML
href=”https://www.example.com”
HTML
target=”_blank”
HTML
rel=”noopener noreferrer”

The href attribute gives the link destination.

The target="_blank" attribute opens the link in a new tab.

The rel="noopener noreferrer" attribute is commonly used with target="_blank" for security and privacy reasons.

Attributes are separated by spaces inside the opening tag.

Quotation Marks in Attributes

It is good practice to put attribute values in quotation marks.

Recommended:

HTML
<a href=”https://www.example.com”>Visit Example</a>

Avoid writing attributes without quotes:

HTML
<a href=https://www.example.com>Visit Example</a>

Browsers may understand some unquoted attribute values, but quoted values are clearer, safer, and easier to read.

Use quotation marks consistently.

Boolean Attributes

Some attributes do not need a value. These are called boolean attributes.

A boolean attribute is either present or not present.

For example, the disabled attribute can be used on a button:

HTML
<button disabled>Submit</button>

This means the button is disabled.

Another example is checked on a checkbox:

HTML
<input type=”checkbox” checked>

This means the checkbox is checked by default.

For beginners, the main point is simple: some attributes follow name="value", while a few attributes work just by being present.

A Complete Example

Here is a simple HTML page using elements, tags, empty elements, and attributes:

HTML
<!DOCTYPE html><html lang=”en”><head>  <meta charset=”UTF-8″>  <title>Elements, Tags, and Attributes</title></head><body>   <h1>About Our Website</h1>   <p class=”intro”>We create simple websites for small businesses.</p>   <p>    Visit our    <a href=”services.html”>services page</a>    to learn more.  </p>   <img src=”office.jpg” alt=”A modern office desk with a laptop”>   <hr>   <p>Thank you for visiting our website.</p> </body></html>

This example includes several important ideas.

The <h1> element has an opening tag, content, and a closing tag:

HTML
<h1>About Our Website</h1>

The <p> element also has an opening tag, content, and a closing tag:

HTML
<p class=”intro”>We create simple websites for small businesses.</p>

The paragraph has a class attribute:

HTML
class=”intro”

The link has an href attribute:

HTML
<a href=”services.html”>services page</a>

The image is an empty element:

HTML
<img src=”office.jpg” alt=”A modern office desk with a laptop”>

The horizontal rule is also an empty element:

HTML
<hr>

Common Mistake: Forgetting the Closing Tag

A common mistake is forgetting to close an element.

Incorrect:

HTML
<p>This is the first paragraph.<p>This is the second paragraph.

Better:

HTML
<p>This is the first paragraph.</p><p>This is the second paragraph.</p>

Browsers may try to fix missing tags automatically, but it is better to write clear and complete HTML.

Common Mistake: Closing Elements in the Wrong Order

When elements are nested, they should be closed in the correct order.

Incorrect:

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

Correct:

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

The <strong> element started inside the paragraph, so it should also close inside the paragraph.

Common Mistake: Putting Attributes in the Closing Tag

Attributes belong in the opening tag, not the closing tag.

Incorrect:

HTML
<a>Visit Example</a href=”https://www.example.com”>

Correct:

HTML
<a href=”https://www.example.com”>Visit Example</a>

The browser needs the href attribute when the link starts.

Common Mistake: Adding Closing Tags to Empty Elements

Empty elements do not need closing tags.

Avoid this:

HTML
<br></br>

Use this:

HTML
<br>

Avoid this:

HTML
<img src=”photo.jpg” alt=”Photo”></img>

Use this:

HTML
<img src=”photo.jpg” alt=”Photo”>

Best Practices

Use opening and closing tags for normal elements.

Close elements in the correct order when nesting them.

Use empty elements only for elements that do not contain content.

Place attributes inside the opening tag.

Use quotation marks around attribute values.

Use clear and meaningful attribute values.

Use alt text for meaningful images.

Use lowercase tag and attribute names for consistency.

Indent nested elements so your HTML is easier to read.

Summary

HTML pages are built from elements.

A typical element has an opening tag, content, and a closing tag:

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

The opening tag starts the element:

HTML
<p>

The closing tag ends the element:

HTML
</p>

Some elements are empty and do not have content or closing tags:

HTML
<br><hr><img src=”photo.jpg” alt=”Description”>

Attributes give extra information to elements:

HTML
<a href=”https://www.example.com”>Visit Example</a>

In this example, href tells the browser where the link should go.

Once you understand opening tags, closing tags, empty elements, and attributes, HTML becomes much easier to read and write.