This forum is now read-only. Please use our new forums at discuss.codecademy.com.

3349 points
Picture
Submitted by
Whoos Hyx
about 7 years ago

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?


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.

1427 points
B7537692cec43c2d2a8d5e24f1e6c12b?s=140&d=retro
Submitted by
Will
about 7 years ago

1 Comment

85db2c409b6e54ae2a0c179d00c4e1e7?s=140&d=retro Michele Moneywell about 7 years ago

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.


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.

777 points
4ff2602540821e0003011691_427289663
Submitted by
Jerry Wang
about 7 years ago


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.

1185 points
85db2c409b6e54ae2a0c179d00c4e1e7?s=140&d=retro
Submitted by
Michele Moneywell
about 7 years ago


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."

1185 points
85db2c409b6e54ae2a0c179d00c4e1e7?s=140&d=retro
Submitted by
Michele Moneywell
about 7 years ago

1 Comment

C2449a109fbd80c9ec11a1e8e3fc96fe?s=140&d=retro abajan almost 7 years ago

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