Who Are You Close to in Your Family
To shut or not to close
It is not, and has never been, valid HTML to write <br></br>. This would imply that the br chemical element accepts content (writing <br>Hi! has admittedly no meaning) However, information technology is very common to run into both <br > and <br /> in HTML5. The W3C says: Void elements are a special kind of element that must not have content. It seems very natural which is why most developers thought it was the right way to write it. Unfortunately the authors of the HTML4 specification didn't think and so. HTML5 makes a lot more sense.
Have you ever wondered if it's improve to "close" a br or input tag similar <br /> or if it'southward better to merely write <br> in HTML5? Or why it'southward not right to write <script src="script.js" />? Well so have I, and my findings on the subject were a lot more interesting than I anticipated (if for some foreign reason you find stuff like this interesting).
If you are not interested in the whole story, just jump to the department «Validity» to get your answer.
Void elements
Void elements are a special kind of chemical element that must not take content. That'due south a big difference to other elements that can exist empty only can likewise contain other elements and text (such every bit <div>s).
The most known void elements are:
<br> <hr> <img> <input> <link> <meta>
The lesser known are:
<area> <base> <col> <command> <embed> <keygen> <param> <source> <runway> <wbr>
That's information technology. Those are all of the existing void elements.
It is not, and has never been, valid HTML to write <br></br>, since this would imply that the br element accepts content (writing <br>Hello!</br> has admittedly no meaning). However, it is very common to see both <br> and <br />.
Although most people know that in XHTML information technology is mandatory to write <br /> the rules for HTML are less obvious.
History
To completely sympathise the rules of void elements a fleck of history is necessary.
HTML, XML and Ten(HT)ML are all based on SGML, the Standard Generalized Markup Language which has been crafted in 1986.
HTML and XML derived directly from SGML. XML is a more restrictive subset of SGML and that'due south what XHTML is based on.
XHTML is basically the same equally HTML just based on XML.
So far so skillful? And so lets become to the interesting role:
SGML has a characteristic called Internet (Cypher End Tag). This is a short notation to avert having to close a tag when the content of your chemical element is uncomplicated text. With NET you tin can write <quote/Quoted text/ instead of <quote>Quoted text</quote>.
As a side note, elements that do not incorporate any text, tin can be written as <quote// which is called SHORTTAG NETENABL IMMEDNET and is the aforementioned equally <quote></quote>.
Now, by that logic, if a void chemical element does not accept a closing tag, <br/ would be interpreted as <br>, and <br/> would be interpreted every bit <br>> which is obviously wrong syntax. If y'all're like me, you're probably thinking «This is insane!». Unfortunately the authors of the HTML4 specification didn't think then, which is why this is part of the specification. Apparently, the browser vendors at the time were not convinced also, which resulted in very poor browser support (which, in this case, is arguably non a bad thing).
XML (and thus XHTML) recognised the madness of such a syntax, and did not include the NET or the SHORTTAG NETENABL IMMEDNET «features», but provided a sane syntax for void elements, namely the Empty-Element tag that looks like this: <br />. It seems very natural which is why most developers thought it was the correct mode to write it.
Luckily HTML evolves and the people at the World wide web Consortium (who are drafting and setting the standards throughout the web) are learning from their by mistakes also. Which is why HTML5 makes a lot more sense.
Right in the introduction of the new HTML5 syntax, the W3C says:
HTML 5 defines an HTML syntax that is compatible with HTML four and XHTML 1 documents published on the Web, but is not compatible with the more esoteric SGML features of HTML iv, such as the NET syntax (i.e. <em/content/).
Yay for HTML5!
(I think they should accept kept the cool SHORTTAG characteristic (<potent>Hell yea</>) but hey… at least HTML is not a complete mess any more)
Validity
So back to the question of validity, the current HTML5 specification for void elements is every bit follows:
Commencement tags consist of the following parts, in exactly the following order:
- A
<graphic symbol. - The element's tag name.
- Optionally, one or more attributes, each of which must be preceded by ane or more space characters.
- Optionally, one or more space characters.
- Optionally, a
/graphic symbol, which may be nowadays only if the element is a void element. - A
>character.
This means that the / character has been rendered optional in HTML5, but it doesn't add together any meaning. At that place is absolutely no deviation betwixt <br> and <br />.
Correctness
Well, for those of you who are really addicted to X(HT)ML, you might remember, «yeah, information technology's optional, but <br /> is notwithstanding 'more right'», but I take to tell y'all: it is not. Actually, one might debate that adding / to a void tag is an ignored syntax error. The possibility to write it has generally been added for compatibility reasons and every browser and parser should non handle <br> and <br /> any differently.
Google's styleguide on that subject is also very clear that y'all should indeed not close void tags.
Theoretical disadvantages
Of course, not closing void tags has its disadvantages as well, but I recollect that they exercise not outweigh the advantage of having make clean and terse void tags like <meta>.
The first disadvantage of not endmost void tags is that users need to have noesis of the existing void tags. If, for example, you lot don't know what an <img> element is, you might be confused if you can't find any closing tags for it. Simply the list of void tags is very brusk and normally it'due south quite obvious which tags are void tags.
The second disadvantage is that it gets more than complicated for editors and parsers to get it correct. They are required to know the complete listing to properly translate the code. If yous write <input> in an editor, it has to know that at that place will never be a </input> following that.
But this is more an upshot related to the specification than to closing or not closing void tags, because <input></input> is invalid, so void elements crave special treatment in any example.
My stance on void tags
I think that the whole concept of void tags could be avoided completely by using the content of some tags instead of defining additional attributes. Permit'south take the <img> tag for example. It has a mandatory alt attribute to add a description of the image, and for good reason: people who tin't see the image (either because they are physically incapable or because their device can't display images) should have a way to know what is depicted (and if you're adding an img tag solely for design purposes then you're doing something wrong anyway). So my question is: why isn't the content of the image tag the alternative tag? Information technology seems rather obvious to me to write <img src="doge.png">Epitome of doge</img>. The aforementioned goes for <meta> tags which even have a content aspect! Why not just use the bodily element content for that? <input value="Value content"> should be <input>Value content</input> every bit is the case with <textarea>, etc…
So really there are only a few void tags that should exist anyway, merely plain the W3C has to take backwards compatibility into account which makes changes of this kind much more than difficult.
Terminal thoughts: the <script> tag
The script tag has really been bothering me because it is such a verbose tag for such a simple directive. It seems wrong to write <script src="my-script.js"></script> since the content of this script tag has no logical correlation to my-script.js (and the html specification disallows to add both, content and the src aspect).
The trouble is, that <script> is not a void tag since yous can inline JavaScript on your page and in that location are no "optional void tags".
Using the <link> tag would take been perfect since information technology's already used for other imports and provides all the attributes necessary to include external files. Of course, every bit then often in the web, the reason it is not used is backwards compatibility, since y'all would exclude all onetime browsers that don't support that syntax.
Tags
Related Stories
whitakerbeyea1975.blogspot.com
Source: https://hackernoon.com/to-close-or-not-to-close-4365d24853ff
Postar um comentário for "Who Are You Close to in Your Family"