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!

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 Introduction to HTML and CSS HTML: The Structural Foundation of Web Pages and Applications Image Tags

Is it okay if i still close the img tag with > ?

The video explains that an img tag, like a few others, don't need to be closed with a >.

Is it okay if I still close the tag with a >? or will it affect the output of the code.

3 Answers

Daniel Riches
Daniel Riches
19,847 Points

image tags should look like this: <img>

I think even <img/> might be okay, at least in xhtml

they are probably saying not to do this: <img></img> -- this is wrong. image tags don't need the closing tag, which is the second one: </img>

But you can experiment with the code and see what happens. the w3 html validator can always help with what is valid html too.

Yup, you are right. You can use <img src="source"> without the "forward slash / " or <img src"source" /> with "forward slash. <img> tag is a self closing tag, so it would be wrong to use "<img></img>".

Ah okay, ' What would happen if i closed the tag with </img> however? would it still work or would there be an error

Daniel Riches
Daniel Riches
19,847 Points

I just tried it with a very simple html page, and no visible error was thrown, but maybe it would cause problems. Maybe with more page elements or styling, JavaScript, or some kind of server rendered templates there would be bigger problems, or maybe SEO issues. Invalid HTML is possibly not always easy to find though, so using the w3 validator is a great practice, especially when just getting started.

Best to stick with the standards, and to have valid markup. Though I think experimenting is good since you will get familiar with what happens when things are done incorrectly. For instance if an error was thrown, then in the future you would know what that error meant if you came across it again.

It's a good question, I've never thought of it that way and didn't dig any deeper. Just followed what was recommended from the beginning. I did find a better explanation on StackOverflow -

`Historically, HTML has been based on SGML(Standard Generalized Markup Language) which allows tags to be omitted under certain conditions.

Since the <img> element cannot have any child nodes, it is defined as EMPTY and the end tag is forbidden (as it would serve no purpose).

XHTML is HTML expressed in XML, and XML does not support optional or forbidden tags (although it allows a self-closing tag to substitute for a start+end tag pair), so it has to be explicitly closed there.

HTML 5 is backwards compatible with versions of HTML that were SGML based.`

Hope that helps you ease your mind a bit ;).

Yes, it is perfectly fine. The <img> tag does not have a closing tag.