Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
https://greensock.com/forums/topic/18144-animating-tofrom-backgroundsize-contain-or-cover/
https://codepen.io/GreenSock/pen/BaRNZxm?editors=0010

broken version: https://codepen.io/ChrisBup/pen/ExmjWRW
-->
<main class="wrapper">
  <div class="bg-img"></div>
</main>
              
            
!

CSS

              
                html {
  box-sizing: border-box;
}

*, *::before, *::after {
  box-sizing: inherit;
  &:focus {
    outline: none;
  }
}

* {
  font-family: monaco, courier;
}

body {
  margin: 0;
  padding: 0;
}

.wrapper {
  width: 100%;
  min-height: 100vh;
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #ddd;
  // @media (min-width: 768px) {
  //   background: palegreen;
  // }
}

.bg-img {
  width: 100%;
  max-width: 600px;
  height: 400px;
  background-image: url('https://satyr.dev/600x400/lightblue');
	background-size: cover;
  background-position: center;
	background-repeat: no-repeat;
	border: 4px solid blue;
}
              
            
!

JS

              
                console.log( "///////////////////" );

gsap.fromTo('.bg-img', {
  // make sure it gets converted to pixels so that it interpolates cleanly
  backgroundSize: bgSize(".bg-img", {size: "150% auto"}),
}, {
  // make sure it gets converted to pixels so that it interpolates cleanly
  backgroundSize: bgSize(".bg-img", {size: "cover"}),
  delay: 0.5,
  duration: 2,
  ease: "power1.inOut"
});

/*
 config is optional and can have any of the following properties: 
 - size [string] - the size to set and convert into px before it gets returned, like "cover" or "150% auto".
 - nativeWidth [number] - native width of the image (in pixels)
 - nativeHeight [number] - native height of the image (in pixels)
 
 note: if you can define the nativeWidth and nativeHeight, it helps because it can skip tasks like creating 
 an image and loading the URL to detect the native size automatically. Sometimes images don't load fast enough,
 so skipping that step avoids the whole issue. 
*/
function bgSize(element, config) {
  config = config || {};
	let e = gsap.utils.toArray(element)[0],
			cs = window.getComputedStyle(e),
			imageUrl = cs.backgroundImage,
      {nativeWidth, nativeHeight} = config,
			size = config.size || cs.backgroundSize,
			image, w, h, ew, eh, ratio;
	if (imageUrl && (!/\d/g.test(size) || size.indexOf("%") > -1)) {
    // console.log("there's a background-image and size doesn't have digits or does have a percentage"); 
    if (!nativeWidth || !nativeHeight) {
      image = new Image();
      image.setAttribute("src", imageUrl.replace(/(^url\("|^url\('|^url\(|"\)$|'\)$|\)$)/gi, ""));
      nativeWidth = image.naturalWidth;
      nativeHeight = image.naturalHeight;
    }
		ew = e.offsetWidth;
		eh = e.offsetHeight;
		if (!nativeWidth || !nativeHeight) {
      // console.log("bgSize() failed;", imageUrl, "hasn't loaded yet.");
      nativeWidth = ew;
      nativeHeight = eh;
    }
		ratio = nativeWidth / nativeHeight;
		if (size === "cover" || size === "contain") {
      // console.log("size is cover or contain: " + size);
			if ((size === "cover") === (nativeWidth / ew > nativeHeight / eh)) {
        // console.log("size is cover: " + size + " with portrait aspect ratio"); 
				h = eh;
				w = eh * ratio;
			} else {
        // console.log("size is cover: " + size + " with landscape aspect ratio"); 
				w = ew;
				h = ew / ratio;	
			}
		} else { // "auto" or %
      // console.log("size is auto and/or %: " + size); 
      size = size.split(" ");
      // console.log("size split:", size);
      // console.log("ew:", ew);
      // console.log("eh:", eh);
      // console.log("parseFloat(size[0]):", parseFloat(size[0]));
      // console.log("parseFloat(size[1]):", parseFloat(size[1]));

      let wNumber = parseFloat(size[0]);
      console.log("wNumber: ", wNumber);
      let hNumber = parseFloat(size[1]);
      console.log("hNumber: ", hNumber);
      
      // if both width and height are auto, use native sizes
      if (isNaN(wNumber) && isNaN(hNumber)) {
        console.log("width is auto and height is auto");
        wNumber = nativeWidth;
        hNumber = nativeHeight;
        console.log("wNumber: " + wNumber);
        console.log("hNumber: " + hNumber);
      }
      
      // if width is auto and height is not, use height percentage
      if (isNaN(wNumber) && !isNaN(hNumber)) {
        console.log("width is auto but height is not");
        wNumber = hNumber;
        console.log("wNumber: " + wNumber);
      }

      // if height is auto and width is not, use width percentage
      if (isNaN(hNumber) && !isNaN(wNumber)) {
        console.log("height is auto but width is not");
        hNumber = wNumber
        console.log("hNumber: " + hNumber);
      }
      
      w = ew * wNumber / 100;
      console.log("w: ", w);
      h = eh * hNumber / 100;
      console.log("h: ", h);
		}
		size = Math.ceil(w) + "px " + Math.ceil(h) + "px";
		config.size && (e.style.backgroundSize = size);
	}
  // console.log("size: " + size + " which is of type: " + typeof size);
	return size;
}
              
            
!
999px

Console