Artician Home
Join Artician Login Search
Today This Week Mark All as Read Feeds
 

Use CSS to reduce image HTTP Requests.

Artician Developer
User is offline
Eric posted Aug 1 2007, 03:22 PM:
Today when we analyze web pages we commonly see 50 to 60 images per web page. The more round trip server requests you require in your pages, the longer your load time becomes. One way to reduce the number of HTTP requests is to combine adjacent images into one composite image and use CSS background position to show the correct part of the image in the provided space of a div, span, or other HTML elements.

Yesterday I took some time out to see if I could use this method on Artician, turns out it works fabulously. Here's what we got:

The image below is saving every page load on artician 15 HTTP requests. All rounded corners on Artician used to be composed of individual images top left, top right, bottom left, bottom right, and then called into the CSS. Even when an image is cached the image location is still checked and sent back to the browser. So here we have one image that's not even 1kb than contains 15 rounded corners used for various containers on Artician.

Rounded Corners:
Artician Image

Then I use CSS to make sure each rounded corner is being displayed in it's appropriate area for example:

CSS Example (Only top 4 corners)
Syntax Highlighted Code: HTML
 
.outer_tl {height:5px; background: url(/images/layout/container/corners.gif) no-repeat top left;}
.outer_tr {height:5px; background: url(/images/layout/container/corners.gif) no-repeat right -6px; margin-left:5px;}
.outer_bl {height:5px; background: url(/images/layout/container/corners.gif) no-repeat left -12px;}
.outer_br {height:5px; background: url(/images/layout/container/corners.gif) no-repeat right -18px margin-left:5px;}
Notice! they are all using the same background image, but I'm using a negative background position to hit the right spot on corners.gif.

Top left corner is using a basic CSS background position of "top left" which is the very top, and very left of the image.
Top right corner is using a background position of "right -6px" which mean all the way to right, but -6px down on the image.
Bottom left corner is using a background position of "left -12px" which mean all the way to left, but -12px down on the image.
Bottom right corner is using a background position of "right -18px" which mean all the way to right, but -18px down on the image.
The other CSS you see like "height:5px;" and "margin-left:5px;" are not effecting the background image, only accounting for the way I setup my rounded corner containers which can be see here. (view source for HTML and CSS)

Keep in mind: the CSS above has all the background properties in one declaration for optimization
The following CSS is using separate background declarations:
[code].outer_tr {
background-image: url(/images/layout/container/corners.gif);
background-repeat:no-repeat;
background-position: right -6px;
}
<!--c2--><!--ec2-->

So there ya have it!
Combine all your little itty bitty images on a site, and let CSS do what it's got to do. This method has been tested in FF2, IE7, IE6, Opera, and Safari

Here's another example being used on Artician:

Networkbar Icons:
Artician Image

And the CSS...
[code]#quick #user span {background: url(/images/layout/network_bar/icons/all.png) top left no-repeat;}
#quick #submitwork span {background: url(/images/layout/network_bar/icons/all.png) left -15px no-repeat;}
#quick #usercp span {background: url(/images/layout/network_bar/icons/all.png) left -30px no-repeat;}
#quick #customize span {background: url(/images/layout/network_bar/icons/all.png) left -45px no-repeat;}
#quick #search span{background: url(/images/layout/network_bar/icons/all.png) left -60px no-repeat;}
a#logout {background: url(/images/layout/network_bar/icons/all.png) left -75px no-repeat;} <!--c2--><!--ec2-->

Hope this is helpful! It really was for us, we already saved a total of 23 HTTP, and I got plenty more to do. The main page (logged out) went from 92 HTTP requests to 69. happy.gif

--------------------
Artician Developer Management
Post 1
Artician Developer
User is offline
KnightAR posted Aug 1 2007, 04:25 PM:
Thats so cool Eric! I didn't even know CSS could do that!

--------------------
Artician Co-Founder / Site PHP & JS Developer
Post 2
Artician Developer
User is offline
Akujin posted Aug 1 2007, 06:51 PM:
Eric should write a book on CSS. It would be a best seller. lol

