38

So, I was learning about lists in HTML at college and the professor said <li> doesn't have an ending tag </li> along with some other tags like <img> and <br>. Is that correct or not? Because I've seen a lot of templates / themes using tag at the end, as well many sites also teach you that </li> exists, so I'm not sure whom to believe and what is correct way of using <li>?

The reason why I'm asking this is because last time we learned about <img> tag, he said that the alt="" attribute gives you a text displayed over the image when you hover the mouse, which turned it's only alternative text for broken images when I asked here and he was wrong for that case, which also made me wonder this as well.

Thanks.

user2699298
  • 1,446
  • 3
  • 17
  • 33
  • 12
    Your professor should be fired. `
  • ` is not a self-closing tag and needs a separate closing `
  • `. – j08691 Dec 12 '13 at 18:00
  • @Cthulhu so, what is correct? – user2699298 Dec 12 '13 at 18:00
  • `Is that correct or not?` — was answering this. – Cthulhu Dec 12 '13 at 18:01
  • 16
    @j08691: it's optional in most cases. The professor is correct. – user2736012 Dec 12 '13 at 18:02
  • 10
    @user2736012 Optional or not, a closing `` tag should never be considered 'optional' if you wish to write proper HTML code. – Anil Dec 12 '13 at 18:03
  • BTW the alt="" tag inside of the image tag WILL create mouse over tag, but not in all browsers. You should test all of you HTML in multiple browser because font and placement may vary. Just thought I'd throw that in for a little extra FYI. – Jeremy Johnson Dec 12 '13 at 18:04
  • 2
    It'll work, but that doesn't mean it's a good idea. – Tim Dec 12 '13 at 18:04
  • 14
    @SlyRaskal: That's your mere opinion. "Proper" HTML is determined by specification, not by your opinion. – user2736012 Dec 12 '13 at 18:04
  • 1
    @JeremyJohnson Tested in all browsers and only IE works for that. – user2699298 Dec 12 '13 at 18:05
  • The reason it will work either way is because HTML compliance and XHTML compliance are two different things. To find if your code is XHTML compliant, and therefore as correct as possible, use a code validator like this one: http://validator.w3.org/unicorn/ – Jeremy Johnson Dec 12 '13 at 18:05
  • @j08691 Which are the cases when it's not optional? I'm just curious. – user2699298 Dec 12 '13 at 18:05
  • 2
    alt="" is also capable of reading the text of the picture to visually impaired persons. This is why meaningful text is useful here and why it is required to validate properly. – Jeremy Johnson Dec 12 '13 at 18:07
  • @user2699298: There are specific rules you can read in the spec. I said "most cases" because I thought in the back of my mind that there may be specific rules with respect to intermingling with `` elements. This may not be the case. – user2736012 Dec 12 '13 at 18:07
  • Note: As discussed in [this related answer](http://stackoverflow.com/a/13232170/562906), you need to make sure that void tags (such as `br` and `input`) use only the shorthand (``) and do not use the full `` syntax, because it is actually invalid in HTML4 – sinelaw Dec 12 '13 at 18:09
  • 2
    @user2736012: I understand the spec, but there is also something to be said about writing *good* code that is easily readable. Not closing a tag is known to be a bad practice. – Anil Dec 12 '13 at 18:09
  • 2
    @SlyRaskal: Again, that's a matter of opinion. Can you give an example of harm that can arise from not closing an `
  • ` element? It's easily readable if one understands the technology one is using. Understanding the technology should be considered the "good practice".
  • – user2736012 Dec 12 '13 at 18:11
  • 3
    @SlyRaskal not writing a closing tag will also cause the page to fail to validate for XHTML compliance. This is not a problem with ALL browsers, but it WILL cause issues in others. It's right up there with using instead of the more correct . will not validate because it is not compliant with standards that are used to determine valid pages. It won't always cause a huge issue, but, then again, there is something to be said for good practice. It also helps others read your code. – Jeremy Johnson Dec 12 '13 at 18:11
  • @user2736012 the problem that it will cause is that it will not validate meaning that as browsers continue to change the problem could snowball. It's never guaranteed, but following XHTML is more correct because browsers are built around these specs. – Jeremy Johnson Dec 12 '13 at 18:12
  • @JeremyJohnson: Please give one example of one browser that can't properly interpret HTML4 with respect to elided `` tags. – user2736012 Dec 12 '13 at 18:14
  • 9
    Writing HTML to be XHTML-compliant just to make sure some obsolete browser that would theoretically ignore the doctype and parse the page as XHTML just doesn't make any sense. Such a browser would refuse to parse >99.9% of web pages anyway. – JJJ Dec 12 '13 at 18:15
  • @user2736012 you miss the point. Just because it does not demonstrate itself as a huge problem now doesn't mean that it won't. The reason for XHTML compliance is so that there is a more uniform standard so that browsers will behave more similarly. I know that most browsers will interpret this correctly (for now), but most will also correctly interpret , which is being deprecated and will likely no longer work in the future incremental updates to some browsers. If you want you webpage to be valid for longer and update it less, use XHTML standards – Jeremy Johnson Dec 12 '13 at 18:18
  • 3
    @JeremyJohnson `` is not and is not being deprecated, neither in lower case nor upper case. – JJJ Dec 12 '13 at 18:20
  • 3
    @JeremyJohnson No I'm not missing the point. You're saying that someday some browser could decide to only support the XHTML doctype and ignore the more common doctypes. You've fabricated this imaginary scenario to bolster your point, but it's not at all based on reality. – user2736012 Dec 12 '13 at 18:21
  • 2
    @user2736012 take from it what you will. Just trying doing web design that won't validate and see how long you keep clients. – Jeremy Johnson Dec 12 '13 at 18:22
  • 5
    Again, it does validate. It doesn't validate the XHTML doctype, and that is only relevant if you use that doctype. – user2736012 Dec 12 '13 at 18:24
  • 1
    Whilst we can argue over the simple technicalities of different specs, there is a more sinister side to this, and that's when programmers presume that there will be no closing tag and code only for that. I've just experienced (not for the first time either) a CMS system editing my (should be optional) closing LI tags and making whole new LI tags in their place, resulting in a ridiculous mess up of the content. Not good. – Darren Crabb Sep 12 '18 at 10:38
  • Well, I certainly learned something new today... I came here from the Angular documentation which has an example with an omitted – mukunda Mar 19 '23 at 06:31