0 points
Submitted by Whoos Hyx
over 9 years

closing input tags with /> or just >???

Should input tags look like

<input type="text" name="example" /> or <input type="text" name="example">

The exercise (Getting Input Section 2 Pt 2) says they should close with a /> but each one I see closes without the slash. I know from my geocities days that tags close with a slash but this course has taught me that a lot of standards have changed since then! So which one is the correct way (in real life, not just to pass codecademy), and why is the lesson giving conflicting information?

Answer 4ffe6cd02363760003010f14

3 votes

Permalink

Realistically, the validity would depend on which doctype you declare. In this context, /> is valid with HTML 5 and XHTML doctypes and invalid with HTML 4.01 doctypes.

This stackoverflow question has more detailed information about exactly what’s going on in your browser as well as links to the official W3C documentation.

points
Submitted by Will
over 9 years

1 comments

Michele Moneywell over 9 years

That is a good reference link. It says that for elements that don’t have a valid closing tag, then /> is allowed. So your comment about it being invalid for HTML 4.01 is incorrect.

Answer 500a185bd796760002017cf3

0 votes

Permalink

I am also confused. In 2.2, the instruction says:

There is an input field with a type of “text”. Notice that the input field doesn’t have a closing tag. Instead it ends with a space and a forward slash.

but in the sample code, there is no such space and forward slash. The instruction is not consistent with the sample code.

points
Submitted by Jerry Wang
over 9 years

Answer 50190ace48a538000201bcd5

0 votes

Permalink

It is a minor error on the course designer’s part to say there is an /> and then forget to include it.

In older versions of HTML, there are tags that don’t need a matching end tag.
is one and is another.

As time went by, many in the HTML community felt that every HTML tag should have a corresponding ending tag. If no ending tag exists, then instead of just ending with an >, end with />
That is why you see some people using “
“ just like that, and others coding it
Some code <input > and others would code that <input /> Bottom line is both ways work. I’m going to go with the />’s myself because I like there to be an “ending” tag.

points
Submitted by Michele Moneywell
over 9 years

Answer 50190f7acb8620000201b5e7

0 votes

Permalink

From the reference link provided by Will (first reply post):

“…a self-closing div will not validate. This is because a div is a normal element, not a void element. According to the spec, tags that cannot have any contents (known as void elements) can be self-closing*. This includes the following tags:

area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr The “/“ is completely optional on the above tags, however, so is not different from , but is invalid.”

points
Submitted by Michele Moneywell
over 9 years

1 comments

abajan about 9 years

I think the “/“ may or may not be optional depending on the doctype.