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

Jentry J
Jentry J
1,821 Points

This wasn't covered in the video, doest it matter if you add a closing bracket to self-closing elements?

Just curious if it really matters that it's self-closing, and if I added a closing bracket if it would actually effect the code?

** I will be attempting to try this on my own, but wanted to leave this for any other curious folks.

2 Answers

Steven Parker
Steven Parker
231,269 Points

If you mean "end tag" then yes, it would be a syntax error.

For example, if you wrote this:

<img src="dog.jpg" alt=""></img>

That's not valid HTML. But browsers generally ignore the spurious end tag, so trying this out may not generate any errors or unexpected rendering effects.

Jentry J
Jentry J
1,821 Points

Thank you, this is pretty much where my brain went! - Thank you Gif

The break take used to just be <br> but with the standards of xhtml they came out with <br /> which basically added a / at the end of any self closing tags such as the line break. Browsers probably still remember how to handle things without the / at the end but it's bad form to do that now a days.

Steven Parker
Steven Parker
231,269 Points

That's a DOCTYPE issue. If your DOCTYPE is html, then for void tags like "<br>" that form is correct. A slash at the end of the start tag is allowed, but has no meaning. But if the DOCTYPE is XHTML, then only "<br />" is correct.

Adhering to the standards of the declared DOCTYPE is the best practice, mixing them would be "bad form".

Steven, While it is bad form, it still works

<!DOCTYPE xhtml>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <p>Here is some text</p>
    <p>Here is some more</p>
    <br>
    <p>Oh look more</p>
  </body>
</html>

Still works just as if it was <br />

I know they left it working so people used to html before xhtml wouldn't have a ton of issues and I don't think they ever went back and fixed it. That said other than usin git in examples to show it works I've never used it without the / lol

Steven Parker
Steven Parker
231,269 Points

I don't think that's actually an xthml DOCTYPE tag, the document was probably being processed as html. Take a look at the W3C valid doctypes list.