Writing Valid (X)HTML


Introduction

The implementation of valid/standard methods, guidelines and best practices will help to produce high quality websites that are accessible to as many as possible. By building valid websites you can save time and money for the development and provides a better experience for the visitor.

Validation is also an important part of web development. Many errors that are hard to find can discover during validation. This is like spell checking and proofreading for grammar and syntax in a document. but is much more precise and reliable than any of those processes because it is dealing with precisely-specified machine languages, not with nebulously-defined human natural language. You can validate your (X)HTML page as well as your CSS.

Here I’m not going brief you about Web Standards, but I’m going to tell how we can use those best practices in Web standards to produce a valid (X)HTML page.

Benefits of having a valid code

  • Future compatibility: Your website will compatible with future web browsers.
  • Simpler development and maintenance: Creating Valid (X)HTML Documents Means Cleaner Code and Easier Maintenance.
  • Faster download and rendering of web pages: Less (X)HTML results in smaller file sizes and quicker downloads. Modern web browsers render pages faster when they are in their standards mode than when they are in their backwards compatible mode.
  • Better accessibility: Semantic (X)HTML, where structure is separated from presentation, makes it easier for screen readers and alternative browsing devices to interpret the content.
  • Better search engine rankings: The separation of content and presentation makes the content represent a larger part of the total file size. Combined with semantic markup this will improve search engine rankings.
  • Simpler adaptation to any media or screen devices: A semantically marked up document can be easily adapted to print and alternative browsing devices, like handheld computers and cellular phones, just by linking to a different CSS file. You can also make site-wide changes to presentation by editing a single file.

Best Practices…

  • Include the correct DOCTYPE declaration at the beginning of the file. A DOCTYPE (short for “document type declaration”) informs the validator which version of (X)HTML you’re using, and must appear at the very top of every web page. DOCTYPEs are a key component of compliant web pages: your markup and CSS won’t validate without them.
  • The DOCTYPE is case-sensitive so use the correct case:
    <!DOCTYPE html PUBLIC "- //W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  • Add the attribute xmlns="http://www.w3.org/1999/xhtml" to the <html> element.
  • Close all elements: Omitting </p> is not valid anymore.
  • Change all elements to lowercase: <P> becomes <p>.
  • Correctly specify empty elements: <hr> becomes <hr />.
  • Quote all attribute values: <p align="right">.
  • Always add attribute values: <hr noshade="noshade" />.
  • Always use &amp; in place of &. : <a href="foo.php?chapter=1&amp;section=2 />
  • <form> element should have to have a action attribute always: <form action=”#”></li>
  • If you are using name & id attributes in input element, both should be in same name: <input name="field_name" id="field_name" />
  • Use alt attribute always for <img> element: <img src="image.gif" alt="" />
  • Use width & height attributes as in this order in <img> element: <img src="image.gif" width="100" height="100" alt="" />
  • Use a separate JavaScript file always & make sure you have put type attribute correctly: <script type="text/javascript" src=”javascript.js"></script>
  • Try to use a external CSS file for styles.
  • Always try to use tables only for TABULAR data.
  • Make sure you are using <th> for table headers & <td> for table data.
  • Elements in (X)HTML must be closed in the reverse order that they were opened in: <strong><em>…</em></strong>

(X)HTML Validation services

CSS Validation services

Accessibility Validation services

Useful Firefox Add-ons for validation services and web development

Related Posts

  • No Related Post

11 Responses to “Writing Valid (X)HTML”


  1. 1 John

    A useful tip is never to use MS Word at any point in the creation of the pages. You get stuck with a lot of MS specific tags.

  2. 2 Chamara

    Yeh MS is messing around when it comes to web. :)

  3. 3 Shyamali

    A verry useful posting, Cham… Tx a lot..!

  4. 4 Chamara

    You are welcome Shy!

  5. 5 Anandawardhana

    Thank you very much for posting this. Very useful information for me.
    By the way, is John’s comment relevant to Open Office as well?

  6. 6 Chamara

    You are welcome Anandawardhana! John’s comment is not relevant to Open Office.

  7. 7 shanX
  8. 8 shanX

    * code above got stripped :(

    use “& amp;” on URLs

  9. 9 webon

    Si estais interesados en el diseño grafico y diseño de paginas web,
    os recomiendo echar un vistazo a la web de Webografico estudios (estudio de diseño de barcelona)
    WEBOGRAFICO estudios
    Diseño gráfico y paginas web en Barcelona
    Gracias amigos y a disfrutar
    http://www.webografico.com

  10. 10 Jan from thrusites design

    Unfortunately, w3c valid pages don’t show the same way under different browsers.

    I would add enctype atribute to the “Best practises / Form” as well… It says which encoding should be used. If the site uses English, then it is OK. But if you work with other language and use SQL databases with the form, then some error might occur especially when weird characters were sent within the inserted text.

  11. 11 Chamara

    @Jan
    We can make those valid pages compatible with more browsers by using proper CSS with some creative thinking :)
    Yeh, it’s better to use UTF-8 as the charset of the HTML page.

Comments are currently closed.