A void element is an HTML element that does not have any content or closing tag.
Void elements, also known as self-closing tags or empty elements, are HTML elements that do not contain any content between an opening and a closing tag. They are defined by the HTML specification and are used to insert elements that do not require any text or child elements. Examples of void elements include <br>
, <img>
, <input>
, <meta>
, and <link>
.
These elements are self-contained and are typically used for their attributes rather than for content. In HTML, void elements are written with just an opening tag.
Example:
Here is an example of some common void elements in HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Void Elements Example</title>
</head>
<body>
<img src="image.jpg" alt="Example Image">
<br>
<input type="text" placeholder="Enter text here">
<link rel="stylesheet" href="styles.css">
</body>
</html>
In this example:
<img>
is used to display an image.<br>
is used to insert a line break.<input>
is used to create a text input field.<link>
is used to link an external stylesheet.