HTML, XHTML. What's the difference?

Published 24/05/2004 18:50   |    Updated 08/05/2008 18:00
HTML, XHTML - what's the difference?

In fact, XHTML is very similar to HTML - so if you've read the section on HTML, you probably already know some of what you need. The main difference is that it's much less forgiving than straight HTML as it demands much greater accuracy when you're creating your code.


If you miss out the odd closing tag in HTML, for instance, the browser will often ignore it. But not in XHTML. In XHTML everything has to be exact. For example, all tags have to be in lower case, all documents must be properly formed, and all tags must be closed.


That might sound like a nuisance, but it's actually a very good thing. It helps create higher quality, more refined code, and makes websites more accessible, as it can be viewed by more devices and more browsers.


And XHTML has other important advantages too. It's easier to maintain, easier to teach and easier to learn, for instance - mostly because its syntax is defined by XML, within which the rules are far more consistent than those in HTML.


Because of this, and all the other key reasons described, you might want consider learning to code in XHTML rather than just HTML from the very beginning.


Basic Rules


Every XHTML document needs a Doctype added at the beginning of the page (to tell the browser about the code the page is written in) - something else that's optional in HTML.


There are three.


Strict - The best option when you're using Cascading Style Sheets


Transitional - Used if you want to use presentational features of HTML in your page.


Frameset - Used if you want to include frames on your page.


Forming your document properly is crucial in XHTML.


Like HTML, XHTML has <html> <head> <title> and <body> tags but, unlike in HTML, they must all be included for the XHTML document to be valid and recognisable. The correct basic layout is as follows and your documents must adhere to it if you're working in XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">





<title>Dave's Homepage</title>

Other header information as required




All your body content






« The Basics: XHTML

The Basics: CSS »

Please tell us how we can make this answer more useful.