SlideShare a Scribd company logo
CSS and Image
Optimization




Stoyan Stefanov, @stoyanstefanov
May 12, 2010
Web Optimization Summit
Agenda
1.  Reducing CSS file sizes
2.  Rendering
3.  Reducing HTTP requests
4.  Optimizing image file sizes
Reducing CSS file sizes
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	


     Long selectors
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	


       C-style indented
         declarations
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	


  What? Not
unique enough?
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	


           One is probably
              enough
CSS recipe for disaster
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	



                   Lame
Crockford on CSS:



 “Long, fragile lists of
  self-contradictory
         rules”
So?
#sidebar #menu ul li.blogroll	
{	
    color: green !important;	
    font-weight: bold;	
}	

Legend:
• easy stuff • tough stuff
So?



 Make the tough stuff
        easy
Reusable CSS
.linklist {color: green; ...}	

/* widgets */	
.module {...}	
.gallery {...}	

/* utilities */ 	
.clearfix {...};	
.flip {...}
Mixin pattern
<ul class="module linklist">	
…
Reusable CSS

1.  Strong foundation:
    reset, grids
2.  Library styles:
    .class not #id
Object-Oriented CSS
•  Nicole Sullivan
•  oocss.org
Minifying CSS
•  Strip white space, comments
•  Some micro-optimizations
•  ~30% savings
•  can’t rename
•  YUICompressor, CSSTidy
•  Inline code too!
                http://tools.w3clubs.com/cssmin/
Browser-specific CSS
•  IE6,7 vs. others
•  -webkit-, -moz-, -o-
•  -ms-, _, *, zoom
•  automated
•  savings? 3-5%, mileage vary
       http://phpied.com/files/css-parse/css-strip.html
CSS and rendering
Filters
•  AlphaImageLoader is bad
•  Verdict is out on the others
Expressions
•  Executed too often
•  Avoid
•  JavaScript or self-rewrite
Behaviors
•  Be careful with the shims
•  ie-css3.htc, pngfix.htc,…
Scrollbars trick
•  Prevent a reflow when you
expect a long page

body {overflow-y: scroll;}
Reducing HTTP requests
CSS blocks rendering
•  The worst component type
•  Place way at the top
•  Inline all @media print, etc



http://www.phpied.com/delay-loading-your-print-css/
http://www.phpied.com/rendering-styles/
Same domain
•  If you split across domains,
keep CSS on the same
domain as HTML
Inline CSS
•  Google search
•  Bing.com: inline + postload
Inline + postload
•  First visit:


1. Inline
2. Lazy-load the external file
3. Write a cookie
Inline + postload
•  Later visits:


1. Read cookie
2. Refer to the external file
Web Fonts
•  Don’t go overboard!
•  Subset
•  Gzip!


      http://snook.ca/archives/html_and_css/
                screencast-converting-ttf2eot
Fewer HTTP requests
•  Inline images:
   in CSS sprites
   with data: URI scheme


                http://csssprites.com
                                    	
                http://spriteme.org
Fewer HTTP requests
•  data: URI scheme

$ php ‐r "echo base64_encode(file_get_contents('my.png'));” 
iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P
4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC 
Fewer HTTP requests
•  data: URI scheme

