views:

589

answers:

6

I was wondering if anyone has done any tests with background images. We normally create a background that repeats at least in one direction (x or y or both).

Example
Let's say we have a gradient background that repeats in X direction. Gradient height is 400px. We have several possibilities. We can create as small image as possible (1 pixel width and 400 pixels high) or we can create a larger image with 400 pixels height.

Observation
Since gradient is 400 pixels high we probably won't choose GIF format, because it can only store 256 adaptive colours. Maybe that's enaough if our gradient is subtle, since it doesn't have that many, but otherwise we'll probably rather store image as a 24-bit PNG image to preserve complete gradient detail.

Dilemma
Should we create an image of 1×400 px size that will be repeated n times horizontally or should we create an image of 100×400 px size to speed up rendering in the browser and have a larger image file size.

So. Image size vs. rendering speed? Which one wins? Anyone cares to test this? With regards to browser rendering speed and possible small image redraw flickering...

A: 

I'd put money on the bottleneck being the image download rather than the rendering engine doing the tiling, so go for the 1 pixel wide option.

Also the 24-bit PNG is redundant since you're still only getting 8 bits per channel (red, green and blue).

Pike65
That's 2^8 x 2^8 x 2^8 possible colours which is significantly greater than 256. Have I completely misunderstood something?
Dominic Rodger
In a gradient from one colour to another (assuming only two are used) then you'll only ever get 256 discrete colours. I think? Curses, now I'm starting to doubt myself. Off to Photoshop . . .
Pike65
It's going to depend on your gradient, but take some examples: a liner RGB gradient from RGB(0,0,0) to RGB(255,0,0) is only going to have 256 colours. RGB(0,0,0) to RGB(255,255,255) could in theory have 3*256 shades, but it's more likely to have just 256. Colours that are closer together are going to have fewer shades.
spookylukey
Paint.NET auto-detects the required bit depth; useful.
Alex Barrett
You're wrong about colours unless your gradient would go from black to one of the pure colours (R, G or B). Or from pure to white (but this one could have more than 256 levels as well). But in any other case you'll end up with an arbitrary number of levels.
Robert Koritnik
A: 

I generally prefer to go in between, 1pixel wide will probably make your gradient seem a bit unclear but you can do something like 5pixel width which gives enough room to the gradient to maintain consistency and clarity across the page.. but I would suggest you can add more patterns and images to a single image and then use background positioning(css sprites) to position them because download a single image of say 50kb would take less time comapared to 5 40kb images since the browser makes fewer requests to the server...

halocursed
I'm not talking about content images where sprites are usual. I'm more thinking of page backgrounds that are NOT sprites and can't be.
Robert Koritnik
sorry maybe misunderstood the question...kind of took it into the wrong direction
halocursed
You can still use sprites for this - you can put horizontally-tiled backgrounds in one image on top of each other, i.e. a 5x200 px image with two 5x100 px gradients.
DisgruntledGoat
Granted... But it would probably impact rendering even further. I guess it should.
Robert Koritnik
+1  A: 

If small dimensions of an image have a negative impact on rendering, I'm sure any decent browser would blit the image internally a few times before tiling.

That said, I tend not to use 1 pixel image dimensions, so I can see the image clearly without resizing it. PNG compression is good enough to handle this at very little cost to file size, in most situations.

Alex Barrett
+3  A: 

The rendering speed is the bottleneck here, since bigger tiles can be put into the browser's cache.

I've actually tried this for the major browsers, and at least some of them rendered noticeably slow on very small tiles.

So if increasing the bitmap size does not result in ridiculously big file sizes, I would definately go with that. Test it yourself and see. (Remember to include IE6, as still many people are stuck with it).

You might be able to strike a good balance between bitmap size and file size, but in general I'd try 50x400, 100x400, 200x400 and even 400x400 pixels.

csl
Well I guess that gradient with a bit more width (like 50 pixels) won't impact file size too drastically.
Robert Koritnik
Maybe try powers of 2 (i.e. 32 or 64 pixels wide), that often helps in these kinds of situations.
DisgruntledGoat
thanks for the powers of 2 tip. Really a good one. I'll keep this tip in mind.
Robert Koritnik
A: 

I have not benchmarked this but I'd bet that on most modern computers the rendering won't be an issue whereas you always want to save on on the image download time. I often go for the 1px type of graphics.

allesklar
+2  A: 

I found out that there may be a huge difference in the rendering performance of the browser, if you have a background-image with width of 1px and repeating it. It's better to have a background-image with slightly larger dimensions. So a image with a width of 100px performs much better in the browser. This especially comes into play when you use a repeated background-image in a draggable layer on your website. The drag-performance is pretty bad with an often-repeated background-image.

acme