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

              
                - let data = {
- 	c0: ['#ff8a00', 'solid RGB background at the bottom'],
- 	c1: ['#e52e71', 'RGB value of overlay'],
- 	a: [50, 'alpha value of overlay']
- };
- let chs = ['r', 'g', 'b'];
- let s = []
- for(let p in data) {
	- if(p !== 'a') {
	- 	let idx = +p.charAt(1);
	- 	data[p][2] = data[p][0].match(/[0-9a-f]{2}/gi).map(c => parseInt(c, 16));
	- 	data[p][2].forEach((ch, i) => { s.push(`--${chs[i]}${idx}: ${ch}`) })
	- }
	- else {
	- 	data.a[2] = +(data.a[0]*.01).toFixed(2);
	- 	s.push(`--a: ${data.a[2]}`)
	- }
- }

body(style=s.join(';'))
	pre(data-lang='SCSS')
		- for(let p in data) {
			span.token--decl(data-comm=data[p][1])
				label.token--prop $#{p}
				if p == 'a'
					span.ctrl
						button.token--val(aria-haspopup='true' aria-expanded='false' aria-controls=`pop`): output(for=p) #{data[p][2]}
						form#pop
							input(id=p type='range' value=data[p][0])
				else
					- let idx = +p.charAt(1);
					input(id=p type='color' data-i=idx value=data[p][0])
					output.token--val(for='p')
						span.token--func rgb
						span.token--oper (
						span.token--args
							- data[p][2].forEach((ch, i) => {
								span.token--arg.ch(style=`--ch: var(--${chs[i]}${idx})`)
							- });
						span.token--oper )
		- }

	article(data-case='two layers')
		.back
		.over
		pre(data-lang='Haml')
			span.token--elem: span.token--sel .base
			span.token--elem: span.token--sel .over
		pre(data-lang='SCSS')
			span.token--rset
				span.token--sel .base
				span.token--punc {
				span.token--decl
					span.token--prop background
					span.token--val
						span.token--var $c0
				span.token--punc }
			span.token--rset
				span.token--sel .over
				span.token--punc {
				span.token--decl
					span.token--prop background
					span.token--val
						span.token--func rgba
						span.token--oper (
						span.token--args
							span.token--arg
								span.token--var $c1
							span.token--arg
								span.token--var $a
						span.token--oper )
				span.token--punc }

	article(data-case='one layer')
		.equiv
		pre(data-lang='Haml')
			span.token--elem: span.token--sel .equiv
		pre(data-lang='SCSS')
			span.token--rset
				span.token--sel .equiv
				span.token--punc {
				span.token--decl
					span.token--prop background
					span.token--val
						span.token--func res-col
						span.token--oper (
						span.token--args
							span.token--arg
								span.token--var $c0
							span.token--arg
								span.token--var $c1
							span.token--arg
								span.token--var $a
						span.token--oper )
				span.token--punc }
              
            
!

CSS

              
                $gap: .125em;
$dark: #171717;
$light: #eee;
$hl: #f44336;
$font-m: 1.25;

$track-w: 12.5em;
$track-h: .25em;
$track-r: .5*$track-h;
$thumb-d: 1.5em;
$thumb-r: .5*$thumb-d;
$dist: $track-w - $thumb-d;
$panel-w: 23.5em;
$break: $font-m*(2*$panel-w + $gap) $font-m*$panel-w;

$t: .3s;