background‐image: url("data:image/
png;base64,iVBORw0KG..."); 
Fewer HTTP requests
•  data: URI scheme

<img src="data:image/png;base64,iVBOR..." /> 
CSS and image optimization
CSS and image optimization
Fewer HTTP requests
•  data: URI scheme
•  works in IE!...
Fewer HTTP requests
•  data: URI scheme
•  works in IE8!
Fewer HTTP requests
•  data: URI scheme
•  MHTML for IE < 8
MHTML
•  MIME HTML
•  Works in IE 6,7
•  Indeed it actually absolutely
does work in IE7/Vista too
MHTML - one part
Content-Location: myimage	
Content-Transfer-Encoding: base64	

iVBORw0KGgoAAAANSU....U5ErkJggg==
MHTML - multi parts
Content-Type: multipart/related; boundary="MYSEPARATOR"	

--MYSEPARATOR	

[here comes part one]	                   The
                                         double-
--MYSEPARATOR	                           dash of
                                         doom
[here's part two]	

--MYSEPARATOR--
MHTML – all together
/*	
Content-Type: multipart/related; boundary="MYSEPARATOR"	

--MYSEPARATOR	
Content-Location: myimage	
Content-Transfer-Encoding: base64	

iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAD....U5ErkJggg==	
--MYSEPARATOR	
Content-Location: another	
Content-Transfer-Encoding: base64	

iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAA....U5ErkJggg==	
--MYSEPARATOR--	
*/	
.myclass {	
    background-image:url(mhtml:http://example.org/styles.css!myimage);	
}	
.myotherclass {	
    background-image:url(mhtml:http://example.org/styles.css!another);	
}
MHTML + data URI
•  drawback: repeats the
same encoded image
•  solution: browser-specific
CSS
•  or… an ingenious hack
MHTML + data URI
•  image header + css + data
/9j/4AA0;background-image:url(data:image/jpeg;base64;00,/9j/4AA0


Reality:

IE:

Others:



http://habrahabr.ru/blogs/webdev/90761/
No-image UI
•  a.k.a. CSS3
Rounded corners
.module {	
  -moz-border-radius: 9px;	
  -webkit-border-radius: 9px;	
  border-radius: 9px;	
}
Rounded corners
            All

            vs. 	
             IE
Gradients
.hd {	
  background-image: -moz-linear-gradient(top, #641d1c, #f00);	
  background-image: -webkit-gradient(linear, left top, left bottom, 	
                                     from(#641d1c), to(#f00));	
  filter: [...].gradient(startColorstr=#ff641d1c,endColorstr=#ffff0000);	
  -ms-filter: "[...].gradient(startColorstr=#ff641d1c,endColorstr=#ffff0000)"; 	
}
No-image UI
•  rounded corners
•  gradients
•  glows, shadows
•  rgba
                  http://www.phpied.com/
     css-performance-ui-with-fewer-images/
                     http://css3please.com
Multi-purpose “mask” images
•  one image on top of
different solid colors
•  for themes, social profiles…
1px wide “mask”
•  Top:
somewhat
transparent
•  Bottom:
Fully
transparent
Multi-purpose mask
Gradient mask
•  the data URI smaller that all
the CSS –moz, -webkit, filter...
A glossy mask
Optimizing image file sizes
Rule #1: No GIFs




$ optipng *.gif
PNG-8
•  Palette image (like GIF)
•  256 colors (like GIF)
•  Smaller than GIF (GIF++)
•  Alpha transparency (GIF++)
Rarely PNG-24
•  Truecolor
PNGSlim
                  	
OptiPNG	        DeflOpt	

           PNGOut	
      PNGOptimizer	

PNGCrush	
                 AdvPNG	
   PNGRewrite
JPEG
•  The format for photos
•  Run through JPEGTran
Study of the images on the top
1000 sites
Q: How many GIFs are out
there?
Q: What if we make them PNG?
Q: Are PNGs optimized?
Q: Are JPEGs optimized?
Top 1000 – how?
•  URLs from Alexa top sites
•  open Fiddler
•  php script that opens/closes
Explorer with each URL
•  enjoy!
•  Export images from Fiddler
Top 1000 – GIF vs. PNG?

                 Animated
                    GIF
                    3%

                            PNG
                            24%

    GIF
    73%
Top 1000 – GIF vs. PNG vs.
JPG?

                            GIF
                            40%
       JPEG
        46%



                PNG   Animated GIF
                13%       1%
Top 1000 – GIF to PNG
$ optipng *.gif	
$ pngoptimizercl –
file:"*.png”	
(1 min/1000 files)	

                 23.79%
                 savings
CSS and image optimization
Top 1000 – Optimizing PNG
$ pngoptimizercl –
file:"*.png"	




                 16.90%
                 savings
Top 1000 – Optimizing JPG
$ jpegtran –copy none -optimize	




                   13.08%
                   savings
Take-home
1.  Think reusable CSS
2.  Reduce HTTP requests:
     - sprites, data URIs
     - CSS3, reusable images
3.  Smush all images
Thank you!


Stoyan Stefanov
@stoyanstefanov
http://www.phpied.com

More Related Content

What's hot (20)

PDF
LESS : The dynamic stylesheet language
Katsunori Tanaka
 
ODP
HPPG - high performance photo gallery
Remigijus Kiminas
 
PDF
Implementing Awesome: An HTML5/CSS3 Workshop
Shoshi Roberts
 
PPTX
SASS is more than LESS
Itai Koren
 
PPTX
From PSD to WordPress Theme: Bringing designs to life
Derek Christensen
 
KEY
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
KEY
Profiling php applications
Justin Carmony
 
KEY
HTML5 and CSS3 for Teachers
Jason Hando
 
KEY
Mongo NYC PHP Development
Fitz Agard
 
PDF
Palestra pré processadores CSS
Just Digital
 
KEY
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Laura Scott
 
PPTX
Css 3
poollar
 
PPTX
Css 3
poollar
 
PDF
Powering your website with realtime data
becoded
 
PDF
Who is Afraid of Cookies?
Asaf Gery
 
KEY
MongoDB, E-commerce and Transactions
Steven Francia
 
ZIP
Theme Kickstart
Peter
 
KEY
Using Sass - Building on CSS
Sayanee Basu
 
LESS : The dynamic stylesheet language
Katsunori Tanaka
 
HPPG - high performance photo gallery
Remigijus Kiminas
 
Implementing Awesome: An HTML5/CSS3 Workshop
Shoshi Roberts
 
SASS is more than LESS
Itai Koren
 
From PSD to WordPress Theme: Bringing designs to life
Derek Christensen
 
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
Profiling php applications
Justin Carmony
 
HTML5 and CSS3 for Teachers
Jason Hando
 
Mongo NYC PHP Development
Fitz Agard
 
Palestra pré processadores CSS
Just Digital
 
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Laura Scott
 
Css 3
poollar
 
Css 3
poollar
 
Powering your website with realtime data
becoded
 
Who is Afraid of Cookies?
Asaf Gery
 
MongoDB, E-commerce and Transactions
Steven Francia
 
Theme Kickstart
Peter
 
Using Sass - Building on CSS
Sayanee Basu
 

Similar to CSS and image optimization (20)

PDF
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
PPTX
Web Optimisation
Gregory Benner
 
PDF
Html graphics
Mukesh Tekwani
 
KEY
Artdm171 Week6 Images
Gilbert Guerrero
 
PPTX
Castro Chapter 5
Jeff Byrnes
 
PPTX
Putting Your Images on a Diet (SmashingConf, 2014)
Guy Podjarny
 
PPTX
Chapter 23: Web Images
Steve Guinan
 
PPT
Your Images are Weighing You Down: Optimization for a Better UX
Tim D'Agostino
 
PDF
High Performance Images: Beautiful Shouldn't Mean Slow
Guy Podjarny
 
PPTX
Images meet Web
傑倫 鍾
 
PDF
Image optimization and you
Johannes Siipola
 
PPT
Yahoo - Web Images optimization
Eduard Bondarenko
 
KEY
HTML CSS & Javascript
David Lindkvist
 
PDF
Image Optimization for the Web at php|works
Stoyan Stefanov
 
PPTX
EscConf - Deep Dive Frontend Optimization
Jonathan Klein
 
PDF
Web Design Workshop
SuseZ
 
PDF
Adobe MAX 2008: HTML/CSS + Fireworks
Nathan Smith
 
PDF
Don't make me wait! or Building High-Performance Web Applications
Stoyan Stefanov
 
PDF
A little journey into website optimization
Stelian Firez
 
PPT
performance.ppt
fakeaccount225095
 
Voices that matter: High Performance Web Sites
Stoyan Stefanov
 
Web Optimisation
Gregory Benner
 
Html graphics
Mukesh Tekwani
 
Artdm171 Week6 Images
Gilbert Guerrero
 
Castro Chapter 5
Jeff Byrnes
 
Putting Your Images on a Diet (SmashingConf, 2014)
Guy Podjarny
 
Chapter 23: Web Images
Steve Guinan
 
Your Images are Weighing You Down: Optimization for a Better UX
Tim D'Agostino
 
High Performance Images: Beautiful Shouldn't Mean Slow
Guy Podjarny
 
Images meet Web
傑倫 鍾
 
Image optimization and you
Johannes Siipola
 
Yahoo - Web Images optimization
Eduard Bondarenko
 
HTML CSS & Javascript
David Lindkvist
 
Image Optimization for the Web at php|works
Stoyan Stefanov
 
EscConf - Deep Dive Frontend Optimization
Jonathan Klein
 
Web Design Workshop
SuseZ
 
Adobe MAX 2008: HTML/CSS + Fireworks
Nathan Smith
 
Don't make me wait! or Building High-Performance Web Applications
Stoyan Stefanov
 
A little journey into website optimization
Stelian Firez
 
performance.ppt
fakeaccount225095
 
Ad

More from Stoyan Stefanov (20)

PDF
Reactive JavaScript
Stoyan Stefanov
 
PPTX
YSlow hacking
Stoyan Stefanov
 
PPTX
Liking performance
Stoyan Stefanov
 
PPTX
JavaScript Performance Patterns
Stoyan Stefanov
 
PPTX
JavaScript performance patterns
Stoyan Stefanov
 
PPTX
High Performance Social Plugins
Stoyan Stefanov
 
PDF
Social Button BFFs
Stoyan Stefanov
 
PPTX
JavaScript навсякъде
Stoyan Stefanov
 
PPTX
JavaScript is everywhere
Stoyan Stefanov
 
PDF
JavaScript shell scripting
Stoyan Stefanov
 
PDF
JavaScript for PHP developers
Stoyan Stefanov
 
PDF
WPO @ PubCon 2010
Stoyan Stefanov
 
PDF
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
PDF
Progressive Downloads and Rendering
Stoyan Stefanov
 
PDF
Performance patterns
Stoyan Stefanov
 
PDF
Psychology of performance
Stoyan Stefanov
 
PPT
3-in-1 YSlow
Stoyan Stefanov
 
PPT
High-performance DOM scripting
Stoyan Stefanov
 
PPT
The business of performance
Stoyan Stefanov
 
PDF
JavaScript Patterns
Stoyan Stefanov
 
Reactive JavaScript
Stoyan Stefanov
 
YSlow hacking
Stoyan Stefanov
 
Liking performance
Stoyan Stefanov
 
JavaScript Performance Patterns
Stoyan Stefanov
 
JavaScript performance patterns
Stoyan Stefanov
 
High Performance Social Plugins
Stoyan Stefanov
 
Social Button BFFs
Stoyan Stefanov
 
JavaScript навсякъде
Stoyan Stefanov
 
JavaScript is everywhere
Stoyan Stefanov
 
JavaScript shell scripting
Stoyan Stefanov
 
JavaScript for PHP developers
Stoyan Stefanov
 
WPO @ PubCon 2010
Stoyan Stefanov
 
Progressive Downloads and Rendering - take #2
Stoyan Stefanov
 
Progressive Downloads and Rendering
Stoyan Stefanov
 
Performance patterns
Stoyan Stefanov
 
Psychology of performance
Stoyan Stefanov
 
3-in-1 YSlow
Stoyan Stefanov
 
High-performance DOM scripting
Stoyan Stefanov
 
The business of performance
Stoyan Stefanov
 
JavaScript Patterns
Stoyan Stefanov
 
Ad

Recently uploaded (20)

PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Digital Circuits, important subject in CS
contactparinay1
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 

CSS and image optimization

  • 1. CSS and Image Optimization Stoyan Stefanov, @stoyanstefanov May 12, 2010 Web Optimization Summit
  • 2. Agenda 1.  Reducing CSS file sizes 2.  Rendering 3.  Reducing HTTP requests 4.  Optimizing image file sizes
  • 4. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; }
  • 5. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } Long selectors
  • 6. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } C-style indented declarations
  • 7. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } What? Not unique enough?
  • 8. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } One is probably enough
  • 9. CSS recipe for disaster #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } Lame
  • 10. Crockford on CSS: “Long, fragile lists of self-contradictory rules”
  • 11. So? #sidebar #menu ul li.blogroll { color: green !important; font-weight: bold; } Legend: • easy stuff • tough stuff
  • 12. So? Make the tough stuff easy
  • 13. Reusable CSS .linklist {color: green; ...} /* widgets */ .module {...} .gallery {...} /* utilities */ .clearfix {...}; .flip {...}
  • 15. Reusable CSS 1.  Strong foundation: reset, grids 2.  Library styles: .class not #id
  • 16. Object-Oriented CSS •  Nicole Sullivan •  oocss.org
  • 17. Minifying CSS •  Strip white space, comments •  Some micro-optimizations •  ~30% savings •  can’t rename •  YUICompressor, CSSTidy •  Inline code too! http://tools.w3clubs.com/cssmin/
  • 18. Browser-specific CSS •  IE6,7 vs. others •  -webkit-, -moz-, -o- •  -ms-, _, *, zoom •  automated •  savings? 3-5%, mileage vary http://phpied.com/files/css-parse/css-strip.html
  • 20. Filters •  AlphaImageLoader is bad •  Verdict is out on the others
  • 21. Expressions •  Executed too often •  Avoid •  JavaScript or self-rewrite
  • 22. Behaviors •  Be careful with the shims •  ie-css3.htc, pngfix.htc,…
  • 23. Scrollbars trick •  Prevent a reflow when you expect a long page body {overflow-y: scroll;}
  • 25. CSS blocks rendering •  The worst component type •  Place way at the top •  Inline all @media print, etc http://www.phpied.com/delay-loading-your-print-css/ http://www.phpied.com/rendering-styles/
  • 26. Same domain •  If you split across domains, keep CSS on the same domain as HTML
  • 27. Inline CSS •  Google search •  Bing.com: inline + postload
  • 28. Inline + postload •  First visit: 1. Inline 2. Lazy-load the external file 3. Write a cookie
  • 29. Inline + postload •  Later visits: 1. Read cookie 2. Refer to the external file
  • 30. Web Fonts •  Don’t go overboard! •  Subset •  Gzip! http://snook.ca/archives/html_and_css/ screencast-converting-ttf2eot
  • 31. Fewer HTTP requests •  Inline images: in CSS sprites with data: URI scheme http://csssprites.com http://spriteme.org
  • 32. Fewer HTTP requests •  data: URI scheme $ php ‐r "echo base64_encode(file_get_contents('my.png'));”  iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P 4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC 
  • 33. Fewer HTTP requests •  data: URI scheme background‐image: url("data:image/ png;base64,iVBORw0KG..."); 
  • 34. Fewer HTTP requests •  data: URI scheme <img src="data:image/png;base64,iVBOR..." /> 
  • 37. Fewer HTTP requests •  data: URI scheme •  works in IE!...
  • 38. Fewer HTTP requests •  data: URI scheme •  works in IE8!
  • 39. Fewer HTTP requests •  data: URI scheme •  MHTML for IE < 8
  • 40. MHTML •  MIME HTML •  Works in IE 6,7 •  Indeed it actually absolutely does work in IE7/Vista too
  • 41. MHTML - one part Content-Location: myimage Content-Transfer-Encoding: base64 iVBORw0KGgoAAAANSU....U5ErkJggg==
  • 42. MHTML - multi parts Content-Type: multipart/related; boundary="MYSEPARATOR" --MYSEPARATOR [here comes part one] The double- --MYSEPARATOR dash of doom [here's part two] --MYSEPARATOR--
  • 43. MHTML – all together /* Content-Type: multipart/related; boundary="MYSEPARATOR" --MYSEPARATOR Content-Location: myimage Content-Transfer-Encoding: base64 iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAD....U5ErkJggg== --MYSEPARATOR Content-Location: another Content-Transfer-Encoding: base64 iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAA....U5ErkJggg== --MYSEPARATOR-- */ .myclass { background-image:url(mhtml:http://example.org/styles.css!myimage); } .myotherclass { background-image:url(mhtml:http://example.org/styles.css!another); }
  • 44. MHTML + data URI •  drawback: repeats the same encoded image •  solution: browser-specific CSS •  or… an ingenious hack
  • 45. MHTML + data URI •  image header + css + data /9j/4AA0;background-image:url(data:image/jpeg;base64;00,/9j/4AA0 Reality: IE: Others: http://habrahabr.ru/blogs/webdev/90761/
  • 47. Rounded corners .module { -moz-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px; }
  • 48. Rounded corners All
 vs. IE
  • 49. Gradients .hd { background-image: -moz-linear-gradient(top, #641d1c, #f00); background-image: -webkit-gradient(linear, left top, left bottom, from(#641d1c), to(#f00)); filter: [...].gradient(startColorstr=#ff641d1c,endColorstr=#ffff0000); -ms-filter: "[...].gradient(startColorstr=#ff641d1c,endColorstr=#ffff0000)"; }
  • 50. No-image UI •  rounded corners •  gradients •  glows, shadows •  rgba http://www.phpied.com/ css-performance-ui-with-fewer-images/ http://css3please.com
  • 51. Multi-purpose “mask” images •  one image on top of different solid colors •  for themes, social profiles…
  • 52. 1px wide “mask” •  Top: somewhat transparent •  Bottom: Fully transparent
  • 54. Gradient mask •  the data URI smaller that all the CSS –moz, -webkit, filter...
  • 57. Rule #1: No GIFs $ optipng *.gif
  • 58. PNG-8 •  Palette image (like GIF) •  256 colors (like GIF) •  Smaller than GIF (GIF++) •  Alpha transparency (GIF++)
  • 60. PNGSlim OptiPNG DeflOpt PNGOut PNGOptimizer PNGCrush AdvPNG PNGRewrite
  • 61. JPEG •  The format for photos •  Run through JPEGTran
  • 62. Study of the images on the top 1000 sites Q: How many GIFs are out there? Q: What if we make them PNG? Q: Are PNGs optimized? Q: Are JPEGs optimized?
  • 63. Top 1000 – how? •  URLs from Alexa top sites •  open Fiddler •  php script that opens/closes Explorer with each URL •  enjoy! •  Export images from Fiddler
  • 64. Top 1000 – GIF vs. PNG? Animated GIF 3% PNG 24% GIF 73%
  • 65. Top 1000 – GIF vs. PNG vs. JPG? GIF 40% JPEG 46% PNG Animated GIF 13% 1%
  • 66. Top 1000 – GIF to PNG $ optipng *.gif $ pngoptimizercl – file:"*.png” (1 min/1000 files) 23.79% savings
  • 68. Top 1000 – Optimizing PNG $ pngoptimizercl – file:"*.png" 16.90% savings
  • 69. Top 1000 – Optimizing JPG $ jpegtran –copy none -optimize 13.08% savings
  • 70. Take-home 1.  Think reusable CSS 2.  Reduce HTTP requests: - sprites, data URIs - CSS3, reusable images 3.  Smush all images