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

              
                <div id="root"></div>
              
            
!

CSS

              
                :root {
    --button-color-bg: #fcfcfc;
    --button-color-border: #777;
}
body {
    font-family: sans-serif;
}
.button {
    background-color: var(--button-color-bg);
    border-radius: 4px;
    border: 1px solid var(--button-color-border);
    cursor: pointer;
    height: 36px;
    padding-left: 40px;
    padding-right: 40px;
    transition: background-color 120ms ease, 
        opacity 1s ease;
}
.button:hover {
    background: #c8e6c8;
}
.button:active {
    box-shadow: 0 2px 6px #555 inset;
    background-color: #999;
}
.button--add {
    margin-left: 18px;
    width: 194px;
}
.button--faded {
    opacity: 0.05;
}
.button--faded:hover {
    opacity: 0.9;
}
.generator {
    animation-name: animateBg-left;
    animation-duration: 14s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
    align-items: center;
    display: flex;
    padding-top: 2em;
    box-sizing: border-box;
    flex-direction: column;
    min-height: 100vh;
}
.title {
    max-width: 314px;
    margin-top: 0;
}
.container {
    background: #fff;
    border-radius: 0.25em;
    box-shadow: 0 3px 14px #000;
    margin-bottom: 1rem;
    min-width: 20rem;
    padding: 2em;
}
.label {
    font-weight: 400;
}
.presets {
    margin: 10px 0;
}
.preset {
    background: gray;
    border-radius: 2px;
    border: 1px solid var(--button-color-border);
    cursor: pointer;
    display: inline-block;
    height: 36px;
    margin-right: 10px;
    width: 36px;
}
.input--textlike {
    border-radius: 2px;
    border: 1px solid var(--button-color-border);
    display: inline-block;
    height: 26px;
    margin: 0 10px 10px;
}
.direction {
    width: 100px;
}
.duration {
    text-align: center;
    width: 50px;
}
.color-list {
    padding: 0 0 0 18px;
}
.color {
    margin-bottom: 10px;
    position: relative;
}
.color__input,
.color__remove {
    box-sizing: border-box;
    cursor: pointer;
    vertical-align: middle;
}
.color__label {
    background-color: #fafafa;
    border-left: 1px solid #888;
    cursor: pointer;
    font-family: Courier New, monospace;
    padding: 7px 8px;
    position: absolute;
    right: 40px;
    top: 1.5px;
}
.color__input {
    border-color: #aaa;
    border-block: 0 none rgba(0,0,0,0);
    border-collapse: collapse;
    border-inline: 0 none rgba(0,0,0,0);
    border-spacing: 0;
    border-style: solid;
    border-width: 1px;
    height: 36px;
    padding: 0;
    width: 280px;
}
.color__remove {
    background-color: var(--button-color-bg);
    border-color: var(--button-color-border);
    border-radius: 0 5px 5px 0;
    border-style: solid;
    border-width: 1px 1px 1px 0;
    display: inline-block;
    height: 34px;
    text-align: center;
    transition: background-color 200ms ease;
    width: 34px;
}
.color__remove:hover {
    background-color: #dbd1d3;
}
.output {
    background-color: #eee;
    border-radius: 5px;
    border: 1px solid var(--button-color-border);
    font-family: Courier New, monospace;
    height: 200px;
    max-width: 324px;
    padding: 5px;
    width: 324px;
    font-size: 12px;
}
.output:focus {
    box-shadow: 0 0 5px 1px green;
    outline: none;
}
input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}
input[type="color"]::-webkit-color-swatch {
    border-radius: 0;
}
@keyframes animateBg-left {
    0% {
        background-position: 0% 0%;
    }
    100% {
        background-position: 100% 0%;
    }
}
@keyframes animateBg-right {
    0% {
        background-position: 100% 0%;
    }
    100% {
        background-position: 0% 0%;
    }
}
@keyframes animateBg-up {
    0% {
        background-position: 0% 0%;
    }
    100% {
        background-position: 0% 100%;
    }
}
@keyframes animateBg-down {
    0% {
        background-position: 0% 100%;
    }
    100% {
        background-position: 0% 0%;
    }
}
              
            
!

