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>Does your browser snap too soon?</h1>
	<div id="result">Testing…</div>
	
	<section>
		<h2>Demo</h2>
		<p>Scroll this slider:</p>
		<div id="demo-1" class="demo colors">
			<div>1</div>
			<div>2</div>
			<div>3</div>
			<div>4</div>
		</div>
		<p>And see how (or if) this slider slides along with it:</p>
		<div id="demo-2" data-slide-with="demo-1" class="demo colors">
			<div>5</div>
			<div>6</div>
			<div>7</div>
			<div>8</div>
		</div>
		<p>In some browsers, setting <code>scrollLeft</code> computes the position of the snap destination instead of the value you defined. For example, if each slide is 400px and you set <code>slider.scrollLeft = 200px</code> it will scroll to 400px instead of 200px.</p>
		<h3>Screencasts</h3>
		<ul>
			<li><a href="https://youtu.be/BO-zXtS47vI" target="_blank" rel="noopener">Chrome</a></li>
			<li><a href="https://youtu.be/i9YbrJ8_t_U" target="_blank" rel="noopener">iOS</a></li>
		</ul>
	</section>
	
	<section>
		<h2>How to Fix</h2>
		<p>In browsers that snap immediately (e.g., Chrome), you’ll need to disable CSS Scroll Snap on the synced slider: <code>scroll-snap-type: none;</code></p>
		<div id="demo-3" class="demo colors-v2">
			<div>9</div>
			<div>10</div>
			<div>11</div>
			<div>12</div>
		</div>
		<br>
		<div id="demo-4" data-slide-with="demo-3" class="demo colors-v2">
			<div>13</div>
			<div>14</div>
			<div>15</div>
			<div>16</div>
		</div>
		<code><pre>.snaps-immediately .your-class {
	scroll-snap-type: none;
}</pre></code>
		<p><strong>Note</strong>: Keep Scroll Snap enabled for the primary slider. Only disable the synced slider, because that’s the one affected by the “snap immediately” bug.</p>
	</section>
	
	<section>
		<h2>Browser Support <small>(as of 2019-10-01)</small></h2>
		<dl>
			<dt>IE</dt><dd>❓ Untested</dd>
			<dt>Edge</dt><dd>❓ Untested</dd>
			<dt>Firefox</dt><dd>⚠️ 68+ Snaps immediately</dd></dd>
			<dt>Chrome</dt><dd>⚠️ 69+ Snaps immediately</dd>
			<dt>Safari</dt><dd>✅ 11+</dd>
			<dt>Opera</dt><dd>⚠️ Snaps immediately</dd>
			<dt>iOS Safari</dt><dd>✅ 11+</dd>
			<dt>Android Browser</dt><dd>❓ Untested</dd>
			<dt>Chrome for Android</dt><dd>⚠️ 76+ Snaps immediately</dd>
		</dl>
	</section>

	<section>
		<h2>References</h2>
		<ul>
			<li><a href="https://www.caniuse.com/#feat=css-snappoints" target="_blank" rel="noopener">Can I use… CSS Scroll snap</a></li>
		</ul>
	</section>
</main>

              
            
!

CSS

              
                /* -------------------------------- */
/* Base                             */
/* -------------------------------- */

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;
}

section {
	margin-bottom: 3rem;
}

h2,
h3 {
	margin-bottom: 0;
}

pre {
	border: 1px solid #ccc;
	padding: 1rem;
	overflow: auto;
	-webkit-overflow-scroll: touch;
}

/* Colors */
.colors > *:nth-child(7n + 1) { background-color: red; }
.colors > *:nth-child(7n + 2) { background-color: orange; }
.colors > *:nth-child(7n + 3) { background-color: yellow; }
.colors > *:nth-child(7n + 4) { background-color: green; }
.colors > *:nth-child(7n + 5) { background-color: blue; }
.colors > *:nth-child(7n + 6) { background-color: indigo; }
.colors > *:nth-child(7n + 7) { background-color: violet; }

/* Colors v2 */
.colors-v2 > *:nth-child(7n + 1) { background-color: yellow; }
.colors-v2 > *:nth-child(7n + 2) { background-color: green; }
.colors-v2 > *:nth-child(7n + 3) { background-color: blue; }
.colors-v2 > *:nth-child(7n + 4) { background-color: indigo; }
.colors-v2 > *:nth-child(7n + 5) { background-color: violet; }
.colors-v2 > *:nth-child(7n + 6) { background-color: red; }
.colors-v2 > *:nth-child(7n + 7) { background-color: orange; }



