HTML Elements and Tags
Table of Contents + โ
Now that youโve created your first HTML page and done with HTML setup, now we will learn about what HTML elements and tags are and how to use them properly.
๐๏ธ What Are HTML Elements?
An HTML element is a complete piece of content that consists of three parts:
- An opening tag (like
<h1>
) - Attributes (optional, like
id="header"
) - Content (the text or other elements inside)
- A closing tag (like
</h1>
)
HTML Element Syntax
Hereโs the Syntax of an HTML element:
<h1 id="header">This is a heading</h1>โ | โ โโโ Closing tag| | โโโ Contentโ โโโ Attribute (optional)โโโ Opening tag
Types of HTML Elements
HTML elements can be categorized into two main types:
- Block level elements: These elements take up the full width available and start on a new line. Examples include
<div>
,<p>
,<h1>
,<ul>
, etc. - Inline elements: These elements only take up as much width as necessary and do not start on a new line. Examples include
<span>
,<a>
,<strong>
,<em>
, etc.
HTML Element Examples
<h1>This is a heading</h1><p>This is a paragraph.</p><span>This is inline text.</span><a href="https://example.com">This is a link</a>
The output of the above code will look like this:
Output
As you can see, the <h1>
and <p>
are block-level elements that take up the full width and start on a new line, while <span>
and <a>
are inline elements that only take up as much width as necessary and do not start on a new line.
๐ท๏ธ HTML Tags
Tags are the keywords wrapped in angle brackets (< >
) that tell the browser what type of content to display and how it should look like to users.
Types of HTML Tags
There are two types of HTML tags:
- Container Tags
- Self-Closing Tags
Container Tags (Most Common)
Container tags are used to wrap content. Container tags have both opening and closing tags:
<p>This is a paragraph</p><h1>This is a heading</h1><strong>This is bold text</strong>
In this example <p>
is the opening tag, </p>
is the closing tag, and โThis is a paragraphโ is the content, so <p>
is the container tag.
Self-Closing Tags
Self-closing tags are generally used to insert elements that donโt have any content inside them. But they have special meaning in HTML. Some of the examples of self-closing tags are:
<br> <!-- Line break --><hr> <!-- Horizontal rule --><img> <!-- Image -->
in this example <br>
, <hr>
, and <img>
are self-closing tags. <br>
inserts a line break, <hr>
creates a horizontal rule, and <img>
embeds an image into the page. They do not have any content inside them, so they donโt need a closing tag.
๐ก Remember
Always close your container tags! For every opening tag like <p>
, you need a matching closing tag like </p>
.
๐ท๏ธ HTML Elements vs HTML Tags
HTML elements and HTML tags are closely related but you donโt have to worry about the difference between them. They are often used interchangeably.
๐ How to Use HTML Elements
Basic Structure
Every HTML element follows the same pattern:
<tagname>content goes here</tagname>
Examples of Common Elements
<h1>Main Title</h1><p>This is a paragraph of text.</p><strong>Important text</strong><em>Emphasized text</em>
Nested HTML Elements
You can use elements inside other elements and its called nesting.
Nested Elements example:
For Example, you can put a <strong>
element inside a <p>
element:
<p>This paragraph contains <strong>bold text</strong> and <em>italic text</em>.</p>```html<p>This paragraph contains <strong>bold text</strong> and <em>italic text</em>.</p>
๐ฏ Nesting Rule
When nesting elements, close them in the reverse order you opened them. Like the once opened first is closed last.
Correct vs Incorrect Nesting
โ Correct nesting:
<p>Text with <strong>bold and <em>italic</em></strong> formatting.</p><p>Text with <strong>bold</strong> and <em>italic</em> formatting.</p>
โ Incorrect nesting:
<p>Text with <strong>bold and <em>italic</strong></em> formatting.</p><p>Text with <strong>bold and <em>italic</em> formatting.</p></strong>
In this example, the first paragraph is not correctly nested because the closing tags are not in the correct order. The <strong>
tag should close after the <em>
tag. Similarly, the second paragraph has the closing </strong>
tag after the <em>
tag, which is incorrect.
๐ง HTML Attributes
HTML attributes provide additional information about an element. They are always specified in the opening tag and come in name/value pairs like name="value"
.
Element Attributes syntax
<tagname name="value" id="uniqueID">content</tagname>
An HTML element can have multiple attributes to provide more details about it.
Common Attributes Example
<h1 class="main-title" id="header">Page Title</h1><p style="color: blue;">Blue text paragraph</p><img src="photo.jpg" alt="Description">
๐ฏ Practical Example
Letโs see how elements work together in a simple webpage:
<!DOCTYPE html><html><head> <title>My First Page</title></head><body> <h1>Welcome to My Website</h1>
<p>This is my <strong>first webpage</strong> using HTML elements!</p>
<p>I'm learning about <em>elements</em> and <em>tags</em>.</p>
<p>Each element has: <br>- An opening tag <br>- Content <br>- A closing tag </p></body></html>
๐ฎ Try It Yourself!
Practice using HTML elements by creating a simple page:
- Create an HTML file name index.html,
- Add a heading with
<h1>
- Write a paragraph with
<p>
- Make some text bold with
<strong>
- Make some text italic with
<em>
- Try nesting elements inside each other
๏ฟฝ Key Takeaways
Now you understand:
- โ Elements are complete pieces of content (opening tag + content + closing tag)
- โ Tags are the keywords in angle brackets that define the element type
- โ Some tags are self-closing and donโt need closing tags
- โ Elements can be nested inside other elements
- โ Attributes provide additional information about elements
- โ Proper nesting order is crucial for valid HTML
๐ Whatโs Next?
In our next lesson, weโll learn about HTML Headings.
Ready to keep building your HTML skills? Letโs continue! ๐