views:

130

answers:

5

The w3c validator doesen't like self-closing tags (those that end with "\>"). Are they still valid in html5?

Some examples would be:

<br \>
<img src="" \>
<input type="text" name="username" \>
+13  A: 

You have the wrong slash - />

Daniel A. White
A: 

You need to use forward slashes for the self-closing tags. My website uses self-closing tags in HTML5 content and the validators don't complain.

hasseg
A: 

Yes, they are valid as this:

<!doctype html> 
<html> 
    <head> 
    <meta charset="utf-8"/> 
    <title></title></head> 
    <body> 
    <hr /> 
    </body> 
</html> 

Validates fine:

http://validator.w3.org/check?uri=http://medero.org/x.html&amp;charset=(detect+automatically)&amp;doctype=Inline&amp;group=0&amp;user-agent=W3C_Validator/1.1

And as pointed out, use /> not \>.

meder
A: 

Not exactly.

If we get the syntax right (it is / not \) then:

In HTML 4, <foo / means <foo> (which leads to <br /> meaning <br>&gt; and <title/hello/ meaning <title>hello</title>. Browsers did a very poor job of supporting this and the spec advised to avoid the syntax.

In XHTML, <foo /> means <foo></foo>

In HTML 5, <foo /> means <foo>. The slash is just syntactic sugar for people who are addicted to XML. The syntax is valid, but it is not a "self-closing tag". The distinction is important since <div /> means <div> in HTML 5 and not <div></div> as it does in XHTML.

David Dorward
+1  A: 

Self-closing tags are valid in HTML5, but not required.

<br> and <br /> are both fine.

Nic