JS

              
                const { useState, useEffect } = React
const rootElement = document.getElementById('root')
const presets = [
  ["#bd4faf","#5794db"],
  ["#0080ff","#388363","#fcf8d6"],
  ["#246655","#462466","#b66d52"],
  ["#41b497","#7f47c5","#c29c46"],
  ["#71c66c","#ff8000","#db5779"],
  ["#cf5c5c","#c19b4a","#def2a0","#c6ee4a","#42eca6","#64b3d9","#208ea2","#498ada","#5b73df","#897ed3"],
  ["#ff0000","#ffff00","#00ff00","#00ffff","#0000ff","#ff00ff"]
]
const currentSet = ["#71c66c","#ff8000","#db5779"]
const isHidden = false

function PresetsSection({parentFunction}) {
  const [items, setItems] = React.useState(presets)
  return (
    <section>
      <span className="label">Presets</span>
      <div className="presets">
        {items.map(item => (
          <Preset 
            key={item}
            colorSeries={item.toString()}
            changePreset={() => parentFunction(item)}
            />
        ))}
      </div>
    </section>
  )
}
function Preset({colorSeries, changePreset}) {
  return (
    <div className="preset" 
      style={{ backgroundImage: `linear-gradient(90deg, ${colorSeries})` }}
      onClick={() => changePreset()}
    ></div>
  )
}
function ColorList({colorSet, removeFunction, changeFunction}) {
  return (
    <ol className="color-list">
      {colorSet.map((color, index) => (
        <ColorItem 
          key={index}
          colorValue={color} 
          removeColor={() => removeFunction(index)} 
          changeColor={() => changeFunction(event, index)} />
      ))}
    </ol>
  )
}
function ColorItem({colorValue, removeColor, changeColor}) {
  const idAttribute = Math.random().toString(36).substr(2, 5)
  return (
    <li className="color">
      <label 
        htmlFor={idAttribute}
        className="color__label">
        {colorValue}</label>
      <input type="color" 
        id={idAttribute}
        className="color__input" 
        value={colorValue} 
        onChange={(event) => changeColor(event)} />
      <button type="button" 
        className="color__remove" 
        aria-label="Remove Color"
        onClick={() => removeColor()}>✖</button>
    </li>
  )
}
function CopyToClipboardButton() {
  const copy = () => {
    const code = document.getElementById('output')
    code.select()
    code.setSelectionRange(0, 99999)
    document.execCommand("copy")
    code.setSelectionRange(0, 0)
  }
  return (
    <button 
      type="button"
      className="button button--copy"
      onClick={copy}>Copy to Clipboard</button>
  )
}
function Output({bg, bgwidth, bgheight, degrees, direction, duration}) {
  let code = getCssCodeForOutput({bg, bgwidth, bgheight, degrees, direction, duration})
  return (
    <section>
      <div style={{display: `flex`, justifyContent: `space-between`, alignItems: `flex-end`, margin: `10px 0`}}>
        <label htmlFor="output"><strong>CSS Output</strong></label>
        <CopyToClipboardButton />
      </div>
      <textarea 
        value={code} 
        readOnly={true} 
        className="output" 
        id="output"/> 
    </section>
  )
}
function getCssKeyframes(direction) {
  switch(direction) {
    case 'left':
      return `@keyframes animateBg {
  0% { background-position: 0% 0%; }
  100% { background-position: 100% 0%; }
}`
    case 'right':
      return `@keyframes animateBg {
  0% { background-position: 100% 0%; }
  100% { background-position: 0% 0%; }
}`
    case 'up':
      return `@keyframes animateBg {
  0% { background-position: 0% 0%; }
  100% { background-position: 0% 100%; }
}`
    case 'down':
      return `@keyframes animateBg {
  0% { background-position: 0% 100%; }
  100% { background-position: 0% 0%; }
}`
  }
}
function getCssCodeForOutput({bg, bgwidth, bgheight, degrees, direction, duration}) {
  const keyframes = getCssKeyframes(direction)
  return (`.animated-gradient {
  animation: animateBg ${duration}s linear infinite;
  background-image: linear-gradient(${degrees}deg,${bg});
  background-size: ${bgwidth}% ${bgheight}%;
}
${keyframes}
`)
}
function useFormInput(initialValue) {
  const [value, setValue] = useState(initialValue)  
  function handleChange(e) {
    setValue(e.target.value)
  }
  return {
    value,
    onChange: handleChange
  }
}
function Generator({title}) {
  const [colorSet, setColorSet] = React.useState(currentSet)
  const [visibility, setVisibility] = React.useState(isHidden)
  const addColor = () => {
    let newColor = '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0')
    setColorSet(colorSet => [...colorSet, newColor])
  }
  const removeColor = (index) => {
    const temp = [...colorSet]
    temp.splice(index, 1)
    setColorSet(temp)
  }
  const changeColor = (event, index) => {
    const temp = [...colorSet]
    temp[index] = event.target.value
    setColorSet(temp)
  }
  const toggleVisibility = (visibility) => {
    setVisibility(!visibility)
  }
  const duration = useFormInput(14)
  const bg = `${colorSet.toString()},${colorSet[0]},${colorSet[1]}`
  const direction = useFormInput('left')
  const bgwidth = (direction.value === 'left' || direction.value === 'right') ? (100 * (colorSet.length + 1)) : 100
  const bgheight = (direction.value === 'left' || direction.value === 'right') ? 100 : (100 * (colorSet.length + 1))
  const degrees = (direction.value === 'left' || direction.value === 'right') ? '90' : '0'

  return (
    <div 
      className="generator"
      role="main"
      style={{ backgroundImage: `linear-gradient(${degrees}deg, ${bg})`, 
             backgroundSize: `${bgwidth}% ${bgheight}%`,
             animationDuration: `${duration.value}s`,
             animationName: `animateBg-${direction.value}`}}>
      <button className={!visibility ? 'button' : 'button button--faded'} onClick={() => {toggleVisibility(visibility)}}>{!visibility ? `Hide` : `Unhide`}</button>
      {!visibility ? 
      <article 
        className="container">
        <h1 
          className="title">Pure CSS Animated Gradient Generator</h1>
        <PresetsSection 
          parentFunction={(item)=>{setColorSet(item)}}/>
        <button 
          type="button"
          className="button button--add"
          onClick={addColor}>Add Color</button>
        <ColorList 
          colorSet={colorSet} 
          removeFunction={(index)=> removeColor(index)}
          changeFunction={(event, index)=> changeColor(event, index)}/>
          
        <section>
          <label htmlFor="duration">
            <span><strong>Duration</strong> (seconds)</span>
            <input id="duration" 
              className="input--textlike duration" 
              type="number" 
              min="1" 
              max="1000" 
              {...duration}/>
          </label>
        </section>
        <section>
          <label htmlFor="direction">
            <strong>Direction</strong>
            <select id="direction"
              className="input--textlike direction"
              {...direction}>
              <option value="left">← Left</option>
              <option value="right">→ Right</option>
              <option value="up">↑ Up</option>
              <option value="down">↓ Down</option>
            </select>
          </label>
        </section>
        <Output 
          bg={bg} 
          bgwidth={bgwidth} 
          bgheight={bgheight} 
          direction={direction.value}
          degrees={degrees} 
          duration={duration.value}/>
        <footer>
          <p>
            <small>Created by <a href="https://granteben.com">Grant Eben</a>.</small><br/>
            <small>Open Source on <a href="https://github.com/gmeben/pure-css-animated-gradient-generator">GitHub</a> and <a href="https://gitlab.com/gmeben/pure-css-animated-gradient-generator">GitLab</a></small>
          </p>
        </footer>
      </article>
      : null }
    </div>
  )
}
const app = <Generator />
ReactDOM.render(app, rootElement)
              
            
!
999px

Console