Table of contents
1.
Introduction
2.
Characteristics of Void Elements
3.
HTML <br> Tag
3.1.
Example
4.
HTML <hr> Tag
4.1.
Example
5.
HTML <img> Tag
5.1.
Example:
6.
HTML <input> Tag
6.1.
Example
7.
HTML <link> Tag
7.1.
Example
8.
HTML <base> Tag
8.1.
Example
9.
HTML <meta> Tag
9.1.
Example
10.
Self-Closing Tags
10.1.
<br>
10.2.
<hr>
10.3.
<img>
10.4.
<input>
11.
Frequently Asked Questions
11.1.
What are void elements in HTML?
11.2.
Why do void elements not need closing tags?
11.3.
Can we use a closing tag with void elements?
12.
Conclusion
Last Updated: Feb 2, 2025
Medium

What Are Void Elements In HTML?!

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Void elements in HTML are self-closing tags that do not require an end tag or contain content. They are commonly used for adding images, line breaks, and metadata. Examples include <img>, <br>, <meta>, and <input>. These elements help structure web pages efficiently. 

What Are Void Elements In HTML?!

In this article, you will learn about the syntax of void elements, their purpose, and how to use them correctly in HTML.

Characteristics of Void Elements

  • No Closing Tag: Void elements do not need a closing tag.
     
  • Self-contained: They do not have any content between opening and closing tags.
     
  • Specific Purpose: Each void element has a unique function.
     
  • Common in Web Development: Used for structuring pages efficiently.

Let’s look at some of the most commonly used void elements in HTML.

HTML <br> Tag

The <br> tag is used to insert a line break in the text. It is useful when you need to break a sentence into a new line without starting a new paragraph.

Example

<!DOCTYPE html>
<html>
<body>
    <p>This is the first line.<br>This is the second line.</p>
</body>
</html>


Output

Output

The <br> tag ensures that the second sentence starts on a new line without adding extra spacing.

HTML <hr> Tag

The <hr> tag is used to insert a horizontal rule, usually to separate sections of content visually.

Example

<!DOCTYPE html>
<html>
<body>
    <h2>Section 1</h2>
    <p>Content of section 1</p>
    <hr>
    <h2>Section 2</h2>
    <p>Content of section 2</p>
</body>
</html>


Output:

Output

A horizontal line will appear between Section 1 and Section 2, visually separating the content.

HTML <img> Tag

The <img> tag is used to embed images in a webpage. It requires the src attribute to specify the image location.

Example:

<!DOCTYPE html>
<html>
<body>
    <h2>Sample Image</h2>
    <img src="https://example.com/sample.jpg" alt="Sample Image" width="300">
</body>
</html>

Output

Output

Explanation

  • The src attribute specifies the image URL.
     
  • The alt attribute provides alternative text for accessibility.
     
  • The width attribute sets the image width.

HTML <input> Tag

The <input> tag is used to create interactive form fields such as text boxes, checkboxes, and buttons.

Example

<!DOCTYPE html>
<html>
<body>
    <form>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>


Output

Output

Explanation:

  • type="text" creates a text input field.
     
  • type="submit" creates a submit button.
     
  • The <br> tag is used to add space between elements.

HTML <link> Tag

The <link> tag is used to connect external resources like CSS stylesheets.

Example

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h2>Styled Text</h2>
</body>
</html>


Output

Output

Explanation:

  • The rel attribute specifies the relationship (stylesheet in this case).
     
  • The href attribute points to the CSS file.

HTML <base> Tag

The <base> tag sets a default URL for all relative links on a page.

Example

<!DOCTYPE html>
<html>
<head>
    <base href="https://www.example.com/">
</head>
<body>
    <a href="page1.html">Visit Page 1</a>
</body>
</html>


Output

Output

Explanation:

  • All relative URLs will now be prefixed with https://www.example.com/.
     
  • The link will point to https://www.example.com/page1.html.

HTML <meta> Tag

The <meta> tag provides metadata about the webpage, such as character encoding and SEO descriptions.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="description" content="Learn about HTML void elements">
</head>
<body>
    <h2>Meta Tag Example</h2>
</body>
</html>


Output

Output

Explanation:

  • charset="UTF-8" sets character encoding to UTF-8.
     
  • name="description" provides a page description for search engines.

Self-Closing Tags

Void elements are often referred to as self-closing tags. These tags are unique because they do not require a closing tag. Instead, they are complete on their own. This means you can add them to your HTML document without needing to wrap any content inside them.

Let's look at some common void elements and how they are used:

<br>

This tag is used to create a line break in your text. It helps to separate lines of text without creating a new paragraph.

<!DOCTYPE html>
<html>
<head>
    <title>Void Elements Example</title>
</head>
<body>
    <p>This is the first line.<br>This is the second line.</p>
</body>
</html>


Output

Output

<hr>

This tag creates a horizontal line across the page. It is often used to separate sections of content.

<!DOCTYPE html>
<html>
<head>
    <title>Void Elements Example</title>
</head>
<body>
    <p>This is the first section.</p>
    <hr>
    <p>This is the second section.</p>
</body>
</html>


Output

Output

<img>

This tag is used to insert images into your webpage. It requires the src attribute to specify the image source.

<!DOCTYPE html>
<html>
<head>
    <title>Void Elements Example</title>
</head>
<body>
    <img src="example.jpg" alt="Example Image">
</body>
</html>


Output

<input>

This tag is used to create input fields in forms. It can be used for text, checkboxes, radio buttons, and more.

<!DOCTYPE html>
<html>
<head>
    <title>Void Elements Example</title>
</head>
<body>
    <form>
        <input type="text" name="username" placeholder="Enter your username">
        <input type="submit" value="Submit">
    </form>
</body>
</html>


Output

Output

Frequently Asked Questions

What are void elements in HTML?

Void elements are HTML elements that do not have closing tags and do not contain any content. Examples include <br>, <hr>, <img>, <meta>, and <link>.

Why do void elements not need closing tags?

Void elements are self-contained and do not wrap around any content, so a closing tag is unnecessary.

Can we use a closing tag with void elements?

No, void elements do not require a closing tag, and adding one may cause unexpected behavior in some browsers.

Conclusion

Void elements in HTML play an essential role in web development. They help insert line breaks, images, metadata, and other non-textual content efficiently. Understanding how to use them correctly ensures that your web pages are structured properly.

Live masterclass