|
home—lects—hws
D2L—breeze (snow day)
xhtml : Even though original HTML doesn't require the following, they are best practices, and they are required for this class:
The most common (non-X)HTML example using improperly nested tags is <p>, which (in HTML) doesn't need to be closed; the browser auto-closes the tag when it reaches the next <p> (or more precisely: when it reaches the next block-level open-tag, or a close-tag which is terminating the enclosing block, e.g. if the paragraph was already inside an itemized list).
Browsers tend to go to great lengths to try to make sense of tag soup; just because a page looks fine in your browser doesn't mean it's legal. The result is that many people learned their HTML by doing a show-source on bad, illegal examples, and then propagating that bad HTML, and never realizing it because most browsers still did something reasonable (although the details of how a browser handled bad HTML are of course entirely non-standard).
Important: In HTML (and enforced by most modern browsers), you can only use the self-closing tag, for those tags which never are allowed a body. For tags that may or may not have a body (notably:script ), you cannot use the self-closing version even when you want an empty body. There is no good reason for this!
- <hr />— fine;
hr never has a body.- <img src="foo.jpg" />— fine;
img never has a body.- <script type="text/javascript" src="foo.jpg"></script>— fine.
- <script type="text/javascript" >callSomeFunction();</script>— fine.
- <script type="text/javascript" src="foo.jpg" />— BAD; a browser may ignore the tag entirely!, which is an annoyingly difficult bug to track down.
See also: xhtml.com/en/future/x-html-5-versus-xhtml-2/.
1A best practice…and yet: ugh! ↩
home—lects—hws
D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Feb.13 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |