10 Tips for Building Cross-browser Websites

10 useful tips to ensure your Web pages look good across all browsers and attract more visitors.

In theory, building a Web page is simple: Throw together some HTML, test it in your browser, and upload. HTML and CSS are standards, so a page tested in one browser should look the same in all other browsers.

In practice, of course, things aren't that simple. Different browsers display HTML and CSS in subtly different ways, while some browsers (we won't name names) veer wildly from the standards.

It's best to build your Web pages so that they look consistent across a range of browsers. This is often known as cross-browser (or multi-browser) coding.

While there's no "magic bullet" that will make a site look great on all browsers, there are a number of things you can do to improve your chances. Here are ten useful tips for cross-browser coding.

1. Keep it simple

The more complex your markup and CSS, the more there is to go wrong. Keep your layout simple at the design stage: a header, a couple of side-by-side columns, and a footer. If your design has 7 free-floating sidebar boxes then you're creating headaches for yourself down the line.

Don't use too many nested elements in your markup, and use the right element for the job. Use ul and li elements to create a menu; don't use a table or a series of p elements.

Place all visual formatting in your CSS. Your HTML should be for content, not style.

2. Validate your code

Run your HTML and CSS through validators before uploading your site. Valid code might not make your page magically work in all browsers, but invalid code can produce all sorts of unpredictable and hard-to-debug problems.

The W3C HTML Validator and CSS Validator are good places to start. You can also use browser plugins, such as the Html Validator Firefox add-on, to check pages automatically while you're viewing them in the browser.

3. Avoid browser quirks modes

Many browsers feature a "quirks mode" that emulates an older, buggier version of the browser. This allows older Web sites to work with modern browsers without having to recode their pages. However, quirks modes can cause headaches if you're building a modern, standards-compliant page, because a browser in quirks mode will render your page in its own unique, non-standard way.

Generally speaking, a browser switches to quirks mode if your page doesn't include a DOCTYPE (document type declaration), so to avoid quirks mode, make sure each page of your site includes a valid DOCTYPE. More details in this Quirks mode Wikipedia article.

4. Use CSS reset rules

Many browser engines have slightly different default values for things like line height and padding around elements. Since these defaults can affect other CSS rules, this can be a real pain. You've probably lost count of the number of times you've had to add margin: 0; padding: 0; to a CSS rule to make it consistent across all browsers.

To avoid this hassle for each and every CSS rule you code, you can instead do all this resetting at the start of your CSS file (or in a separate file). Eric Meyer has put together a bunch of CSS rules to do just that. He also lists his reasons for using CSS resets. Once you've included these rules at the start of your style sheet, you can be reasonably sure that you're working from the same start point across all browsers.

5. Develop in Firefox

Most Web coders tend to test their site in one browser as they develop the site, then test in other popular browsers (and tweak as necessary) towards the end of the job. This is a good approach, because it's quicker to test in one browser as you go than five.

Personally I'd recommend testing in Firefox as you build your site, then when you're happy with it, testing the site in other browsers — particularly Internet Explorer — to fix any compatibility issues. Firefox is very developer-friendly (thanks to its large range of add-ons) and is fairly standards-compliant. If your site looks good in Firefox then it'll probably look pretty much the same in Opera and Safari.

No doubt your Firefox-tested site will have a few issues in IE, but in my experience it's better to write standards-compliant HTML and CSS that works in Firefox, then tweak for IE later. If you develop your site using IE then you'll end up coding for all of IE's little foibles, which will mean a lot more work later when it comes to tweaking your site for other browsers.

6. Test in as many browsers as possible

Since pretty much all modern browsers have their little rendering quirks, it's important to test your creations across a wide range of browsers. Here's a suggested minimum at the time of writing:

  • Internet Explorer 6, 7 and 8
  • Firefox 3
  • Safari 3
  • Opera 9