@mixin swatch() {
	--d: calc(#{$thumb-d} - 8px);
	margin: 4px 2px;
	box-sizing: border-box;
	border: none;
	width: var(--d); height: var(--d);
	border-radius: 50%
}

@mixin track() {
	border: none;
	width: 100%; height: $track-h;
	border-radius: $track-r;
	background: $light;
	background: radial-gradient(circle at var(--pos), transparent $thumb-r, $light 0);
	color: transparent
}

@mixin thumb($m: 0) {
	margin-top: $m;
	border: none;
	width: $thumb-d; height: $thumb-d;
	border-radius: 50%;
	transform: scale(calc(1 - var(--not-hl)*.35));
	background: $hl;
	cursor: ew-resize;
	transition: transform $t ease-out
}

* {
	box-sizing: border-box;
	margin: 0;
	border: none;
	padding: 0;
	background: transparent;
	color: inherit;
	font: inherit
}

html {
	overflow-x: hidden;
	min-width: 180px;
	font: #{$font-m*1em}/ 1.375 ubuntu mono, consolas, monaco, monospace;
}

body {
	display: grid;
	grid-template: max-content 1fr/ 1fr 1fr;
	grid-gap: $gap;
	height: 100vh
}

pre {
	position: relative;
	z-index: 2;
	padding: .25em;
	background: $dark;
	color: $light;
	white-space: normal;
	
	&:first-child {
		grid-column-end: span 2
	}
	
	&::before {
		float: right;
		padding: 0 .25em;
		border-radius: 3px;
		background: #ffa800;
		color: $dark;
		font-size: .75em;
		font-weight: 600;
		content: attr(data-lang)
	}
	
	* {
		clear: both
	}
}

button {
	cursor: pointer;
	
	&:focus { outline: solid 0 transparent }
	
	output {
		display: inline-block;
		padding: 0 .375em;
		border-radius: 3px;
		transition: $t ease-out
	}
}

form {
	--exp: 0;
	display: grid;
	place-content: center;
	position: absolute;
	top: calc(100% + 3px); left: -1em;
	padding: .5em;
	border-radius: 5px;
	transform-origin: 1px 0;
	transform: scale(var(--exp));
	box-shadow: 2px 2px 5px mix($dark, $light);
	background: $dark;
	transition: transform $t ease-out;
	
	[aria-expanded='true'] + & { --exp: 1 }
}

article {
	display: grid;
	grid-template-rows: repeat(2, max-content) 1fr;
	position: relative;
	z-index: 0;
	
	div {
		position: absolute;
		top: 0; right: 0; bottom: 0; left: 0;
	}
	
	pre {
		margin: .5em auto;
		width: 100%; max-width: $panel-w;
		border-radius: 5px;
	}
	
	&::after {
		z-index: 2;
		place-self: end end;
		margin: 1em .25em .25em;
		padding: 0 .25em;
		border-radius: 5px;
		background: $light;
		color: $dark;
		font: 600 1em/ 1.5 segoe script, comic sans ms, purisa, cursive;
		content: attr(data-case)
	}
}

input {
	--hl: 0;
	--not-hl: calc(1 - var(--hl));
	cursor: pointer;
	transition: $t ease-out;
	
	&[type='color'] {
		box-sizing: border-box;
		margin-right: .25em;
		padding: 0 2px;
		width: $thumb-d; height: $thumb-d;
		border-radius: 3px;
		background: rgba(#{red($light)}, #{green($light)}, #{blue($light)}, var(--hl));
		transition-property: background-color;
		
		&::-webkit-color-swatch-wrapper { padding: 0 }

		&::-webkit-color-swatch { @include swatch }

		&::-moz-color-swatch { @include swatch }
	}
	
	&[type='range'] {
		&, &::-webkit-slider-thumb, 
		&::-webkit-slider-runnable-track { -webkit-appearance: none }
		
		--pos: calc(#{$thumb-r} + var(--a)*#{$track-w - $thumb-d});
		width: $track-w;
		min-height: $thumb-d; height: $track-h;
		opacity: var(--exp);
		filter: grayScale(var(--not-hl));
		transition-property: filter, opacity;
		transition-timing-function: ease-out, cubic-bezier(.75,0,.25,1);
		
		&::-webkit-slider-runnable-track { @include track() }
		&::-moz-range-track { @include track() }
		&::-ms-track { @include track() }
		
		&::-ms-fill-lower { display: none }
		
		&::-webkit-slider-thumb { @include thumb($m: calc(#{$track-r} - #{$thumb-r})) }
		&::-moz-range-thumb { @include thumb() }
		&::-ms-thumb { @include thumb() }
	}

	&:focus {
		--hl: 1;
		outline: solid 0 transparent
	}
}

.token {
	&--elem, &--rset, &--decl {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	
	&--sel {
		--g: var(--gg);
		color: mix(#fff, #e52e71, 25%);
	}
	
	&--decl {
		margin: 2px 0;
		:not(.token--rset) > &::after { content: ';' }
	}
	
	&--prop {
		margin-right: .25em;
		color: mix(#fff, #9c27b0, 25%);
		
		&::after { content: ':' }
	}
	
	&--val output { color: mix(#fff, $hl, 25%) }
	
	&--func {
		color: mix(#fff, #03a9f4, 25%)
	}
	
	&--punc {
		margin: 0 .25em;
	}
	
	&--arg {
		color: mix(#fff, #4caf50, 25%);
		
		&:not(:last-child) {
			margin-right: .25em;
			
			&::after { content: ',' }
		}
	}
	
	&--var { color: mix(#fff, #8bc34a, 25%) }
}

[class*='token']::after { color: $light }

.ctrl { position: relative }

.ch {
	counter-reset: ch var(--ch);
	&::before { content: counter(ch) }
}

[aria-expanded='true'], :focus {
		> output {
		background: $hl;
		color: $light
	}
}

.back { background: RGB(var(--r0), var(--g0), var(--b0)) }
.over { background: RGBA(var(--r1), var(--g1), var(--b1), var(--a)) }

.equiv {
	--r: calc(var(--r0) + (var(--r1) - var(--r0))*var(--a));
	--g: calc(var(--g0) + (var(--g1) - var(--g0))*var(--a));
	--b: calc(var(--b0) + (var(--b1) - var(--b0))*var(--a));
	background: RGB(var(--r), var(--g), var(--b))
}

@media (max-width: #{nth($break, 1)}) {
	body {
		grid-template: max-content 1fr 1fr/ 1fr
	}
	
	pre:first-child { grid-column-end: span 1 }
}

@media (max-width: #{nth($break, 2)}) {
	html { font-size: 1em }
}

@media (max-width: #{$panel-w}) {
	.token {
		&--rset {
			&, & .token--decl {
				display: inline;
				white-space: pre-wrap
			}
			.token--sel + .token--punc { --str: ' ' }
			.token--punc:last-child::before, .token--punc::after { content: '\A'var(--str, '') }
		}
	}
}

@media (max-width: 300px) {
	article pre { border-radius: 0 }
	
	.token {
		article:last-of-type &--rset &--val::before {
			content: '\A    '
		}
	}
}

@media (max-width: 240px) { html { font-size: .75em } }
              
            
!

JS

              
                let _BODY = document.body, 
		CHS = ['r', 'g', 'b'];

let exp = false;

function toRGB(hex) {
	return hex.match(/[0-9a-f]{2}/gi).map(c => parseInt(c, 16))
}

function update(e) {
	let _tg = e.target;
	
	switch(true) {
		case _tg.type === 'color':
			let rgb = toRGB(_tg.value),
					i = +_tg.dataset.i;
			
			rgb.forEach((ch, j) => _BODY.style.setProperty(`--${CHS[j]}${i}`, ch));
			break;
		case _tg.type === 'range':
			let val = +(_tg.value*.01).toFixed(2);
			
			_BODY.style.setProperty('--a', val);
			document.querySelector('output[for=a]').textContent = val;
			break;
	}
};

function trigger(_el) {
	if(_el === _BODY) return null;
	if(_el.hasAttribute('aria-haspopup') && _el.getAttribute('aria-haspopup')) return _el;
	return trigger(_el.parentNode)
}

function inForm(_el) {
	const re = RegExp('pop', 'i');
	
	return re.test(_el.id) || re.test(_el.parentNode.id)
}

function toggleFrom(btn) {
	btn.setAttribute('aria-expanded', exp = !exp);
};

addEventListener('input', update, false);
addEventListener('change', update, false);
addEventListener('click', e => {
	let _tg = e.target, 
			_trig = trigger(_tg);
	
	if(_trig && !exp) toggleFrom(_trig);
	else {
		if(exp && !inForm(_tg) && !_trig)
			toggleFrom(document.querySelector('[aria-expanded=true]'))
	}
}, false);
              
            
!
999px

Console