123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="wrapper">
	<div class="controls">
		<button data-btn>Generate layout</button>
		<button data-color>Colour shuffle</button>
		<button data-copy>Copy CSS to clipboard</button>
		<button class="btn--fw" data-fw>Show full width grid</button>
	</div>

	<div class="aspect-box">
		<div class="grid">
			<div class="grid__item"></div>
			<div class="grid__item"></div>
			<div class="grid__item"></div>
			<div class="grid__item"></div>
			<div class="grid__item"></div>
			<div class="grid__item"></div>
			<div class="grid__item"></div>
		</div>
	</div>
	
	<div class="sidebar">
		<h3>Get the code:</h3>
		<div class="code-block" data-code></div>
	</div>
</div>
            
          
!
            
              @import url("https://fonts.googleapis.com/css?family=Lato:400,400i,700");

* {
	box-sizing: border-box;
}

body {
	background-color: darken(#232338, 10%);
	color: white;
	font-family: Lato, sans-serif;
	font-weight: 400;
}

button {
	padding: 15px 30px;
	border: none;
	background: linear-gradient(160deg, #6666cc, #333399);
	color: white;
	font-family: Lato, sans-serif;
	letter-spacing: 0.03em;
	transition: filter 500ms ease;
	
	&.is-copied {
		filter: hue-rotate(180deg);
	}
	
	&:hover {
		filter: hue-rotate(30deg);
	}
}

h3 {
	color: lighten(#6666cc, 10%);
}

.wrapper {
	@media (min-width: 1600px) {
		display: grid;
		grid-template-columns: [start] 2fr var(--column, 1fr) [end];
		grid-column-gap: 40px;
		overflow-x: hidden;
		
		&.is-closed {
			.aspect-box {
				grid-column: span 2;
			}
		}
	}
}

.grid {
	grid-column: start;
	// Aspect ratio stuff:
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

.controls {
	grid-column: start / end;
	display: flex;
	color: white;
	align-items: center;
	background-color: #232338;
	
	> *:not(:last-child) {
		margin-right: 10px;
	}
}

.sidebar {
	padding: 20px;
	width: 100%;
	overflow-x: scroll;
	position: relative;
}

.btn--fw {
	margin-left: auto;
	padding-left: 17px;
	padding-right: 17px;
	display: none;
	
	@media (min-width: 1600px) {
		display: block;
	}
}

.aspect-box {
	height: 0;
  overflow: hidden;
  padding-top: 56.25%;
  position: relative;
	grid-column: span var(--colSpan, 1);
}

/* Cool stuff starts here */
.grid {
	--h1: 312;
	--h2: 240;
	display: grid;
	grid-template-columns: repeat(16, 1fr);
	grid-template-rows: repeat(9, 1fr);
	grid-gap: 10px;
	background: linear-gradient(60deg, hsl(var(--h1), 100%, 50%), hsl(var(--h2), 50%, 40%));
	border-bottom: 1px solid rgba(black, 0.1);
	padding: 20px;
}

.grid__item {
	grid-column: var(--colStart, 1) / span var(--span, 3);
	grid-row: var(--rowStart, 1) / span var(--span, 3);
	border: 4px solid rgba(black, 0.5);
	mix-blend-mode: soft-light;
	
	&:nth-child(even) {
		filter: blur(var(--blur, 0px));
	}
}

.code-block {
	color: white;
	padding: 20px;
}

            
          
!
            
              const $wrapper = document.querySelector('.wrapper')
const $grid = document.querySelector('.grid')
const $items = [...document.querySelectorAll('.grid__item')]
const $btn = document.querySelector('[data-btn]')
const $color = document.querySelector('[data-color]')
const $copy = document.querySelector('[data-copy]')
const $codeBlock = document.querySelector('[data-code]')
const $fw = document.querySelector('[data-fw]')

const getRandomInt = (min, max) => {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

const calculateItemProps = () => {
	return $items.map(() => {
		return {
			span: getRandomInt(1, 7),
			blur: getRandomInt(1, 10)
		}
	})
}

const findMax = (size, totalTracks) => {
	return totalTracks - size
}

const randomBlur = (el, blur) => {
	el.style.setProperty('--blur', `${blur}px`)
}

const showStyles = (h1, h2, array) => {
	$codeBlock.innerHTML = 
`<pre>
.grid {
	--h1: ${h1};
	--h2: ${h2};
	display: grid;
	grid-template-columns: repeat(16, 1fr);
	grid-template-rows: repeat(9, 1fr);
	grid-gap: 10px;
	padding: 20px;
	background: 
		linear-gradient(60deg, 
		hsl(var(--h1), 100%, 50%), 	
		hsl(var(--h2), 50%, 40%));
}

.grid__item {
	grid-column: 
		var(--colStart, 1) / span var(--span, 3);
	grid-row:
		var(--rowStart, 1) / span var(--span, 3);
	border: 4px solid rgba(black, 0.5);
	mix-blend-mode: soft-light;
}

.grid__item:nth-child(even) {
	filter: blur(var(--blur, 0px));
}

${array}
</pre>`
}

const getItemStyles = ({ span, posX, posY, blur }, index) => {
	return (
`.grid__item:nth-child(${index + 1}) {
	--colStart: ${posX};
	--rowStart: ${posY};
	--span: ${span};
	--blur: ${blur};
}

`
	)
}

let itemStylesArray = []

let h1
let h2

const generateLayout = () => {
	const itemsSizes = calculateItemProps()
	itemStylesArray = []
	h1 = getComputedStyle($grid).getPropertyValue('--h1')
	h2 = getComputedStyle($grid).getPropertyValue('--h2')
	
	itemsSizes.map(({ span, blur }, index) => {
		const $el = $items[index]
		const maxPosX = findMax(span, 18)
		const maxPosY = findMax(span, 11)
		const posX = getRandomInt(1, maxPosX)
		const posY = getRandomInt(1, maxPosY)
		
		$el.style.setProperty('--span', span)
		$el.style.setProperty('--colStart', posX)
		$el.style.setProperty('--rowStart', posY)
		
		randomBlur($el, blur)
		
		$el.props = {
			span, posX, posY, blur
		}
		
		itemStylesArray.push(getItemStyles($el.props, index))
	})
	
	showStyles(h1, h2, itemStylesArray.join(' '))
}


const colorShuffle = () => {
	h1 = getRandomInt(0, 361)
	h2 = getRandomInt(h1 + 30, h1 + 90)
	$grid.style.setProperty('--h1', h1)
	$grid.style.setProperty('--h2', h2)
	showStyles(h1, h2, itemStylesArray.join(' '))
}


const copyToClipboard = () => {
	const range = document.createRange()
	range.selectNode($codeBlock)
	window.getSelection().addRange(range)
	
	const btnWidth = $copy.getBoundingClientRect().width
	
	try {
		const success = document.execCommand('copy')
		const msg = success ? 'Copied' : 'Unsuccessful'
		$copy.style.width = `${btnWidth}px`
		$copy.innerText = msg
		$copy.classList.add('is-copied')
		console.log(msg)
	} catch(err) {
		console.log('A problem occurred')
	}
	
	window.getSelection().removeAllRanges()
	
	setTimeout(() => {
		$copy.classList.remove('is-copied')
		$copy.innerText = 'Copy CSS to clipboard'
		$copy.style.width = 'auto'
	}, 3000)
}

generateLayout()

$btn.addEventListener('click', generateLayout)

$color.addEventListener('click', colorShuffle)

$copy.addEventListener('click', copyToClipboard)

// Close sidebar
let open = true

$fw.addEventListener('click', () => {
	if (open) {
		$wrapper.classList.add('is-closed')
		$fw.innerText = 'Revert'
		open = false
	} else {
		$wrapper.classList.remove('is-closed')
		$fw.innerText = 'Show full width grid'
		open = true
	}
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console