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 trial

HTML How to Make a Website Creating HTML Content Add Image Gallery Content

Jamie Ross
Jamie Ross
302 Points

closing 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" />

Some html was stripped out of the question body. I've added the proper formatting if anyone needs to revise their answer.

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey 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! :)

My opinion always close tag.

Nathan Tallack
Nathan Tallack
22,160 Points

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

changed comment to answer

Jamie Ross
Jamie Ross
302 Points

Thank you everyone! :)