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

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<div id="title"><h3>random gradient generator</h3>
  <p id="gradientInfo">click the button to start</p>
  </div>  
 <div class="buttonholder"><button id="changeButton">new gradient</button>
  </div>
  <div id="boxContainer">
<div class="box"></div>
<div class="box"></div>
    </div>
  <ul id="list">
  </ul>
</body>
</html>
              
            
!

CSS

              
                :root {
  --gradient: #222;
  --gradientTwo: #222;
  --gradientThree: #222;
}

body {
background: var(--gradient)
}

#title {
  background: linear-gradient(#cff, #fcc);
  text-align: center;
  width: 300px;
  margin: 0 auto;
  margin-top: 5em;
  padding: 20px;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
  font-family: monospace;
}

.buttonholder {
  background: var(--gradientTwo);
  text-align: center;
  width: 300px;
  margin: 0 auto;
  padding: 20px;
}

button {
  color: #222;
  background-color: #eee;
  font-size: .75rem;
  padding: .25em;
}

.box {
  width: 75px;
  height: 75px;
  background: var(--gradientThree);
  text-align: center;
  margin: 0 auto;
}

#boxContainer {
  display: flex; 
}

ul {
  font-family: monospace;
  text-align: center;
  color: white;
  list-style: none;
  line-height: 2em;
  background-color: #222;
  margin: auto;
  width: 300px;
  height: 100px;
  overflow: auto;
  scrollbar-color: #222 #444;
  scrollbar-width: thin;
  transition: all 0.5s ease-out;
}

ul::-webkit-scrollbar {
    width: 1em;
}

li {
  margin-right: 2.5em;
}

li:hover {
font-weight: bold;
cursor: pointer;
}

              
            
!

JS

              
                const btn = document.getElementById("changeButton")
const ul = document.getElementById("list")
const infoText = document.getElementById("gradientInfo")

//event handlers
const newGradient = () => {
  const randomGradient = randomColors()
  setAllGradients('--gradient', '--gradientTwo', '--gradientThree', randomGradient)
  setTextAndHistory(infoText, randomGradient) 
  underlineSelection()
}
const setGradientFromHistory = () => {
  const target = event.target;
  if (target.tagName != 'LI') return; // event delegation
  const gradientFromHistory = target.innerText.split(" ")
  setAllGradients('--gradient', '--gradientTwo', '--gradientThree', gradientFromHistory)
  setText(infoText, gradientFromHistory)
  underlineSelection()
}

const setGradient = (gradient, degree, colorArray) => {
  const [colorOne, colorTwo, colorThree] = colorArray
  document.documentElement.style.setProperty(gradient, `repeating-linear-gradient(${degree}deg, ${colorOne}, ${colorTwo} 60px, ${colorThree} 80px`)
}
const setAllGradients = (one, two, three, gradient) => {
  setGradient(one, 90, gradient)
  setGradient(two, 180, gradient)
  setGradient(three, 120, gradient)
}
const setTextAndHistory = (textbox, colorArray) => {
  setText(textbox, colorArray)
  setHistory(colorArray)
}
const setHistory = ([colorOne, colorTwo, colorThree]) => {
  const history = document.createElement("li");
  history.style.color= `${colorOne}`;
  history.innerHTML = `${colorOne} ${colorTwo} ${colorThree}`;              
  ul.insertBefore(history, ul.firstChild);
 }
const setText = (textbox, [colorOne, colorTwo, colorThree]) => {
  textbox.innerText = `${colorOne}, ${colorTwo}, ${colorThree}`  
}
const underlineSelection = () => {
  const listArray = Array.from(document.querySelectorAll('ul>li'))
  listArray.forEach(li => li.innerHTML.slice(0,7) == infoText.innerHTML.slice(0,7) ?
                      li.style.textDecoration = 'underline' : li.style.textDecoration = 'none')
}
const randomColors = () => {
  return [`#${Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, 0)}`, `#${Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, 0)}`, `#${Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, 0)}`]}
const randomDegree = () => Math.floor(Math.random() * 360) + 1

btn.addEventListener('click', newGradient)
ul.addEventListener('click', setGradientFromHistory)
              
            
!
999px

Console