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

              
                <main>
	<h1>How Does It Scale?</h1>
	
	<form id="size">
		<button type="button" class="is-active">cover</button>
		<button type="button">contain</button>
		<button type="button">100% auto</button>
		<button type="button">auto 100%</button>
		<button type="button">500px auto</button>
		<button type="button">auto 500px</button>
		<br><br>
		<label for="form-name">Custom <code>background-size</code>:</label><br>
		<input id="form-name" type="text" name="custom" value="cover">
	</form>
	
	<form id="position">
		<div class="position-grid">
			<button type="button">top left</button>
			<button type="button">top center</button>
			<button type="button">top right</button>
			<button type="button">center left</button>
			<button type="button" class="is-active">center</button>
			<button type="button">center right</button>
			<button type="button">bottom left</button>
			<button type="button">bottom center</button>
			<button type="button">bottom right</button>
		</div>
		<br><br>
		<label for="form-name">Custom <code>background-position</code>:</label><br>
		<input id="form-name" type="text" name="custom" value="center">
	</form>
	
	<div id="demo"></div>
	
	<h2>References</h2>
	<ul>
		<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-size" target="_blank" rel="noopener">MDN: background-size</a></li>
		<li><a href="https://blogs.adobe.com/creativecloud/a-primer-to-background-positioning-in-css/" target="_blank" rel="noopener">Adobe: “A Primer To Background Positioning In CSS” by Sara Soueidan</a></li>
	</ul>
</main>

              
            
!

CSS

              
                html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	display: block; /* Fix IE rendering main as inline */
	margin: 0 auto;
	max-width: 42rem;
	padding-left: 1rem;
	padding-right: 1rem;
}

img {
	display: block;
	max-width: 100%;
}

form {
	margin-bottom: 1rem;
}

button {
	border: 1px solid black;
}

button.is-active {
	background-color: blue;
	color: white;
}

.position-grid {
	display: inline-grid;
	grid-template-columns: repeat(3, 1fr);
}

/* https://codepen.io/jakob-e/pen/YbrRYN */
#demo {
  width: 100vw;
	height: 500px;
  margin-right: calc(50% - 50vw);
  margin-left: calc(50% - 50vw);
	background-color: #eee;
	background-image: url('https://images.unsplash.com/photo-1555685812-4b943f1cb0eb?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ');
	background-size: cover;
	background-position: center;
	background-repeat: no-repeat;
	user-select: none;
}

              
            
!

JS

              
                /**
 * QSA = "Query Selector All" that returns an Array instead of a NodeList.
 * @param  {String}   selector
 * @param  {Element}  context
 * @return {Array}
 */
function qsa(selector) {
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
  return Array.prototype.slice.call(context.querySelectorAll(selector));
}

/**
 * Return the index for a given element.
 * @param  {Element}  elem
 * @return {Number}
 */
function getIndex(elem) {
  var i = 0;
  while ((elem = elem.previousElementSibling)) i++;
  return i;
}

/**
 * Is element a button?
 * @param  {Element}
 * @return {Boolean}
 */
function isButton(elem) {
	return elem.tagName === 'BUTTON';
}

/**
 * Is event an arrow key?
 * @param  {Event}
 * @return {Boolean}
 */
function isArrowKey(event) {
	return [
		'ArrowLeft',
		'ArrowRight',
		'ArrowUp',
		'ArrowDown',
	].indexOf(event.key) >= 0;
}

/**
 * Init form to customize image properties.
 * @param  {Element} form
 * @param  {Object}  params
 * @return {void}
 */
function initForm(form, params) {
	var input = form.querySelector('input[name="custom"]');
	var buttons = qsa('button[type="button"]', form);
	var buttonValues = buttons.map(function (button) { return button.innerText; });

	/**
	 * Reset button states.
	 * @return {void}
	 */
	function resetButtons() {
		buttons.forEach(function (button) {
			button.classList.remove('is-active');
		});
	}

	// Buttons
	form.addEventListener('click', function (event) {
		// Only watch buttons
		if (!isButton(event.target)) {
			return;
		}

		var button = event.target;
		var value = button.innerText;

		event.preventDefault();
		button.focus();

		// Update image
		demo.style[params.prop] = value;
		// Update buttons
		resetButtons();
		button.classList.add('is-active');
		// Update input
		input.value = value;
	});

	// // Auto-click focused buttons
	// form.addEventListener('focusin', function (event) {
	// 	// Only watch buttons
	// 	if (!isButton(event.target)) {
	// 		return;
	// 	}
	// 
	// 	event.target.click();
	// });
	
	// Custom input
	input.addEventListener('input', function (event) {
		var value = input.value;
		var match = buttonValues.indexOf(value);

		// Update image
		demo.style[params.prop] = value;
		// Reset buttons
		resetButtons();
		
		if (!value) {
			buttons[0].classList.add('is-active');
		} else if (match >= 0) {
			buttons[match].classList.add('is-active');
		}
	});
}

initForm(size, { prop: 'background-size' });
initForm(position, { prop: 'background-position' });

// Arrow keys - size
var sizeButtons = qsa('button[type="button"]', size);
size.addEventListener('keydown', function (event) {
	// Only watch arrow keys on buttons
	if (!isButton(event.target) || !isArrowKey(event)) {
		return;
	}
	
	var button = event.target;
	var index = getIndex(button);
	var nextIndex = index;
	var numButtons = sizeButtons.length;
	
	event.preventDefault();
	
	if (event.key === 'ArrowLeft') {
		nextIndex -= 1;
	} else if (event.key === 'ArrowRight') {
		nextIndex += 1;
	}

	if (nextIndex < 0) {
		nextIndex += numButtons;
	} else if (nextIndex >= numButtons) {
		nextIndex -= numButtons;
	}
	
	var nextButton = sizeButtons[nextIndex];
	nextButton.click();
});

// Arrow keys - position
var positionButtons = qsa('button[type="button"]', position);
position.addEventListener('keydown', function (event) {
	// Only watch arrow keys on buttons
	if (!isButton(event.target) || !isArrowKey(event)) {
		return;
	}
	
	var button = event.target;
	var index = getIndex(button);
	var nextIndex = index;
	var numButtons = positionButtons.length;
	
	event.preventDefault();
	
	if (event.key === 'ArrowLeft') {
		nextIndex -= 1;
		if (nextIndex % 3 === 2) nextIndex += 3;
	} else if (event.key === 'ArrowRight') {
		nextIndex += 1;
		if (nextIndex % 3 === 0) nextIndex -= 3;
	} else if (event.key === 'ArrowUp') {
		nextIndex -= 3;
	} else if (event.key === 'ArrowDown') {
		nextIndex += 3;
	}

	if (nextIndex < 0) {
		nextIndex = 2; // += numButtons;
	} else if (nextIndex >= numButtons) {
		nextIndex -= numButtons;
	}
	
	var nextButton = positionButtons[nextIndex];
	nextButton.click();
});

// Make image resizable
interact('#demo').resizable({
  edges: { bottom: true, top: true },
}).on('resizemove', function(event) {
  event.target.style.height = event.rect.height + 'px';
});

              
            
!
999px

Console