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

              
                <div class="viewport"></div>
              
            
!

CSS

              
                .viewport {
	background-image: url('https://images.unsplash.com/photo-1496276577120-a58058bc5935?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=dd6bd068e0f38725ecb1a9203906b993');
	background-size: cover;
	height: 100vh;
}
              
            
!

JS

              
                TweenMax.fromTo('.viewport', 2, {
		backgroundSize: function(i, e) { //I use a function-based value so that it works with multiple targets and so that it grabs the value EXACTLY at the time the tween starts.
			return getBGSize(e);
		}
	}, {
		backgroundSize: "300px 250px",
		yoyo: true,
		repeat: -1, 
		delay: 1, 
		ease: Power1.easeInOut
});


//this function converts the backgroundSize of an element from "cover" or "contain" or "auto" into px-based dimensions. To set it immediately, pass true as the 2nd parameter.
function getBGSize(element, setInPx) {
	var e = (typeof(element) === "string") ? document.querySelector(element) : element,
			cs = window.getComputedStyle(e),
			imageUrl = cs.backgroundImage,
			size = cs.backgroundSize,
			image, w, h, iw, ih, ew, eh, ratio;
	if (imageUrl && !/\d/g.test(size)) {
		image = new Image();
		image.setAttribute("src", imageUrl.replace(/(^url\("|^url\('|^url\(|"\)$|'\)$|\)$)/gi, "")); //remove any url() wrapper. Note: some browsers include quotes, some don't.
		iw = image.naturalWidth;
		ih = image.naturalHeight;
		ratio = iw / ih;
		ew = e.offsetWidth;
		eh = e.offsetHeight;
		if (!iw || !ih) {
			console.log("getBGSize() failed; image hasn't loaded yet.");
		}
		/*
		if (size === "cover") {
			if (iw / ew > ih / eh) { //must stretch height more than width
				h = eh;
				w = eh * ratio;
			} else {
				w = ew;
				h = ew / ratio;
			}
			
		} else if (size === "contain") {
			if (iw / ew > ih / eh) { //must fit to width
				w = ew;
				h = ew / ratio;
			} else {
				h = eh;
				w = eh * ratio;
			}
			
		} else if (size === "auto") {
			w = iw;
			h = ih;
		}
		*/
		//simplified/shorter version of above logic
		if (size === "cover" || size === "contain") {
			if ((size === "cover") === (iw / ew > ih / eh)) {
				h = eh;
				w = eh * ratio;
			} else {
				w = ew;
				h = ew / ratio;	
			}
		} else { //"auto"
			w = iw;
			h = ih;
		}
		size = Math.ceil(w) + "px " + Math.ceil(h) + "px";
		if (setInPx) {
			e.style.backgroundSize = size;
		}
	}
	return size;
}





              
            
!
999px

Console