📜 ⬆️ ⬇️

How to make background-size work in old IE

All of us, sometimes, want to make a beautiful background image in a particular area of ​​the site.
More often, in this case, we want the background not to be distorted when the browser window is resized.
Sometimes this can be done with the help of img , in which one of the dimensions (width or height) is specified in % , sometimes you need a more flexible solution (if only because the background is the background, and the content is not placed in the picture without special techniques).

The use of the CSS background-image property, which sets the background image for an element, comes to mind. However, without additional tags, the image is tied to the upper left corner and cropped if the size of the element is smaller than the image, and white areas appear on the right and below, if the size of the element is larger.

The next thing that comes to mind is the search for additional CSS properties that will help customize the background. These are background-repeat , background-position and background-size .
This is where the problems begin, more precisely, with background-size , since this property is not supported by IE8 and even IE9 in quirks mode .

There are two solutions to this problem:

')
If you chose the right solution, here's what is on the Internet:

1. Native crutches for IE

Sometimes it is acceptable to stretch and compress the background image, since the user will not notice the difference (a single-pixel strip gradient can stretch across the entire width of the page and look great)
To do this, add the following properties as CSS properties:
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/logo.gif', sizingMethod='scale'); -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/logo.gif', sizingMethod='scale')"; 

images / logo.gif is the path to the background image.
These properties are understood only by IE and do not harm normal browsers.

2. jquery.backgroundSize.js

This plugin is marked by the author as obsolete, however, it works.
Benefits:

Disadvantages:


To use this solution, you need to connect it to the page like this:
 <!-- The flag should be inserted before loading the script --> <script>$.debugBGS = true;</script> <!--[if lt IE 9]> <script src="/path/to/jquery.backgroundSize.min.js"></script> <![endif]--> 

And set the background-size property for the elements like this:
 $(elem).css( "background-size", "cover" ); 


3. background-size-polyfill

It is the development of solution number 2.
Benefits:

Disadvantages:


To use this solution, you must place the .htaccess file, taken from the site of this solution, in the root of the site, and backgroundsize.min.htc wherever you want.
When setting CSS properties for an element, along with background-size you need to set the -ms-behavior property : url (path_to_your_chook_job / backgroundsize.min.htc);

4. background-size-emu

The decision is based on the ideas of decision number 3.
Benefits:

Disadvantages:


To use this solution, you just need to connect the script to the page.


Common problems

Solutions # 2, # 3 and # 4 are inserted into the element for which the background-size is set , an absolutely positioned div with img inside and change the size of the picture depending on the size of the element.

Since this is still a crutch, all solutions are not supported:


Lastly

All of the above libraries are available on GitHub.

Source: https://habr.com/ru/post/234447/


All Articles