Sobes.tech
Junior

Is it necessary to close single tags in HTML with />?

sobes.tech AI

Answer from AI

In HTML5, closing single tags (such as <img>, <br>, <hr>, <input>, <link>, <meta>) with /> is optional and considered bad practice. This is a legacy from XHTML syntax, where self-closing tags are required to ensure proper XML parsing.

In HTML5, the parser understands the document structure and tag context without the need for explicit closing of single elements.

Example:

<img src="image.jpg" alt="Beautiful image">
<br>
<input type="text" name="username">

You can use /> for compatibility with XML parsing, but it does not affect rendering in modern browsers operating in HTML mode.

Using /> is mandatory in XHTML and optional in HTML5, but it is not necessary for correct operation and is considered redundant in the context of modern HTML development.

It is recommended to follow conventions and avoid /> in HTML5 for code clarity and brevity, unless there are specific requirements for XHTML or XML parser compatibility.