Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJamie Ross
302 Pointsclosing image tag: <img> or <img />
The img tag: Are both of the following valid, with and without the closing slash?
<img src="image-name.jpg" alt="alt-description">
<img src="image-name.jpg" alt="alt-description" />
4 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Jamie,
The correct way is <img>. As stated in the MDN Documentation, It "Must have a start tag and must not have an end tag.".
Keep Coding! :)
Dalibor Bulic
8,728 PointsMy opinion always close tag.
Nathan Tallack
22,160 PointsIn HTML4 it was always a little messy because plenty of browsers did not follow the standards right.
I like how I saw it explained on a stackoverflow article a while ago...
In HTML 5, img slash means img, the start tag. It is not a "self-closing tag". Instead, certain elements are designated as having no end tag, for example br. These are collectively called void elements. The slash is just syntactic sugar for people who are addicted to XML. Using the slash in a non-void element tag is invalid, but browsers parse it as the start tag anyway, leading to a mismatch in end tags.
In XHTML, img slash means img img slash, but since this only works in XML parsing mode and most documents are served as text/html there are compatibility guidelines to follow.
Jason Anello
Courses Plus Student 94,610 Pointschanged comment to answer
Jamie Ross
302 PointsThank you everyone! :)
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsSome html was stripped out of the question body. I've added the proper formatting if anyone needs to revise their answer.