/* -------------------------------- */
/* Definition List w/ Leading Lines */
/* https://codepen.io/tannerhodges/pen/YrPMgv */
/* -------------------------------- */

dl {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-column-gap: 1em;
  grid-row-gap: 0.5em;
  margin: 1em;
  line-height: 1.3;
}

dt {
  position: relative;
  grid-column: 1;
  font-weight: bold;
  overflow: hidden;
}

dd {
  grid-column: 2;
  margin-left: 0;
}

dt::after {
  position: absolute;
  top: 0.6em;
  width: 100%;
  margin-left: 0.75em;
  border-bottom: 1px dotted #ccc;
  content: '';
}



/* ------------------------------ */
/* Test Snap                      */
/* ------------------------------ */

/* NOTE: These styles are minified into the `snapsImmediately` JavaScript */
/* so it can be easily copy/pasted into your own projects. 👇 See JS tab. */

/* .test-snap {
	display: flex;
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100vw;
	overflow: scroll;
	scroll-snap-type: x mandatory;
	-webkit-overflow-scrolling: touch;
	opacity: 0;
}

.test-snap > * {
	flex: 1 1 0%;
	min-width: 50vw;
	height: 1rem;
	scroll-snap-align: start;
} */



/* ------------------------------ */
/* Demo                           */
/* ------------------------------ */

#result {
	margin-bottom: 2rem;
}

.demo {
	display: flex;
	width: 100%;
	overflow-x: scroll;
	scroll-snap-type: x mandatory;
	-webkit-overflow-scrolling: touch;
}

.demo > * {
	flex: 1 1 0%;
	min-width: 50%;
	padding: 1rem;
	text-align: center;
	scroll-snap-align: start;
}

/* Force broken demo */
#demo-2 {
	scroll-snap-type: x mandatory !important;
}



/* ------------------------------ */
/* Demo - Fix                     */
/* ------------------------------ */

.snaps-immediately [data-slide-with] {
	scroll-snap-type: none;
}

              
            
!

JS

              
                /**
 * Check whether this browser immediately snaps elements
 * when you programmatically update `el.scrollLeft`.
 * @type {Boolean}
 */
var snapsImmediately = (function () {
	// Build test slider
	var test = document.createElement('div');
	test.className = 'test-snap colors';
	test.innerHTML = '<div>1</div><div>2</div><div>3</div><div>4</div><style>.test-snap{display:-webkit-box;display:-ms-flexbox;display:flex;position:fixed;bottom:0;left:0;width:100vw;overflow:scroll;-ms-scroll-snap-type:x mandatory;scroll-snap-type:x mandatory;-webkit-overflow-scrolling:touch;opacity:0}.test-snap>*{-webkit-box-flex:1;-ms-flex:1 1 0%;flex:1 1 0%;min-width:50vw;height:1rem;scroll-snap-align:start}</style>';
	
	// Add test element to the page
	document.body.appendChild(test);
	
	// Test scroll position
	var scrollPosition = Math.ceil(window.innerWidth * 0.75); // Round value because `scrollLeft` only accepts whole numbers
	test.scrollLeft = scrollPosition;
	var snapsImmediately = test.scrollLeft !== scrollPosition;

	// Add class to document root
	if (snapsImmediately) {
		document.documentElement.className += ' snaps-immediately';
	}
	
	// Remove test element
	test.parentNode.removeChild(test);
	
	return snapsImmediately;
})();

// ------------------------------
// Demo
// ------------------------------

// Show test result on page load
var result = document.getElementById('result');
var message = '😃 Your browser snaps smoothly.';

if (snapsImmediately) {
	message = '😱 Watch out! Your browser snaps immediately.';
}

result.innerHTML = message;

/**
 * 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));
}

// Sync scroll between demo sliders
qsa('[data-slide-with]').forEach(function (slider) {
	var id = slider.getAttribute('data-slide-with');
	var target = document.getElementById(id);

	// Only sync with targets that exist
	if (!target) {
		return;
	}

	target.addEventListener('scroll', function (event) {
		slider.scrollLeft = target.scrollLeft;
		console.log(target.scrollLeft, slider.scrollLeft);
	});
});

              
            
!
999px

Console