Hello, my name is Pieter!
I build web applications and craft user interfaces.
Speeding up your website
People live in a hurry, so you'll probably lose some visitors (and possible customers) if your website takes a long time to load. For those who are in this situation and are willing to fix it, here's how.
1) Optimizing images
This is an absolute best practice and you should do it on every website you create. In Photoshop, it's pretty easy. You go to File > Save for Web and Devices and that's it. But what image format should you use? There are three main formats, JPEG (or JPG), GIF and PNG. Below is when you should use each format.
- JPEG: Being originally created for photographs and art, the JPEG format is best used when showing true-colour images or pictures. In Photoshop, use Quality: 60%-70% for best results.
- GIF: You should use GIF for flat-color images, e.g. logos or text on a plain background. Also, GIF is used when showing animations.
- PNG: PNG is a image format, created especially for the web (in order to replace GIF). Where GIF only has a range of 256 colours, PNG supports 24-byte colours and alpha channel transparency. You should use 24-bit PNG for images with more than 256 colours - e.g. gradients - or images using transparency.
There are also some online tools like Image Optimizer, doing the same thing online.
For further reading, here are some links:
- JPEG:
- Clever JPEG Optimization Techniques (Smashing Magazine)
- PNG:
- Clever PNG Optimization Techniques (Smashing Magazine)
- PNG Optimization Guide: More Clever Techniques (Smashing Magazine)
2) Cut your CSS
While developing your website, you often add a margin-left: 5px; or a font-style: italic; to your elements in order to fix some bugs or to spice the design up a little. But when the layout is fully coded, you should take a deep breath and take another good look at it. Are there double properties? Can you put multiple properties into one?
p {
Should be:
margin-top: 10px;
margin-bottom: 5px;
padding: 5px;
padding-left: 15px;
}
p {
Also, try to combine elements having the same properties. If you style blockquotes the same as your code blocks, it's unnecessary to have the following:
margin: 10px 0 5px;
padding: 5px 5px 5px 15px;
}
code {
Instead, you'd better use:
padding: 5px;
background: #eee;
border: 1px solid #999;
}
blockquote {
padding: 5px;
background: #eee;
border: 1px solid #999;
}
code, blockquote {
Again, there are online tools like CSS Optimizer doing the same thing, but I recommend that you do it yourself or don't rely on these.
padding: 5px;
background: #eee;
border: 1px solid #999;
}
Note: Compromising your CSS (with tools like CSS Optimizer) will make it hard to read. Always save a readable copy for further developing.
3) Reducing HTTP-requests
The idea behind this is simple. Every image, CSS, Javascript, ... is a little package that has to be send from your server to the user's computer and then be compiled by the browser. By reducing the amount of packages, the page loads faster. How can this be done? Here's what Yahoo! Developer Network says:
- Combine CSS-files. Instead of having reset.css, header.css, body.css, ... just have one file, stylesheet.css.
- Do the same with Javascript.
- Use CSS-sprites (see point 4)
- Use image-maps
4) Use CSS-sprites
There are already enough tutorials on CSS-sprites, so I don't see why I should explain it in depth here. I'd like to refer you to the articles on A List Apart and CSS-Tricks.
The main idea behind CSS-sprites is: having all the images combined in one big image instead of having a lot of small images. By doing so, you reduce the amount of HTTP-requests and reducing the load time.
5) Small improvements
There are some small improvements that won't help you a lot, but when your website is getting a lot of traffic, every little bit helps.
- Use a slash (/) on your links: e.g. pieterbeulque.be/blog/ instead of pieterbeulque.be/blog. When the slash is not there, the server first has to check what kind of file you're trying to access. With slash, the server knows immediately that you're wanting to view a directory.
- Use height="" and width="" on your image-tags. When the browser finds these, it doesn't has to figure out the size before loading it. Note: don't use this to resize your images! When you enter width="800" height="600" your image should be 800px wide and 600px high.
- Place your stylesheets at the top. This makes your webpages appear to load faster, but actually they don't.
- Place your scripts at the bottom. Scripts block parallel downloads, so loading can only be continued when the script is downloaded/executed.
- Use external stylesheets and Javascripts. External files are cached better.
Back to the homepage - The blog archives - © Pieter Beulque 2008-2010