What is the difference between this html5 form elements? - html

I am learning HTML5, i want to know why these elements are closed differently the first input ends with > and the second ends with /> what difference does it make?
<input name = "howtosite" type = "radio"
value = "search engine" checked>
<input type = "color" autofocus />
(Hexadecimal code such as #ADD8E6)

Briefly, some terminology: Confusingly, "HTML" now means two things:
The definition of the various kinds of elements that make up what we use in web pages and such. This is what tells us that there is an element called div and what it's for.
One of the two serializations of it (the written form), which tells us we write div elements like this: <div>content</div>.
The other serialization of HTML is XHTML. The two serializations differ in places, because XHTML is XML.
HTML defines some elements that never have content, like <br>, and in the HTML serialization they're usually written just like that, <br>. In the XHTML serialization that's a problem, because XML requires that all tags be closed and <br> is just a start tag. Putting the slash ("solidus") just before the ending > closes the tag, so in XHTML, <br> becomes <br/>. The / is tolerated in the HTML serialization, but it serves no purpose. It only serves a purpose in XHTML. (Note that in really, really old browsers, you may need a space before the solidus, e.g. <br />, but we're talking very old indeed.)
This is only true for void elements like <br> and <input> that never have any content, and foreign elements (MathML and SVG). You never write <div/>, for instance, even if the div is going to be empty. The correct form of an empty div is always <div></div> (whether in the HTML or XHTML serialization).
Full detail in the specification, and in particular §8.1.2.1.
So regarding your two specific examples: The first is only valid in the HTML serialization. The second is also valid in the HTML serialization, and would be valid in the XHTML serialization if the autofocus attribute had a value (in XML, attributes must have a value, so you have to write autofocus="autofocus").

Related

Terminology - The types of elements in HTML

A while ago there was a term that I remembered that described two categories of elements. I forgot the term and I want to know what that term was. The information I can remember is that the first category of elements get their values from within HTML like <p> or <a> or <ul> but there is another category of elements which get their values from "outside" of HTML like <img> or <input type="textbox">. I want to know the terminology for these types.
Edit - I've went through Zomry, Difster and BoltClock's answers and didn't get anything. So I remembered some extra piece of information and decided to add it. The two categories are Lazy Opposites of each other. For example if one is called xyz, then the other is called non-xyz.
Probably you mean replaced elements (and non-replaced, respectively)?
However, the distinction between them is not so unambigous. For example, form controls were traditionally considered replaced elements, but the HTML spec currently explicitly lists them as non-replaced (introducing the "widget" term instead).
The HTML specification mentions for tags like <img> and <input> the following: Tag omission in text/html: No end tag.
Tags with an end tag are defined as: Tag omission in text/html: Neither tag is omissible.
So as far as I can find, the HTML spec does define a technical name for this, apart from void versus normal elements, so what Watilin pointed out in the comments should be fine: standalone vs containers.
As an added side-note: HTML has a lot more HTML content categories. You can find a complete overview at the HTML spec here: https://html.spec.whatwg.org/multipage/indices.html#element-content-categories
Also interesting to read to visualize that a bit better: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories
Elements whose contents are defined by text and/or other elements between their start and end tags don't have a special category. Even the HTML spec just calls them normal elements for the most part in section 8.1.2.
Elements whose primary values are defined by attributes and that cannot have content between their tags are called void elements. img and input are indeed two examples of void elements. Note that void elements are not to be confused with empty elements; see the following questions for more details on that:
Are void elements and empty elements the same?
HTML "void elements" so called because without content?
<input type="text" id="someField" name="someField">
With an input selector, you can get a value from it like so (with jQuery):
$("#someField).val();
Where as with a paragraph or a div, you don't get a value, you get the text or html.
<div id="someDiv">Blah, blah, blah</div> You can get that with jQuery as follows:
$("#someDiv").html();
Do you see the difference?

What kinds of HTML tags come in pairs and what not in pairs?

What kinds of HTML tags come in pairs and what not in pairs?
Wikipedia about HTML says:
HTML tags most commonly come in pairs like <h1> and </h1>, although some tags represent empty elements and so are unpaired, for example <img>
What does it mean by "empty elements"? <img> represents image embedding which isn't it nonempty element?
Thanks.
All tag come in pairs except for Void elements, which at the time of this writing include: area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, and wbr.
The Wikipedia page (as of now) is confused and confusing, and it uses old HTML terminology: the term “empty element” as used up to HTML 4.01 and various XHTML specifications was replaced by “void element” in HTML5. These terms are used to describe elements that cannot have any content, in the technical meaning for “content” as defined in HTML. When HTML appears as linear text, as serialized, using tags, this “content” is what appears between the start tag and the end tag (either of which may be implied for some elements). In the Document Object Model, “content” consists of the child nodes of an element that are element nodes or text nodes.
Emptyness does not imply invisibility. For example, <hr> is normally rendered as a horizontal rule. But the element is empty because its definition does not allow any content for the element
“Void element” is introduced in HTML5 by a list: area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, wbr. However, this is meant to mean the elements with content model that allows no content; the list follows from this. The definitions of these elements specify “Content model: Empty.”
An element is an empty/void element if it is declared so in an applicable HTML specification. The definition of each element indicates its content model (allowed content).
“Empty element” is (or was) an element with EMPTY declared content in the formal syntax. As such, it was simply a syntactic concept: an empty element cannot have any content (any elements or any text except whitespace) between the start tag and the end tag. According to HTML rules except XHTML, the end tag must be omitted (implied), whereas in XHTML, an empty element may be written either with the end tag present, e.g. <br></br>, or using a special syntax where a slash in the start tag makes it act as an end tag, too, e.g. <br/>. The latter is recommended in clause C.2 of XHTML 1.0.
HTML5 has two syntaxes (serializations, linearizations): classic HTML syntax and XHTML syntax. In the former, old HTML rules for empty elements apply to void elements, e.g. no end tag is allowed for <br>. However, for compatibility, a slash is allowed as in XHTML, e.g. <br/>, but it has no effect. In the XHTML syntax, all XML syntax rules apply, so <br> alone is fatally invalid (well-formedness error), and either <br></br> or <br/> must be used.

Why is an input tag not allowed directly within a form tag?

I just read the following at http://w3fools.com/#html_forms:
Non-block-level elements (such as <input>) are not valid directly inside <form> tags until HTML5.
I had never heard of anything along these lines, and every basic HTML tutorial I've seen seems to be just fine with putting input tags directly inside a form tag. So my question has three parts:
Is the above statement legitimate?
Why is this the case? (Was it simply an oversight, or were the creators of the HTML spec trying to prevent specific problems by creating this rule?)
What is the recommended way to construct a form with inputs? (Are we just supposed to create a div or a table directly inside the form tag?)
It's standards pedantics.
The statement is legitimate as far as the standard goes: in HTML 4.01, the definition for <form> specifies that it may only contain block elements or <script>. As far as what every browser in the world allows, it's fine.
I can only guess that they consider <form> to not be a layout tag at all, and they want all inline elements to be contained inside a block element.
Yes, you're supposed to place a <div>, <table>, <p>, or some other block presentational element inside the <form>.
The above statement is true. In HTML the <input> tag is not a valid element of the <form> tag. In order to make this validate, you need to enclose the <input> tag with either a <fieldset> or <div>. Which is demonstrated below.
<form action="/" method="post">
<fieldset>
Field: <input type="text" name="field" />
<br />
<input type="submit" value="Submit" />
</fieldset>
</form>
First of all I'd like to mention that it's not really surprising that HTML tutorials teach you to do things wrong – HTML was practically designed to accept any and every way of doing things. You can leave tags unclosed, you can nest them improperly and whatnot, which is one of the reasons I personally use XHTML.
That statement seems to be true, but because of how HTML is designed, it does not matter in practise. XHTML probably prohibits this.
Form isn't really a container of any sort. It seems like the creators of the HTML spec were fond of things like block-level elements you should wrap everything inside of. This is just my view on it, though, but as far as I've noticed, non-block-level elements shouldn't be used without a proper container for them.
It's exactly like you shouldn't put non-block-level elements in a <blockquote>. Block-level-elements are containers for other elements.
A div, a table – I think even a <p> does the thing here.
Well, according to the HTML 4.01 specification (specifically section 17.3), this is technically true. However, I don't know of any web browser that would actually give you a problem over it.

What do you call tags that need no ending tag?

There are HTML tags, such as <img />, <input /> and <button />, that need no ending tag (</img>, </input> and </button>). What is the term that describes this type of tags?
This syntax has a variety of names depending on what language you are using. The best way to find out what it is called is to look at the specification for the specific language.
HTML 4.x
I can't find any mention of this syntax in the HTML 4.x specification. It is not valid syntax.
HTML 5
In the HTML 5 specification the / character (called a SOLIDUS) is valid but has no effect for void elements such as <br />, <hr />, <img />, <input />, etc. and for foreign elements (such as SVG tags) it designates a start tag that is marked as self-closing. It is not a valid syntax for all other tags (such as <button /> mentioned in your question).
Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.
XML
According to the XML specification it is called an empty-element tag:
The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag.
XHTML
According to the XHTML specification it is called the minimized tag syntax for empty elements:
C.2. Empty Elements
Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="karen.jpg" alt="Karen" />. Also, use the minimized tag syntax for empty elements, e.g. <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents.
C.3. Element Minimization and Empty Element Content
Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p> and not <p />).
In general if you want to be precise I would recommend using the names as defined in the appropriate standard. Then if people aren't exactly sure what you mean they can look it up in the standard to find out. However if you don't want to use the name in the standard you are free to call it something else if you want. The important thing is that the people who communicate with you can understand you. I don't think anyone would misunderstand you if you used the term 'self-closing tag' for a tag in an XML document even if the standard officially calls it something else.
Thanks to Alohci for the HTML 5 reference.
The term is self-closing.
HTML tags can be of two types. They are
Paired Tags
Unpaired Tags
Paired Tags:
A tag is said to be a paired tag if the text is placed between a tag and its companion tag. In paired tags, the first tag is referred to as Opening Tag and the second tag is referred to as Closing Tag.
Example:
<i>This text is in italics. </i>
Note: Here <i> is called opening tag. and </i> is called closing tag.
Unpaired Tags:
An unpaired tag does not have a companion tag. Unpaired tags are also known as Singular or Stand-Alone Tags.
Example : <br> , <hr> etc. These tags does not require any companion tag.
Those tags are called "Standalone Tags". Standalone tags are having no closing tags in HTML
But in XHTML they have to be self closed by adding forward slash before the closing angular bracket
I've always called them Singleton tags!
Optional closing tags like p, li, etc.
It is also worth mentioning that several other tags besides those mentioned in the question don't need a closing tag: they just close automatically when something specified in the standard appears.
For example:
<body>
<p>abc
<p>def
</body>
is equivalent to:
<body>
<p>abc
</p>
<p>def
</p>
</body>
because the p closes both at:
the start of another p
when the parent of the p closes
This is specified at 12.1.2.4 "Optional tags" https://html.spec.whatwg.org/multipage/syntax.html#optional-tags but I don't think there's an actual name besides "an element with an optional closing tag".
See also: P-end-tag (</p>) is not needed in HTML
That is called a self closing tag
There are paired and unpaired tags.
Unpaired tags are opened and do not have to be closed. They stand alone.
<img />, <input /> and <button />
I know them as bachelor tags.
e.g. here: http://moodle.cs.huji.ac.il/cs09/file.php/67782/xml-intro.pdf (page 30)
I've called them self-closing, single tags and monotags, I don't know why I haven't adopted a single term though.
I've seen them referred to as singlet (Which is presumably a short form of single-tag)
This sort of Element is an empty element (since it does not contain anything, it just may have attributes). That's the correct way according to the specification, AFAIK. (If the Element is not empty, the Element consists of the opening tag, the closing tag, and the content inbetween.)
Those tags are also called "unpaired", "single", or "bachelor tags". The term "self-closing" I don't like because they don't close themselves any more than other tags, it's still you or your program that puts the "/>" in there.
According to here:
An element consists of a start tag, content, and an end tag.

HTML 5: Is it <br>, <br/>, or <br />?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I've tried checking other answers, but I'm still confused — especially after seeing W3schools HTML 5 reference.
I thought HTML 4.01 was supposed to "allow" single-tags to just be <img> and <br>. Then XHTML came along with <img /> and <br /> (where someone said that the space is there for older browsers).
Now I'm wondering how I'm supposed to format my code when practicing HTML 5.
Is it <br>, <br/> or <br />?
Simply <br> is sufficient.
The other forms are there for compatibility with XHTML; to make it possible to write the same code as XHTML, and have it also work as HTML. Some systems that generate HTML may be based on XML generators, and thus do not have the ability to output just a bare <br> tag; if you're using such a system, it's fine to use <br/>, it's just not necessary if you don't need to do it.
Very few people actually use XHTML, however. You need to serve your content as application/xhtml+xml for it to be interpreted as XHTML, and that will not work in old versions of IE - it will also mean that any small error you make will prevent your page from being displayed in browsers that do support XHTML. So, most of what looks like XHTML on the web is actually being served, and interpreted, as HTML. See Serving XHTML as text/html Considered Harmful for some more information.
I think this quote from the HTML 5 Reference Draft provides the answer:
3.2.2.2 Void Elements
The term void elements is used to designate elements that must be empty. These requirements only apply to the HTML syntax. In XHTML, all such elements are treated as normal elements, but must be marked up as empty elements.
These elements are forbidden from containing any content at all. In HTML, these elements have a start tag only. The self-closing tag syntax may be used. The end tag must be omitted because the element is automatically closed by the parser.
HTML Example:
A void element in the HTML syntax. This is not permitted in the XHTML syntax.
<hr>
Example:
A void element using the HTML- and XHTML-compatible self-closing tag syntax.
<hr/>
XHTML Example:
A void element using the XHTML-only syntax with an explicit end tag. This is not permitted for void elements in the HTML syntax.
<hr></hr>
In other words:
Invalid HTML 5: <IMG></IMG>
Valid HTML 5: <IMG>, <IMG/>
And while HTML forbids certain closing tags, xhtml requires them:
Invalid xhtml: <img>
Valid xhtml: <img></img> or <img/>
Other elements that are forbidden from having a closing tag in HTML:
Element
Valid HTML
Valid xhtml
AREA
<AREA>
<AREA></AREA>
BASE
<BASE>
<BASE></BASE>
BASEFONT
<BASEFONT>
<BASEFONT></BASEFONT>
BR
<BR>
<BR></BR>
COL
<COL>
<COL></COL>
FRAME
<FRAME>
<FRAME></FRAME>
HR
<HR>
<HR></HR>
IMG
<IMG>
<IMG></IMG>
INPUT
<INPUT>
<INPUT></INPUT>
ISINDEX
<ISINDEX>
<ISINDEX></ISINDEX>
LINK
<LINK>
<LINK></LINK>
META
<META>
<META></META>
PARAM
<PARAM>
<PARAM></PARAM>
The fact that HTML forbids certain closing tags, while xhtml requires them is xhtml's problem. If you're writing HTML, you follow the HTML rules.
At the same time, browers gave up trying to enforce the standards, because everyone gets it wrong. It's not obvious:
that </BR> is forbidden
that </P> is optional
and </SPAN> is required
And then xhtml came along, with its XML rule that every element must have a closing tag, and people just assumed that HTML was the same thing. So the standards gave up, and were later revised to throw up their hands to the reality.
XML doesn't allow leaving tags open, so it makes <br> a bit worse than the other two. The other two are roughly equivalent with the second (<br/>) preferred for compatibility with older browsers. Actually, space before / is preferred for compatibility sake, but I think it only makes sense for tags that have attributes. So I'd say either <br/> or <br />, whichever pleases your aesthetics.
To sum it up: all three are valid with the first one (<br>) being a bit less "portable".
Edit: Now that we're all crazy about specs, I think it worth pointing out that according to dev.w3.org:
Start tags consist of the following
parts, in exactly the following order:
A "<" character.
The element’s tag name.
Optionally, one or more attributes, each of which must be
preceded by one or more space
characters.
Optionally, one or more space characters.
Optionally, a "/" character, which may be present only if the
element is a void element.
A ">" character.
In HTML (up to HTML 4): use <br>
In HTML 5: <br> is preferred, but <br/> and <br /> is also acceptable
In XHTML: <br /> is preferred. Can also use <br/> or <br></br>
Notes:
<br></br> is not valid in HTML 5, it will be thought of as two line breaks.
XHTML is case sensitive, HTML is not case sensitive.
For backward compatibility, some old browsers would parse XHTML as HTML and fail on <br/> but not <br />
Reference:
http://www.w3schools.com/tags/tag_br.asp
http://en.wikipedia.org/wiki/XHTML
According to the spec the expected form is <br> for HTML 5 but a closing slash is permitted.
I would recommend using <br /> for the following reasons:
1) Text and XML editors that highlight XML syntax in different colours will highlight properly with <br /> but this is not always the case if you use <br>
2) <br /> is backwards-compatible with XHTML and well-formed HTML (ie: XHTML) is often easier to validate for errors and debug
3) Some old parsers and some coding specs require the space before the closing slash (ie: <br /> instead of <br/>) such as the WordPress Plugin Coding spec: http://make.wordpress.org/core/handbook/coding-standards/html/
I my experience, I have never come across a case where using <br /> is problematic, however, there are many cases where <br/> or especially <br> might be problematic in older browsers and tools.
XML requires all tags to have a corresponding closing tag. So there is a special short-hand syntax for tags without inner contents.
HTML5 is not XML, so it should not pose such a requirement. Neither is HTML 4.01.
For instance, in HTML5 specs, all examples with br tag use <br> syntax, not <br/>.
UPD Actually, <br/> is permitted in HTML5. 9.1.2.1, 7.
If you're interested in comparability (not compatibility, but comparability) then I'd stick with <br />.
Otherwise, <br> is fine.
Both <br> and <br /> are acceptable in HTML5, but in the spirit of HTML, <br> should be used. HTML5 allows closing slashes in order to be more compatible with documents that were previously HTML 4.01 and XHTML 1.0, allowing easier migration to HTML5. Of course, <br/> is also acceptable, but to be compatible with some older browsers, there should be a space before the closing slash (/).
If you are outputting HTML on a regular website you can use <br> or <br/>, both are valid anytime you are serving HTML5 as text/html.
If you are serving HTML5 as XHTML (i.e. content type application/xhtml+xml, with an XML declaration) then you must use a self closing tag like so: <br/>.
If you don't the some browsers may flat out refuse to render your page (Firefox in particular is very strict about rendering only valid xhtml+xml pages).
As noted in 1. <br/> is also valid for HTML5 that happens to be generated as XML but served as a regular text/html without an XML declaration (such as from an XSL Transform that generates web pages, or something similar).
To clear up confusion: Putting a space before the slash isn't required in HTML5 and doesn't make any difference to how the page is rendered (if anyone can cite an example I'll retract this, but I don't believe it's true - but IE certainly does a lot of other odd things with all forms of <br> tags).
The excellent validator at http://validator.w3.org is really helpful for checking what's valid (although I'm not sure you can rely on it to also check content-type).
<br> is sufficient but in XHTML <br /> is preferred according to the WHATWG and according to the W3C.
To quote Section 8.1.2.1 of HTML 5.2 W3C Recommendation, 14 December 2017
Start tags must have the following format:
…
After the attributes, or after the tag name if there are no attributes, there may be one or more space characters. (Some attributes are required to be followed by a space. See §8.1.2.3 Attributes below.)
Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.
If you use Dreamweaver CS6, then it will autocomplete as <br />.
To validate your HTML file on W3C see : http://validator.w3.org/
<br> and <br/> render differently. Some browsers interpret <br/> as <br></br> and insert two line breaks
<br/> is the most appropriate one. This tag notation can also be used in Reactjs where a line break is required instead of <br>
Most of the cases in HTML, the tags are in pair. But for a line break you don't need a pair of tags. Therefore to indicate this, HTML uses <br/> format. <br/> is the right one. Use that format.
<br> tag has no end tag in HTML
In XHTML, the <br> tag must be properly closed, like this: <br />
In XML every tag must be closed. XHTML is an extension of XML, hence all the rules of XML must be followed for valid XHTML. Hence even empty tags (nodes without child nodes) like should be closed. XML has a short form called self closing tags for empty nodes. You can write <br></br> as <br />. Hence in XHTML <br /> is used.
HTML is very lenient in this regard, and there is no such rule. So in HTML empty nodes like <br> <hr> <meta> etc are written without the closing forward slash.
HTML
<br>
<hr>
<meta name="keywords" content="">
<link rel="canonical" href="http://www.google.com/">
XHTML
<br />
<hr />
<meta name="keywords" content="" />
<link rel="canonical" href="http://www.google.com/" />
Not all tags can be self closed. For example, a tag like <script src="jQuery.min.js" /> is not allowed by XHTML DTD.
Well all I know is that <br /> gives a break with a white line and <br> just gives a break in some cases. This happened to me when I was setting up an IPN-script (PHP) and sent mails and checked the inbox for it. Dont know why but I only got the message to look neat using both <br /> and <br>
Have a look at the mail here: http://snag.gy/cLxUa.jpg
The first two sections of text is seperated by <br />, hence the whitespace lines, the last three rows of text in the bottom and the last section is seperated by <br> and just gives new row.
Ummm.....does anyone know a SINGLE vendor, user-agent, or browser maker that has ever followed the W3C Specifications 100%??? So if HTML5 says it supports all three break element versions, you can bet the vendors support the same and even more sloppier versions!
The ONLY thing that matters in this debate is to CONSISTENTLY use coding that also happens to follow XML specifications as well as HTML specifications when possible. That means you should use the correct XML version of the break tag and encourage all your team to do the same:
<br />
The same space-slash format should apply for the img, a, hr, and meta tags in your code. Why? Because:
Its is backwards compatible with older XHTML user-agents / browsers
The browser vendors support the XML version anyway so the HTML5 specification is moot.
The sloppy implementations of most user-agents today, in the past, and in the future will accept it.
It allows your markup to be comparable with XML standards should you need to go back to creating XHTML/XML documents from your markup.
It's "good coding practice" for ALL WEB DEVELOPERS to keep using solid markup practices that follow XML, including coding in all lower case, quoted attributes, escaped XML characters, etc. etc. Why? In the future if you have to switch to XML data you automatically code and think in XML.
We can only hope that in the future World Wide Web, we move away from private vendor-implemented standards and go back to solid, reliable, verified markup that parses faster, moves data over the wires faster, and make our future Internet a more standardized medium using XML.
Old Netscape always needed the " /" space before the slash or it failed. Who cares about old browsers, right? But its one more case for my version I still like :)
Besides, in the robotic and machine world that's here, where robots don't have the same Human-interface coding problems HTML5 solves for us, they will gladly go back to XML data systems and parse such UI web pages much faster when converted to XML data.
<br> and <br /> render differently in some browsers, so choosing either over the other isn't going to hurt your project, but do expect a bulk find..replace to affect the page render in some browsers, which may result in extra work for yourself or even embarrassment should the change affect nothing in your test browser, but break it in the preferred browser of your clients'.
I prefer <br> since it is what I have used since Erwise and Netscape Navigator (early web browsers), but there's no reason not to choose <br /> instead. It may be useful for some preprocessing, comparability, etc.
Even if your choice boils down to preferring the look of one over the other, or you (or your favourite HTML editor e.g. Dreamweaver) might like your code to be xml compliant. It's up to you.
A quick side note:
Not to be confused with br, but in addition you may also consider using wbr tags in your HTML: A word break opportunity tag, which specifies where in a text it would be ok to add a line-break.
For further reading, please have a read of the HTML5 spec.
The elements without having end tags are called as empty tags. In html 4 and html 5, end tags are not required and can be omitted.
In xhtml, tags are so strict. That means must start with start tag and end with end tag.