WebsiteTips.com - Web Design Tutorials, Tips, Top Resources

Ten Ways to Speed Up the Download Time of Your Web Pages

by Trenton Moss

Published November 2004. Copyright © 2004 Trenton Moss. All rights reserved. Published at websitetips.com with permission.

Why is Download Speed So Important?

Do you like to wait for pages to download? Neither do your site users. Read on and find out how to speed up that download time...

1. Lay Out Your Pages with CSS, Not Tables

CSS downloads faster than tables because:

To learn more about CSS and the amazing things it can do for your website, check out our CSS resources area.

2. Don't Use Images to Display Text

It's our old friend CSS to the rescue again. There's no need to use images to display text as so much can be accomplished through CSS. Have a look at this code:

a:link
{
color: #ffffff;
background: #ff9900;
text-decoration: none;
padding: 0.2em;
border: 4px solid;
border-color: #99f #008 #008 #99f
}

a:hover
{
color: #ffffff;
background: #ffaa11;
text-decoration: none;
padding: 0.2em;
border: 4px solid;
border-color: #008 #99f #99f #008
}

This will give you a really simple button that appears to be pushed down when you mouseover it - See it in action if you like. Have a look at how else you can use CSS to make navigation buttons.

3. Call Up Decorative Images through CSS

It's possible to present images as part of the background, called up through CSS. If you've got an image that's 200px by 100px you can use the following HTML code:

<div class="pretty-image"></div>

And this CSS:

.pretty-image
{
background: url(filename.gif);
width: 200px;
height: 100px
}

This may at first seem a little pointless but this technique could really improve the download time of your pages. Browsers basically download background images after everything else. By using this technique, your text will load instantaneously and your site users can freely roam about the page while your 50kb fancy image downloads.

This technique disables the ALT attribute though so if you really want to have one then replace the above HTML code with this:

<image src="spacer.gif" class="pretty-image" alt="description" />

Spacer.gif is a 1px x 1px transparent image. Now you have a 50 byte transparent image and the main content loading first, and your great big decorative image, complete with ALT text, loading second. Perfect.

Please note that this technique is only good for decorative images and not informational ones. Any user who has CSS disabled will not be able to see your CSS-embedded images (or their alternative text).

4. Use Contextual Selectors

This is inefficient:

<p class="text">This is a sentence</p>
<p class="text">This is another sentence</p>
<p class="text">This is yet another sentence</p>
<p class="text">This is one more sentence</p>

.text
{
color: #03c;
font-size: 2em
}

Instead of assigning a value to each individual paragraph, we can nest them within a <div> tag and assign a value to this tag:

<div class="text">
<p>This is a sentence</p>
<p>This is another sentence</p>
<p>This is yet another sentence</p>
<p>This is one more sentence</p>
</div>

.text p
{
color: #03c;
font-size:2em
}

This second CSS example basically says that every paragraph within class="text" should be assigned a colour value of #03c and a font size of 2em.

At first glance this doesn't look too important, but if you can apply this properly throughout your document you can easily knock off 20% of the file size.

You may have noticed the colour values are shorter than normal. #03c is a shortened version of #0033cc - you can assign this abbreviated value to any colour value like this.

5. Use Shorthand CSS Properties

You can use the following shorthand properties for the marginCSS command.

Use:

margin: 2px 1px 3px 4px /* top, right, bottom, left */

...instead of

margin-top: 2px;
margin-right: 1px;
margin-bottom: 3px;
margin-left: 4px

Use:

margin: 5em 1em 3em /* top, right and left, bottom */

...instead of

margin-top: 5em;
margin-right: 1em;
margin-bottom: 3em;
margin-left: 1em

Use:

margin: 5% 1% /* top and bottom, right and left */

...instead of

margin-top: 5%;
margin-right: 1%;
margin-bottom: 5%;
margin-left: 1%

Use:

margin: 0 /* top, bottom, right and left */

...instead of

margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0

These rules can be applied to margin, border and padding.

You can find out more about CSS shorthand properties in our aptly titled article, CSS Shorthand Properties.

6. Minimise White Space, Line Returns and Comment Tags

Every single letter or space in your HTML code takes up one byte. It doesn't sound like much but it all adds up. We've found that by working through your page source and eliminating unnecessary white space and comments, you can shave off up to, or even over (if your HTML is really inefficient) 10% of its file size.

7. Use Relative Call-ups

Try to avoid absolute call ups as they take up more space. An example of an absolute call up is: <a href="http://www.URL.com/filename.htm">. Much better would be <a href="filename.htm">. But what if some files are in different folders to other ones? Use these shorthand properties:

8. Remove Unnecessary META Tags and META Content

Most META tags are pretty much unnecessary and don't achieve very much. If you're interested, you can see a list of META tags that are available. The most important tags for search engine optimisation are the keywords and description tags, although due to mass abuse they've lost a lot of importance in recent times. When using these META tags try to keep the content for each under 200 characters - anything more increases the size of your pages. Lengthy META tags are not good for search engines anyway because they dilute your keywords.

9. Put CSS and JavaScript into External Documents

To place CSS in an external document use:

<link type="text/css" rel="stylesheet" href="filename.css" />

To place JavaScript in an external document use:

<script language="JavaScript" src="filename.js" type="text/javascript"></script>

Any external file is called up just once and then cached (stored) on the user's computer. Instead of repeating JavaScript or CSS over and over again in HTML files, just write it out once in an external document.

And don't forget, there's no limit to the number of these external documents that you can use! For example, instead of making one huge CSS document, have one main one and some others that are specific to certain areas of your site.

10. Use / at the End of Directory Links

Don't do this: <a href="http://www.URL.com/directoryname">

Do this instead: <a href="http://www.URL.com/directoryname/">

Why? If there's no slash at the end of the URL the server doesn't know if the link is pointing to a file or to a directory. By including the slash the server instantly knows that the URL is pointing to a directory and doesn't need to spend any time trying to work it out.

About the Author

This article was written by Trenton Moss. Trenton's crazy about web usability and accessibility - so crazy that he went and started his own web usability and accessibility consultancy to help make the Internet a better place for everyone. He's very good at running CSS training and spends much of his time working on the world's most accessible CMS.

Related Optimization Articles, Tutorials, Code Examples

Related within Articles, Tutorials

Related within Web Site Resources

WebsiteTips.com's section of annotated links to many top-notch tutorials, tips, recommended books, and more.

topNavigation belowSearch/Sidebar

Articles, Tutorials, Tips:

Most Popular Books

In association with  In Association with Amazon.com 

Our Other Sites

SKDesigns

Effective, accessible, standards-based Web site design since 1996

SKDesigns - skdesigns.com

Brainstorms & Raves

Weblog on Web design, CSS, standards, music, more. By Shirley Kaiser, SKDesigns.

Brainstorms & Raves


Within Articles, Tutorials, Tips:

Within Web Site Resources:
SkipShow or Hide Detailed Navigation