CSS repeating background, sprite or 1px png

蓝咒 提交于 2019-12-10 16:23:36

问题


Ok I want to know what is the best practice for performance regarding CSS background images and http requests.

1. Use many different 1px png background images resulting in several individual http requests

OR

2. Use one large image sprite with big gradient block chunks for use as background image. This will increase file size but save on http requests.

Love to hear you opinions...


回答1:


I think it would be better to use data:uri technique for small images (like 1px-backgrounds).

background: url(data:image/png;base64,....) top left repeat-x;

It works for all modern browsers. For old IE browsers (like IE6, IE7) you can overwrite styles by conditional comments.

background: url("path/to/background.png") top left repeat-x;

Of course this way you have to re-encode background, if it has changed. But it saves a lot of requests.




回答2:


If you're talking about using these images for gradients, I'd suggest using CSS gradients, then you won't need any images at all.

Of course, the obvious problem with CSS gradients is that it isn't supported by older versions of IE. The good news is that there is a fix for IE called CSS3Pie that allows it to support the standard CSS gradients feature (along with several other useful CSS features.

No more images required; just one HTC file (which only gets downloaded by IE).




回答3:


saving http requests is always the better solution. But nevertheless you should watch the file size, so that it does not get to big. Then you should consider making two images or more.

Look at the following tool which allows the easy creation of sprite images from unsprited images:

http://spriteme.org/



来源:https://stackoverflow.com/questions/6547167/css-repeating-background-sprite-or-1px-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!