Explain Codes LogoExplain Codes Logo

Do we still need forward slashes when closing void elements in HTML5?

html
responsive-design
best-practices
html5
Nikita BarsukovbyNikita Barsukov·Feb 6, 2025
TLDR

In HTML5, you can drop the self-closing slashes for void elements. Here’s what that looks like:

XHTML (the old school way):

<!-- "I need an ending... or do I?" Let's find out! --> <img src="path/to/image.jpg" />

HTML5 (the modern approach):

<!-- "Free at last, no slashes in sight!" --> <img src="path/to/image.jpg">

So, in a nutshell, the slash is optional in HTML5. Use the simpler, slashless syntax for a cleaner codebase.

A brief recap on void elements in HTML

Void elements or self-closing elements in HTML are unique as they don't require closing tags.

What are void elements?

These special elements, such as <img>, <link>, <meta>, and <br>, are self-standing, meaning they don’t wrap any content.

Slash: To be or not to be?

In the days of XHTML, the forward slash was necessary—part of the etiquette, as every element had to be 'properly closed'. However, in HTML5 this is no longer the case.

Cleaning up your code

By omitting the optional forward slash, you can achieve cleaner, clearer code—less is more, after all.

Validation for peace of mind

Not sure if your syntax is sound? Tools like validator.w3.org are your friends. They'll reassure you that skipping the slash on void elements isn't a code-crime. Both W3C and WHATWG confirm that the forward slash is indeed optional.

Code compatibility and modern syntax

Consistency is king

How you choose to write your code often depends on your team's preferences. Including the forward slash might harmonize with your team's coding style. But conversely, skipping it can create a more consistent HTML5 look throughout your project.

HTML5 meets XHTML

Working in a complex environment where your HTML5 might journey into XML content territory? Then, the self-closing syntax (slash and all) might be a life-saver. It ensures flexibility across various document interpretations.

When frameworks join the party

With the surge of JavaScript frameworks and templating languages, developers often use self-closing syntax for void tags, for that extra bit of readability. As they say, "readability counts."

Building future-proof and semantic code

The evolution of best practices

Best practices evolve just like our coding techniques. The current trend leans towards cutting ties with the seemingly unnecessary forward slash for void elements—embracing HTML5 specification in its entirety.

Embracing future HTML standards

Keeping your code close to current specifications can help to future-proof it. HTML keeps growing stronger, and sticking to the specs could mean smoother transitions to newer versions in the future.

SEO and self-closing slashes

Rumor has it that self-closing slashes mess with SEO performance. This couldn't be further from the truth. Rest assured that search engines can parse HTML just fine—slash or no slash!

Accessibility and HTML5

As a pro developer, you're undoubtedly concerned about accessibility. Here's the good news: omitting the forward slash is valid in HTML5, meaning no negative impact on accessibility with self-closing tags.