Differences between HTML and XHTML

posted by Kravvitz at 8:18 PM on Jul. 12th, 2010

Category: HTML 2 comments

XHTML is HTML reformulated as an application of XML. What that means is that you've got the same elements and attributes in both languages, but XHTML follows the stringent rules of XML. (XHTML inherits additional attributes from XML though.) So in practice, there are a few differences.

Requirements for writing XHTML markup:

  • Element names and attributes should be lowercase. (HTML is case-insensitive.)
  • Attributes should have a value. (In HTML certain — boolean — attributes, including "checked" and "selected" don't require a value.)
  • A pair of quotes, either single or double, should surround all attribute values. (HTML allows values that contain only certain characters to be unquoted.)
  • All elements must be closed (e.g. have an end tag). Empty elements, including <img> and <br>, should be self-closed like this: <img /> and <br />. (In HTML many end tags — and even some start tags — are optional.)
  • The xmlns attribute should be specified on the <html> start tag with the value "http://www.w3.org/1999/xhtml".
  • CDATA markers should be specified if "<" and/or "&" will be used inside <script> or <style> elements. (It's best to keep scripts and styles in external files anyway.)

One thing to keep in mind is that while in true XHTML documents all elements can be self-closed, most XHTML documents are served as HTML (with "Content-type: text/html") so HTML parsers are used. So to maintain compatibility, only elements that are empty by definition should be self-closed. One problem in particular arises if you try to self-close a <script> element (it causes major problems in IE). (Also empty <p>, <ul>, <ol>, and <li> elements are semantically unsound.)

More information:

#1 coinlyinoMe 9:12 PM on Feb. 27th, 2011

Very informative post. Thanks for taking the time to share your view with us.

#2 Roberta 4:23 AM on Sep. 13th, 2012

To think, I was confused a mintue ago.

Login or Register to post a comment