• Stars
    star
    1,264
  • Rank 37,217 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated about 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Adds support for background-size "cover" and "contain" to IE8

background-size polyfill

An IE behavior adding support for background-size to IE8.

Demo

Introduction

Progressive Enhancement is the mantra I live by. It means "Have fun with CSS3 and don't worry about IE8 users; they'll never notice they're missing out on your gorgeous text-shadows and gradients, anyway".

All was well until I discovered the elegance of background-size: cover; and background-size: contain;. The first one, for instance, allows an image to completely cover a background, without having to send a 1920x1080 background image down the pipes.

Unfortunately, they don't degrade gracefully: websites would likely appear broken to IE8 users :-( ...unless you use this behavior!

How to use it?

Upload backgroundsize.min.htc to your website, along with the .htaccess that will send the mime-type required by IE (Apache only — it's built in nginx, node and IIS).

Everywhere you use background-size in your CSS, add a reference to this file.

.selector { 
	background-size: cover;
	/* The url is relative to the document, not to the css file! */
	/* Prefer absolute urls to avoid confusion. */
	-ms-behavior: url(/backgroundsize.min.htc);
}

The elements styled this way should have a position: relative; or position: fixed; and a z-index. If not, they will be given a position: relative; and z-index: 0;.

Limitations

This polyfill inserts an <img/> in the background of all elements matched in the css to emulate the background-size value. Calculating the displayed position and size of this image is quite complex and function of numerous parameters:

  • the size of the element itself
  • the size of the image
  • the values of background-[size/position/clip/origin/attachment/scroll]

It is thus impossible to emulate background-size completely and perfectly. But it's still possible to enjoy the main features:

  • correct position and size of the background image
  • updated position and size on browser resize
  • updated image, position and size when the background-image is modified

The following style properties, values or behavior aren't supported:

  • multiple backgrounds (although the :after trick can still be used)
  • 4 values syntax of background-position
  • any repeat value in background-repeat
  • non-default values of background-[clip/origin/attachment/scroll]
  • resizing the background when the dimensions of the element change

Removing any of these limitations is probably just one fork away...

License

MIT Licensed http://louisremi.mit-license.org/ Early versions by @louis_remi, now maintained by @jefferyto

Are you using this in a paid work?
Send me music http://www.amazon.co.uk/wishlist/HNTU0468LQON

Changelog

0.2.0 (2013-08-27)

  • Added support for:
    • Dynamically changing position and size of the background image (partial reimplementation of #8)
    • Dynamically changing background image with CSS classes
    • Dynamically setting background image to none (#13)
    • Lengths and percentages for background size (partial reimplementation of #8, #19)
    • Lengths for background position
    • Debouncing window resize events (alternative implementation of #17)
    • Cloning polyfilled elements (#2)
    • background-attachment: fixed for body (#22)
  • Fixed:
    • JS error when using left or top for background-position (#14, #21, #24)
    • Other "Invalid argument" / NaN JS errors (#11, #16, #23)
    • JS error when printing
  • Use standard HTC XML tags (#10)
  • Work around IE hang on unload when the JScript portion is greater than 512 bytes
  • Added unit tests, grunt build process
  • Misc fixes and code cleanup

0.1.0 (2012-11-23)

  • Initial release

More Repositories

1

jquery-smartresize

Debounced and Throttled Resize Events for jQuery
JavaScript
787
star
2

jquery.transform.js

jQuery cssHooks adding a cross browser transform property to $.fn.css() and $.fn.animate()
JavaScript
620
star
3

jquery.transition.js

Upgrade jQuery's .animate() method to use CSS3 Transitions in modern browsers.
JavaScript
437
star
4

jquery.backgroundSize.js

DEPRECATED, please use its successor: background-size polyfill
JavaScript
263
star
5

Activable

declarative UI components | 2K, 0 dependency, IE8 compat.
JavaScript
82
star
6

menu-effects

CSS3 menu effects
JavaScript
45
star
7

rotor.js

rotate3d manipulation library - lightweight and extensible
JavaScript
43
star
8

jquery.persival.js

Persival can persist the values users entered in forms, to make them resilient to "inadvertent tab closing" and crashes
JavaScript
35
star
9

node-DJs

Restart a server after each change in main script and dependencies
JavaScript
30
star
10

gulp-shelter

A cozy place for your shell tasks
JavaScript
22
star
11

borderimage

cross-browser implementation of border-image CSS3 property
JavaScript
21
star
12

mediaCache

Put image, audio and video resources in a dynamic cache for offline use
PHP
17
star
13

touch-devtool

Simulate touch events with a mouse, for fun and profit.
JavaScript
16
star
14

node.s3.js

A high level, file oriented javascript API to Amazon S3
JavaScript
10
star
15

jquery.browser

Browser and mobile platform sniffing scripts (plugin & standalone)
JavaScript
9
star
16

WordPress-Post-Relations

Lightweight relations between WordPress post types
PHP
8
star
17

jquery.backgroundXY.js

A jQuery cssHooks implementing background-position-X and background-position-Y in browsers lacking it.
JavaScript
8
star
18

orientation-devtool

Simulate orientation events, for fun and profit
JavaScript
8
star
19

victor

Convert your SVG files to lightweight, Raphael-compatible JSON
JavaScript
7
star
20

WordPress-Sortable-Posts

Reorder posts by drag-and-drop
JavaScript
7
star
21

geolocation.js

a polyfill providing a normalized W3C geolocation API across browsers.
JavaScript
7
star
22

store.space.js

a store.js plugin bringing namespaces to localStorage, in half a Kb
JavaScript
6
star
23

jquery.cookie

The ultimate library to write cookies with jQuery
6
star
24

node.pdf.js

Pdf generation for node using wkhtmltopdf
JavaScript
6
star
25

Math.uuid.js

Minimalistic version of Robert Kieffer's Math.uuid.js, primarily aimed at interacting with couchDB
JavaScript
5
star
26

froglet.js

sandboxed-Widget boilerplate for bookmarklet and extension
JavaScript
4
star
27

slack-app-in-tab

A Chrome + Firefox extension to open the Slack app with its multi-workspace sidebar in a simple browser tab
JavaScript
4
star
28

fxngn.js

A minimalist effects/game engine built to evaluate the influence of != parameters on performances
JavaScript
4
star
29

node.csv.js

Every language need its CSV parser, JavaScript needs a JSON style API
JavaScript
4
star
30

todoso

ToDoSo is a presentation authoring, playing and sharing Web application
JavaScript
4
star
31

ti

nothing interesting
JavaScript
3
star
32

jquery-interval-bookmarklet

Experimenting with visual performances of jQuery animations
JavaScript
3
star
33

jquery.delegate.js

Backport the delegate function introduced in jQuery 1.4, for jQuery 1.3
JavaScript
3
star
34

jquery.updateInterval

update $.fx.interval property dynamically and don't wait to see the effect
JavaScript
3
star
35

designer-tools

cross-browser plugin that makes it easy to style your pages in a cross-browser way
JavaScript
3
star
36

EventSound

audible jQuery events
JavaScript
2
star
37

CV

Take a wild guess
1
star
38

xentity

Declarative UI Component Factory
JavaScript
1
star
39

Slasher

slasher game for the GDPJAM
JavaScript
1
star
40

todosOverlay

The overlay plugin for jQuery that is used on the ToDoSo introduction page
1
star