Is there a problem with not putting "/" (bar) in auto-closing tags?

Asked

Viewed 2,939 times

5

When I was learning about HTML, I read that there are two types of tags, those that are necessary to 'close', (<tag></tag>, ex: <div></div>) and those with self closure (<tag/>, ex: <img src="#"/>), but I came across an inconvenience (or not), which is the following, closing or not my auto-closing tags, they work the same way, for example:

This

<img src="#" />

It’s the same as that (note that I didn’t close the tag with the bar)

<img src="#">

The same can be repaired at tags br, input, etc..

My question is, what’s the difference between closing those tags? What risks I take?

2 answers

15


The idea of closing the tags without children comes from XML, in a proposal to unify the two languages by creating XHTML. However, as far as I know this has not gone forward - nor has this requirement been adopted in HTML5.

Thus, closing these tags is not mandatory, but is optional if you want to do it. I always do, by "aesthetic" even, but it is not necessary.

(It is worth remembering that if the tag is not one of those listed as "no kids", not closing is a mistake - although browsers "turn around" to interpret the content for you. But in this case, the closure should be given by a distinct closing tag. More details below)

Notes:

  1. Here it is the length of the specification that talks about this kind of tag. This question in the SOEN also gives more information.
  2. Also based on the above features, it is good to note that in HTML5 <elemento /> is interpreted as simply <elemento>, and not as <elemento></elemento> as would be done in XML. So if you want to create a tag without children - when this tag admits kids - don’t use that simple way.
    • More specifically, the / shall be admitted to types of element emptiness (void Elements) and foreign (Foreign Elements - elements that escape HTML, such as SVG or Mathml). In the former, it behaves as described, ignoring the /. In others, it behaves as in XML, closing the element. Using the / an element that is neither void nor Foreign - such as a div - is incorrect according to specification.
  • 2

    Just a complement to the above answer: in fact it is optional to put the closing bar in a self-closing tag (for example, <br />) but I find it interesting to differentiate self-closing tags from normal tags.

0

If you are using the XHTML with Java for example, Tags without closure need the /> at the end, as well as attributes some attributes that need to receive as their own name value. Already in HTML there is no such need, however it is a good practice to include yes closure, ex: <br /> <hr /> ...

  • Someone still uses XHTML, when HTML5 already supports tags void with /> and >? I believe that Java has no relation, even if I’m talking about some framework I’m almost sure that it should be in a more updated version that uses/supports HTML5.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.