-2

IIRC single HTML tags like input can be written as <input> or <input />. I'm just wondering which form is considered "proper" HTML.

1
  • 3
    These are called "self-closing tags". Questions about markup are off-topic for Super User, but you can read more about this by looking around on Stack Overflow, where they've been discussed before.
    – nhinkle
    Sep 1, 2011 at 21:27

3 Answers 3

2
  • HTML 4.0: Empty elements only have a start tag, as in SGML.

    <input>

    Most browsers will accept XML-style self-closing tags.

  • XHTML, any version: Empty elements must be self-closing or have an end tag, as in XML. Self-closing tags are recommended.

    <input /> – recommended for compatibility

    <input></input> – valid XML but may be incompatible with HTML4-only browsers

  • HTML 5.0: Void elements only have a start tag, but a slash is allowed and ignored.

    <input>

    <input />

3
  • Wow, the lack of standardization is terrible. I guess the best thing would be to use self-closing and hope no one uses HTML 4 only browsers.
    – darkfeline
    Sep 1, 2011 at 21:34
  • @darkfeline Yep :) HTML was a mess, after all it became popular in the 90s hence all kinds of pages looking different in different browsers. If you wish to code toward standards take a look at w3schools. Including a type and making sure you adhere to standards is big help and will make your code more cross browser compatible. Microsoft is even building new browsers toward w3 standards (FINALLY!) but they are still a ways off. Use strict XHTML for best results though.
    – Jeff F.
    Sep 1, 2011 at 21:37
  • 1
    @Jeff Noooooo w3schools! They're the last site I would suggest ... see w3fools why.
    – slhck
    Sep 2, 2011 at 6:51
3

See the HTML 5 specification:

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.

So if you are using one of the defined void elements of the HTML syntax, you can omit the / character. I usually put it for readability purposes, so you don't look for a corresponding closing tag. However, both versions are valid.

2

<input /> is considered proper, the reason is because input doesn't require multi line attributes.

<textarea> needs a </textarea> as it's basic body text is inside the tags, <input> has a value attribute so the tag can be be closed in one.

1
  • Depends on if you are coding in HTML or XHTML. <input> is considered proper for HTML but the web is moving to XHTML and thus <input /> is preferred.
    – Jeff F.
    Sep 1, 2011 at 21:35

Not the answer you're looking for? Browse other questions tagged .