--------------------
Artician Senior Web Development
http://akujin.artician.com
Post 3
Artician Developer
User is offline
Eric posted Aug 1 2007, 07:44 PM:
Haha thanks guys! biggrin.gif

--------------------
Artician Developer Management
Post 4
Artician Staff
User is offline
Lee posted Aug 1 2007, 08:06 PM:
wtf h4x! Eric is a genius. tongue.gif

--------------------
Post 5
Artician Staff
User is offline
mark posted Aug 1 2007, 08:37 PM:
That's great Eric, I'm sure I'll use this somtime!
Thanks! biggrin.gif

You use the same technique for hovers and stuff right?
Post 6
Artician Developer
User is offline
Eric posted Aug 1 2007, 09:51 PM:
QUOTE
You use the same technique for hovers and stuff right?

Yessir.

QUOTE
wtf h4x! Eric is a genius.

Haha, thanks but the only genius's in here are Akujin, and KnightAR biggrin.gif - PHP Masters!

--------------------
Artician Developer Management
Post 7
Artician Beta Tester
User is offline
woodcore posted Aug 2 2007, 02:37 PM:
Damn I've never thougt about that kind of extensive use with bg-image positioning. Noiice eric, really. Proud of ya!
Post 8
Artician Developer
User is offline
Eric posted Aug 2 2007, 05:44 PM:
Ha, thanks my CSS brother biggrin.gif

--------------------
Artician Developer Management
Post 9
Artician Staff
User is offline
kman posted Aug 3 2007, 03:10 AM:
Haha props : D
Very clever man.
Post 10
Artician Developer
User is offline
Eric posted Aug 3 2007, 12:14 PM:
Thanks kman wink.gif

--------------------
Artician Developer Management
Post 11
Artician Beta Tester
User is offline
chaoskaizer posted Apr 18 2008, 06:41 AM:
there is a name for this methods "sprite css " or image mapping

change from
CODE

.outer_tl {height:5px; background: url(/images/layout/container/corners.gif) no-repeat top left;}
.outer_tr {height:5px; background: url(/images/layout/container/corners.gif) no-repeat right -6px; margin-left:5px;}
.outer_bl {height:5px; background: url(/images/layout/container/corners.gif) no-repeat left -12px;}
.outer_br {height:5px; background: url(/images/layout/container/corners.gif) no-repeat right -18px margin-left:5px;}


replace with
CODE

.bgs,.outer_tl,.outer_tr,.outer_bl,.outer_br{background-image: url(/images/layout/container/corners.gif);background-repeat: no-repeat;height:5px}

.outer_tl {background-position: top left}
.outer_tr {background-position: right -6px; margin-left:5px}
.outer_bl {background-position: left -12px}
.outer_br {background-position: right -18px; margin-left:5px}


note: there is a cross browser bug with background position using "alpha numeric value" ie ( top -6px or 40% left ... ) its better to used absolute prop like 0px 2px, 50% 40% or center left;


-- should be
CODE

.bgs,.outer_tl,.outer_tr,.outer_bl,.outer_br{background-image: url(/images/layout/container/corners.gif);background-repeat: no-repeat;height:5px}

.outer_tr {background-position: 9px -6px; margin-left:5px}
.outer_bl {background-position: 0px -12px}
.outer_br {background-position: 9px -18px; margin-left:5px}


the default background position is always set at" top left "(0px 0px) coordinate so no need to set .outer_tl. hope this will help others write better code and understand CSS inheritance.

--------------------
know thyself
Post 12
Artician Moderator
User is offline
hannesc posted Apr 19 2008, 05:49 AM:
This is awesome. I am doing my first site with round corners and am only now starting to get into CSS properly. Thank you so much for this guys. It is going to be a huge help. biggrin.gif

--------------------
Post 13
User is offline
yenfa posted Apr 27 2008, 07:04 AM:
Thanks for that tutorial! Its a great help ~

--------------------
Post 14
 
« Previous Topic Next Topic »

Topic Tags: optimization, css corners, http request, rounded corners, corners, rounded, background, css, request, http Description: Who's Reading: 2 User(s) 0 Members:

Browse Without Ads!