Void Element
May 20, 2023
A void element in HTML is a self-closing tag that does not require a closing tag. The syntax for a void element consists of the element name, followed by zero or more attributes, and a closing slash. For example, the <img>
tag is a void element that is used to display images on a web page.
Void elements are an essential part of HTML and are used extensively in modern web development. They allow developers to include elements in their web pages that do not require content or child elements, such as images, input fields, and line breaks.
Purpose of Void Elements
The purpose of void elements is to provide a way for developers to include elements in their web pages that do not require content or child elements. This is useful because it allows developers to create more efficient and streamlined web pages.
Void elements are also used to provide metadata about a web page. For example, the <meta>
tag is a void element that is used to provide information about a web page, such as its author, description, and keywords. This metadata is used by search engines and other web services to provide context and relevance to the content on the web page.
Another purpose of void elements is to provide input fields for forms on web pages. The <input>
tag is a void element that is used to create text boxes, buttons, checkboxes, and other input fields that are essential for creating interactive web pages.
Usage of Void Elements
Void elements can be used in a variety of ways to enhance the functionality and design of a web page. The most common usage of void elements is to display images on a web page. The <img>
tag is a void element that is used to display images, and it has several attributes that can be used to control the size, alignment, and other properties of the image.
The following is an example of how the <img> tag can be used to display an image on a web page:
<img src="image.jpg" alt="Example Image" width="500" height="300">
In this example, the src
attribute specifies the URL of the image, the alt
attribute provides alternative text that is displayed if the image cannot be loaded, and the width
and height
attributes specify the dimensions of the image.
Another common usage of void elements is to provide input fields for forms on web pages. The <input>
tag is a void element that is used to create text boxes, buttons, checkboxes, and other input fields that are essential for creating interactive web pages.
The following is an example of how the <input>
tag can be used to create a text box on a web page:
<input type="text" name="username" id="username" placeholder="Enter your username">
In this example, the type
attribute specifies that the input field is a text box, the name
attribute provides a name for the input field that can be used to reference it later, the id
attribute provides a unique identifier for the input field, and the placeholder
attribute provides text that is displayed in the input field until the user enters text.
Void elements can also be used to create line breaks on a web page. The <br>
tag is a void element that is used to create a line break between two elements on a web page.
The following is an example of how the <br>
tag can be used to create a line break on a web page:
<p>This is the first line.<br>This is the second line.</p>
In this example, the <br>
tag is used to create a line break between the first and second lines of text.
List of Void Elements
Here is a list of the most commonly used void elements in HTML:
<area>
<base>
<br>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<param>
<source>
<track>
<wbr>