Self-closing tags in HTML
There are two types of HTML tags: conventional tags that have opening as well as closing tags, and self-closing tags.
What are self-closing tags in HTML?
Self-closing tags in HTML are special tags that don’t need closing tags. Void elements are another name for them. These don’t wrap around their contents like other elements do. They stay empty and fill themselves up instead.
In HTML5, self-closing tags can have or not have closing slashes. For instance, both
<br> and <br />work. In the past, slashes were needed in prasing XHTML and XML documents correctly. Now, HTML parsers can easily handle both syntaxes.
These tags follow simple rules and make structuring of html documents smooth. They make the coding shorter and cleaner. Browsers may show them the same way whether or not they have closing slashes.
List of Self-Closing Tags for HTML5
In HTML5, these are the self-closing HTML tags that are utilized the most:
- <br> – Line break
- <img> – Image
- <hr> – Horizontal rule
- <meta> – Metadata
- <link> – Linking stylesheets
- <input> – Input field
- <source> – Media sources
- <area> – Image map area
- <col> – Table column
- <embed> – Embed external resource
- <track> – Media text track
- <wbr> – Word break opportunity
Most of the self-closing tags you see on web pages are covered in this list.
How to Use Self-Closing Tags
It’s easy to use self-closing tags. You only write the opening tags and not the closing tags. You need to add closing slashes in XHTML 1.0. Today, in HTML5, browsers will accept both forms.
In HTML, the requirements for parsing rules are less strict. Browsers may not pay attention to extra slashes when they render. This keeps the structuring of documents while making them more adaptable.
Examples
These are some examples of self-closing tags:
<br>
<hr>
<img src=”image.jpg” alt=”Sample”>
<input type=”text”>
Many HTML documents utilize these self-closing tags. They use established syntaxes for structuring. Modern browsers will have no trouble parsing these.
Using self-closing tags, HTML keeps your coding tidy. They are very important for creating web pages that work.