Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <body id="gradient">
  <h1>Background Generator</h1>

  <h2>Pick Colors</h2>
    <input class="button color1" type="color" name="color1" value="#ffffff">
    <input class="button color2" type="color" name="color2" value="#0086a0">
    
  <h2>Current CSS Background</h2>
  <div class="group1">
     <div class="colorValue" type="text" id="colorValue"></div>
  </div>
  <div class="group1">
     <button class="btn-copy" type="button">Copy</button>
  </div>

  <h2>Random CSS Background</h2>
    <button class="button" type="button" id="button1">Color1</button>
    <button class="button" type="button" id="button2">Color2</button>

  <script type="text/javascript" src="script.js"></script>
</body>
              
            
!

CSS

              
                body {
  font: 'Raleway', sans-serif;
  color: rgba(0,0,0,.5);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .5em;
  top: 15%;
}

h1 {
  font: 600 3.5em 'Raleway', sans-serif;
  color: rgba(0,0,0,.5);
  text-align: center;
  text-transform: .5em;
  width: 100%;
}

h3 {
  font: 900 1em 'Raleway', sans-serif;
  color: rgba(0,0,0,.5);
  text-align: center;
  text-transform: none;
  letter-spacing: 0.01em;
}

.button {
  width: 100px;
  height: 40px;
  border: 2px solid light-grey;
  color: black;
}

.colorValue {
  font: 0.8em 'Arial', sans-serif;
  color: rgba(0,0,0,.5);
  text-align: center;
  letter-spacing: 0.01em;
  border-radius: 10px;
  border: 1px solid grey;
  padding: 5px;
  background-color: white;
}

.btn-copy {
  border-radius: 10px;
  border: none;
}

.color1, .color2 {
  width: 80px;
  height: 30px;
}

.group1 {
  height: 30px;
  padding: 5px;
  vertical-align: middle;
  display: inline-flex;
  justify-content: center;
}
              
            
!

JS

              
                var css = document.querySelector (".colorValue");
var color1 = document.querySelector (".color1");
var color2 = document.querySelector (".color2");
var body = document.getElementById ("gradient");

setGradient ();

function setGradient () {
  body.style.background = "linear-gradient(to right, "
                        + color1.value + ", "
                        + color2.value + ")";

  css.textContent = body.style.background + ";";
}

color1.addEventListener ("input", setGradient);
color2.addEventListener ("input", setGradient);

function randomColor () {
  var x = Math.round (0xffffff * Math.random()).toString(16);
  var y = (6 - x.length);
  var z = "000000";
  z = z.substring (0, y);
  var rColor = "#" + z + x;
  
  return rColor;
}

function setRandomGradient (element1, element2) {
  element1.value = randomColor();
  element2.style.background = element1.value;
  setGradient ();
}

var button1 = document.getElementById ("button1");
var button2 = document.getElementById ("button2");

button1.addEventListener ("click", function () {
  setRandomGradient (color1, button1);
})
button2.addEventListener ("click", function () {
  setRandomGradient (color2, button2);
})

function selectElementText (element) {
  var range = document.createRange ();
  range.selectNodeContents (element);
  var selection = window.getSelection ();
  selection.removeAllRanges ();
  selection.addRange (range);
}

var btnCopy = document.querySelector (".btn-copy");

btnCopy.addEventListener ("click", function () {
  selectElementText (css);
  document.execCommand ("copy");
});
              
            
!
999px

Console