What is the difference/Better between -
< Some Code /> eg- <meta charset="UTF-8" />, <link href="style.css" type="text/css"/>
&
< Some Code > eg- <meta charset="UTF-8">, <link href="style.css" type="text/css">
What is the difference/Better between -
< Some Code /> eg- <meta charset="UTF-8" />, <link href="style.css" type="text/css"/>
&
< Some Code > eg- <meta charset="UTF-8">, <link href="style.css" type="text/css">
HTML isn't XML (except XHTML) so both are equivalent to most browsers.
From an XML perspective the />
is a self-closing tag. It's a short-hand.
<img src="..." />
<input type="text" />
<textarea>
</textarea>
These are all semantically correct and XML valid. HTML will usually validate (though it's no longer recommended) if you removed the ending from img and input.
<img src="..." >
<input type="text" >
<textarea>
</textarea>
This is semantically the same to HTML but is no longer XML valid.
Edit: To partially address the comments regarding textarea
below, input
and img
are tags which never have child elements. These would be syntactically invalid with them. That's why it doesn't really matter (with regards to HTML validation) whether they properly close. They never wrap children therefor the browser isn't looking for a closing tag. Conversely textarea
does have the ability to have child html (that's how rich text boxes are implemented in HTML) and needs a closing tag.
/>
is a self closing tag. Which would typically have attributes to specify its characteristics. And it also means that there's no possible child elements to this.
Example:
<input type=button/>
- here I am not expecting any child elements
></>
usually allows child elements.
Example:
<span><p>Hello</p></span>
- here span allows the child element(s)
So there's no concept of better one between these two.
/>
is used in self-closed element that does not have any child elements. Example:
<img src='bla.jpg' />
< >
always should be used with a closing tag </ >
e.g.
<h1>Hello</h1>
/>
is a self closing tag. Meaning its not followed by a closing element. For example, <a href="#"></a>
compared to <img src="#" />
. You need to know which tags are self-closing and which are not (you'll pick this up quickly): http://xahlee.info/js/html5_non-closing_tag.html
In HTML 5, it is valid to not have the closing slash on self-closing tags (<br>
). In xHTML you need to have the closing slash (<br />
): https://stackoverflow.com/a/3558200/703229