Best Practices in Web Development

Roger Johansson and Sean Fraser recently reviewed websites which were submitted for the CSS Reboot Spring 2006, and they seemed to be quite shocked when 71.8% failed to validate. While this is sobering and to a degree surprising — one might expect better results from CSS aware developers on a relaunch — it confirmed my own results from reviewing a couple of high profile websites for clients.

In this article:

Case study

Over the years we made several technical analyses for the clients’ own websites and some of their competitors, including major German broadcasting companies, TV program journals, and a couple of large online travel sites.

We were checking for overall performance like file sizes as well as best practices in (X)HTML, CSS, semantics, and some other aspects of friendliness to users and search engines. We mostly skipped accessibility because there were already severe errors in the other areas.

It’s not like these websites were made by some backstreet crack houses agencies. All have a well-known logo stuck to them, most of them get more than a million visits each month, some up to more than a hundred million. Still the reviews felt like a time-travel to 1998. In our elite circles we speak about a “new professionalism,” but HTML in the wild is still a very dark and ugly jungle.

Most common errors were missing alt-attributes and unencoded &-characters in URLs. Also quite common were XML endslashes in HTML documents, or hacks from the nineties: proprietary attributes like background in table data, height in a table row, marginwidth, marginheight, topmargin and leftmargin, or missing type attributes in style and script tags.

Getting into the depths of old-school programming we found proprietary tags like <nobr>, <ilayer>, or <spacer>, misconceptions like valign="center" instead of "middle", forgotten closing tags, or tag soup like the classic form between table rows (to prevent top margins), block level elements inside of inline elements, <p> tags around everything (including tables and headlines!), or script tags before or after <html> </html>. Especially ad servers and site statistics were infamous for their crappy stone-age code.

Only a few were missing a doctype declaration (DTD) altogether, most ran HTML transitional 4.01 in quirks mode, only one claimed to be XHTML 1.1 (gasp). Seven out of ten had a charset encoding, but only the XHTML website did care to define the site’s language. Almost all had no clue about CSS positioning and used table layout.

Sometimes we encountered code where developers obviously hadn’t understood even the most basic principles of CSS 1. They used either font tags with style attributes, or other tags just like font tags with a lot of repeated inline style. Thus the advantages of CSS through central style sheets with classes and IDs were counteracted.

It didn’t come as a surprise that the few websites using CSS 2 for layout had significant smaller file sizes. Those with CSS 2 had between 15-21 KB of HTML, their competitors with table layout 55-95 KB. Still you got to pay attention to the file size of images and rich media (including banners) as they frequently blow up the total amount. Be aware of DHTML menus, as they have a tendency to get out of control sizewise (40k JavaScript in one case, 85k in another). Less than a total of 100k for the home page should be sufficient, even for sites with rich graphic interfaces.

I don’t want to bore you with further rants about burning bandwidth with 600k large home pages (multiply that by a million), hence we thought positive and made a checklist of best practices. Although the checkpoints appear to be quite obvious and common sense when you read them, you might be surprised that the best website managed to fulfil 22 out of 29 checkpoints, the worst only 8, with an average of 12.

By the way, the HTML and CSS part of the checklist is more or less the same we use for testing the qualifications of job applicants. We associated a severity grade (major, average, minor) with each checkpoint, and for each error in the small task we give them we subtract 3, 2, or 1 point(s) respectively from a maximum of 100. Half the amount is subtracted when a checkpoint is partially fulfilled. We adopted that system from the German accessibility test — it’s fair and a great way to compare test results.

Here’s the short version of our checklist. Feel free to make your own severity rating. Just be consistent in your own tests to get comparable results.

Best practices in HTML

  1. The document must validate as HTML or XHTML.
  2. The document must have a DTD.
  3. The document must have a character encoding, either through a HTTP header or a meta tag.
  4. The document should have the language defined in the following ways:

    • <html lang="en">
    • <html xml:lang="en">
    • <meta name="content-language" content="en">
  5. All attribute values should be quoted. In XHTML, quotes are mandatory.
  6. Element and attribute names should use a consistent case to enhance readability, preferred is lowercase for XHTML compability.
  7. Code should be well <!-- commented --> to help maintaining developers to get a fast grip of the basic structure and key concepts.
  8. Code should be consistently indented, tabs preferred over spaces.
  9. Images, image maps and scripted content must have an alt-attribute with an appropriate value.
  10. Images should always specify their width and height to allow browsers to continue rendering while waiting for the image data.
  11. Some special characters like &quot;, &lt;, &gt;, and &amp; (especially within attributes like href or src) must always be replaced by their character entities or numeric character references (NCR). Also characters not included in the character set, like &euro; in ISO 8859-1, must be replaced. The W3C I18N activity group further recommends to replace other special characters like &ndash;, but not common characters like “ü” in German texts or “é” in French to maintain code readability.
  12. script and style tags must have a type attribute.
  13. JavaScript code should be included using external files for caching and better maintenance.

    Of course with Yahoo’s performance research about browser cache and HTTP requests you could discuss if external files load faster with several server requests through script src="foo.js" or just one request and internal HTML code, but either way you should use central, non-redundant files and include them in one way or another.

