This jQuery plugin takes the very first image of an element and convert it into the background image. This also keeps the normal image tag in place for SEO purposes.
It works best in situations when you want to have a square image but the image's size itself is not square. So this plugin will convert it into the background image and apply it to the targeted element and then make sure the image remains square all the time.
size
: set the image background size. default iscover
responsive
: set to 'true' if you want the element to resize as a normal image on responsive devices. default isfalse
position
: set the image background position. default iscenter center
width
: set a custom width of the box, applies on the container. default is blank,height
: set a custom height of the box, applies on the container. default is blankrepeat
: set the image background repeat. default isno-repeat
attachment
: set the image background attachment. default is blankfallbackSrc
: set a fallback image URL. default is blank
Following properties are all CSS properties so you can use same values as in CSS: size
position
width
height
repeat
attachment
Download this plugin using this NPM commend.
npm i jquery-convert-to-background
Simply close this repository or download it as zip. After that, include the jquery.convertToBackground.js
file in the head or footer of your HTML page.
<script src="/js/jquery.convertToBackground.js"></script>
You can also use the minified version, which is: jquery.convertToBackground.min.js
$('.element-that-has-img').convertToBackground({
// size: 'cover',
// responsive: false,
// position: 'center center',
width: 300,
height: 200,
// repeat: 'no-repeat',
// attachment: '',
// fallbackSrc: '',
});