Testing in all 3 versions of Internet Explorer can be a challenge, as only one version of IE can theoretically be installed on one copy of Windows. There are a number of ways round this problem:

  • Have 3 PCs for testing. You can then install IE6 on one PC, IE7 on another, and IE8 on the third. Obviously space or money might make this impractical.
  • Hack IE6, 7 and 8 to work on one copy of Windows. There are various programs that can help you install multiple versions of IE on one copy of Windows. For example, check out TredoSoft's Multiple IE and Utilu's IE Collection. However, doing this can make your Windows setup unstable, and the different versions of IE can behave in strange ways, so this approach is not for the faint-hearted.
  • Use virtualization. A nice approach is to use virtualization software such as VMware or VirtualBox to run 3 separate copies of Windows on one PC or Mac. You can then install a different version of IE on each Windows copy. This results in stable, independent versions of IE. The drawbacks are cost (3 Windows licenses needed) and performance (you'll need a powerful computer to run 3 versions of Windows plus your host operating system).

    One way round these drawbacks is to use the snapshots feature of software such as VMware. Install XP with IE6 and take a snapshot; upgrade to IE7, take a snapshot; upgrade to IE8, take a snapshot. Now you can flip between IEs by reverting to your 3 snapshots, all while using one Windows licence.

  • Use an online testing service. Sites such as Browsershots and Browsercam let you capture screenshots of your site on a wide variety of browsers and operating systems. The screenshots are then displayed in a Web page for download, or can be emailed to you. Sometimes you have to pay to use these services, and the screenshots can take a while to come through, but it does let you test on practically any browser under the sun without needing to have it running locally.
  • 7. Fix IE issues by using conditional comments

    Sometimes, no matter how hard you try, it's impossible to make your otherwise-beautiful site work in Internet Explorer without resorting to some serious CSS and/or JavaScript hacks. However, there is an alternative. By including IE-only markup with conditional comments, you can serve up additional HTML, CSS or JavaScript just to IE, allowing you to work round any issues. Much cleaner than using hacks.

    8. Make IE6 work with transparent PNGs

    Transparent PNG images are wonderful things — they let you have up to 256 levels of transparency in an image, which means you can do neat tricks like translucency and smooth borders on any type of background. However, Internet Explorer 6 doesn't properly display transparent PNGs by default, instead displaying ugly boxes where the transparency should be.

    The good news is that, with a bit of tweaking, you can get transparent PNGs to work perfectly in IE6, without having to make major changes to your markup or CSS. A good solution is IE PNG Fix from Angus Turnbull.

    9. Provide fallbacks

    While most Web browsers support Flash and JavaScript, and nearly all support images, it's a good idea to provide fallbacks in case these features aren't available on a visitor's browser. For example, I've seen many sites with Flash navigation menus that are completely unusable on browsers that don't support Flash (such as Mobile Safari on iPhone).

    • Images: Make sure that each img tag contains an alt attribute describing the image.
    • Flash movies: Don't use Flash for essential stuff like site navigation — or, if you do, provide a non-Flash alternative.
    • JavaScript: Again, try to avoid using JavaScript for essential functionality. Using it to make things easier or faster is fine. Use the noscript element to provide a non-JS alternative (or at least to display a message asking the visitor to turn on JavaScript).

    10. Code by hand

    This last one's a bit controversial. Many folks these days use WYSIWYG editors like Dreamweaver and KompoZer. Such editors can save a lot of time and are good if you don't know HTML. However, there's a bit of an art to creating flexible, elegant, cross-browser HTML, and page editors don't always do a great job of it.

    My personal preference is to code all HTML, XHTML, and CSS by hand. That way I have full control over what I'm creating, and can easily fine-tune the code as necessary to work in a range of browsers.

    If you find coding by hand tedious, there are many excellent text-based Web page editors out there to make your life easier. Take a look at CoffeeCup (Windows) or Coda (Mac).

    Wrapping up

    It's worth taking a bit of extra time to make your pages work across as many browsers as possible. By doing so, you not only increase your audience reach, but you also keep your visitors happy, which is always good!

    Follow Elated

    Related articles

    Responses to this article

    There are no responses yet.

    Post a response

    Want to add a comment, or ask a question about this article? Post a response.

    To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

    Top of Page