Best practices in CSS

  1. CSS must validate.
  2. Elements should be positioned with CSS 2, not tables.
  3. All ornamental lines, borders, background colors and -images, underlines and other decorational elements should be rendered with CSS 2.
  4. Text should be formatted with CSS 1, not with font tags.
  5. All style sheets should be included in reusable, centrally maintainable, and cacheable external CSS files, not as redundant code within the HTML files.
  6. External CSS files should be sent with the correct MIME type (text/css).
  7. CSS classes and IDs must be valid, i.e. beginning with a letter, not a number or an underscore. IDs must be unique. Their names should be generic, describe functionality rather than appearance.

Best practices for search engines

  1. Use a descriptive title with the name of the website and consistent separating characters in it.
  2. Use keywords, keyphrases, and a description reflecting the page’s content.
  3. The URL should be descriptive, human-readable, easy to remember, to bookmark, and easy to spell on a phone. Don’t bother the user with file extensions, thus you will be future compatible and independent from your current software.
  4. Use the Robots Exclusion Standard, i.e. robots.txt.
  5. The content should be as high up in the code as possible as it gets more relevance there.
  6. Use proper semantic code, like headlines, or list items for anything that resembles a list. Don’t use list items to get bullet points or headlines to set bold text.
  7. Use a favorite icon for bookmarking and to keep your error log clean.
  8. Use the PICS standard to signal search engines and filter programs that your website is safe for kids.

12 Responses to ‘Best Practices in Web Development’

  1. Hasematzel.de

    Nach fast monatelanger Abstinenz lasse ich mich noch einmal in den Foren bei OpenBC.com sehen. Dort stolpere in Webdesign und Usability über einen Beitrag von Martin Kliehm, der auf einen Post im Blog learningtheworld.eu verweist, in dem er erneut das Thema Webstandard aufgreift.

    Ausgehend von der beachtlichen Menge nicht-validierender Seiten, die für den CSS Reboot ’06 gemeldet waren, führt er in einer übersichtlichen, aber detaillierten Liste die Maßnahmen an, die bei einem Projekt „Webseite“ Berücksichtigung finden sollten. Klasse Arbeit, Lesepflicht!

  2. accessBlog » Wochenendbeilage

    […] Martin Kliehm: Best Practices in Web Development (exzellente Zusammenfassung, daher ergeht Lesebefehl!) […]

  3. 70 Expert Ideas For Better CSS Coding | Smashing Magazine

    [...] CSS classes and IDs must be valid. “I.e. beginning with a letter, not a number or an underscore. IDs must be unique. Their names should be generic, describe functionality rather than appearance.” [CSS Best Practices] [...]

  4. Fredrik Holmstrom » Läsvärt AKA dagens länkskörd

    [...] Best Web-Practices [...]

  5. boozeheads.org » Boas práticas em HTML e CSS

    […] Trecho do artigo traduzido do blog Learning the World. Para acessar o artigo completo em inglês, clique aqui. […]

  6. css, php, dhtml и java Ñ?татьи и Ñ?крипты » 70 Ñ?поÑ?обов улучшить CSS код

    […] КлаÑ?Ñ?Ñ‹ и идентификаторы должны быть правильными. Имена клаÑ?Ñ?ов и идентификаторов должны начинатьÑ?Ñ? Ñ? буквы, идентификаторы должны быть уникальными, их имена должны отражать функциональноÑ?Ñ‚ÑŒ Ñ?лемента, а не его оформление. [CSS Best Practices] […]

  7. 70�专家谈CSS设计 | 一个全新的 WordPress Blog | PureCSS

    […] CSS classes å’Œ IDs å¿…é¡»å?ˆæ³•. “我们在定义这些对象的使用最好用他们的功能, 而ä¸?是他的外观â€? [CSS Best Practices] […]

  8. Help understanding DIV

    […] CSS classes and IDs must be valid. “I.e. beginning with a letter, not a number or an underscore. IDs must be unique. Their names should be generic, describe functionality rather than appearance.” [CSS Best Practices] […]

  9. 雨中人 » Blog Archive » 70ä½?专家谈CSS设计

    […] CSS classes å’Œ IDs å¿…é¡»å?ˆæ³•. “我们在定义这些对象的使用最好用他们的功能, 而ä¸?是他的外观â€? [CSS Best Practices] […]

  10. » Blog Archive » 70 идей Ñ?кÑ?пертов длÑ? улучшениÑ? CSS кода

    […] Классы и идентификаторы должны быть правильными. Имена классов и идентификаторов должны начинаться с буквы, идентификаторы должны быть уникальными, их имена должны отражать функциональность элемента, а не его оформление. [CSS Best Practices] […]

  11. 70 Expert Ideas For Better CSS Coding : CLB Webmaster Việt Nam

    […] CSS classes and IDs must be valid. “I.e. beginning with a letter, not a number or an underscore. IDs must be unique. Their names should be generic, describe functionality rather than appearance.â€? [CSS Best Practices] […]

  12. 70 идей экспертов для улучшения CSS кода - Design For Masters

    [...] Классы и идентификаторы должны быть правильными. Имена классов и идентификаторов должны начинаться с буквы, идентификаторы должны быть уникальными, их имена должны отражать функциональность элемента, а не его оформление. [CSS Best Practices] [...]