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
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.
1 comments
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
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.
Answer 50190ace48a538000201bcd5
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.
Answer 50190f7acb8620000201b5e7
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.”
1 comments
I think the “/“ may or may not be optional depending on the doctype.
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons