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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

            
              <div class="main-title">
  <h1>CSS Spinner</h1>
  <h3>Customizer</h3>
</div>

<pre><code class="css" id="spinner-css"></code></pre>

<div class="spinner-container">
  <div class="spinner"></div>
</div>

<div class="sorry">
  There is obviously so much to improve and add. If you have any ideas, don't be afraid to fork this project and have at it!
</div>
            
          
!

CSS

            
              * {
  margin: 0;
  padding: 0;
}

body {
  font-family: sans-serif;
  background: #111;
  color: white;
}

:root {
  --spinner-background-color: #00000000;
  --spinner-background-color: auto;
  --spinner-border-radius: 50%;
  --spinner-box-shadow-right: 1px;
  --spinner-box-shadow-bottom: 1px;
  --spinner-box-shadow-blur: 0px;
  --spinner-box-shadow-spread: 0px;
  --spinner-box-shadow-color: green;
  --spinner-box-shadow-inset: ;
  --spinner-width: 50px;
  --spinner-height: 50px;
  --spinner-speed: 1s;
  --spinner-animation-timing-function: linear;
  --spinner-animation: spin;
}

.main-title {
  margin: 20px;
  h3 {
    font-family: monospace;
    font-size: 1.5em;
  }
}

#spinner-css {
  max-width: 400px;
  border-radius: 4px;
  margin: 0 auto;
  padding: 0 20px;
}

.spinner-container {
  margin-top: 100px;
  display: flex;
  justify-content: center;
  .spinner {
    border-radius: var(--spinner-border-radius);
    background-color: var(--spinner-background-color);
    box-shadow:
      var(--spinner-box-shadow-inset)
      var(--spinner-box-shadow-right)
      var(--spinner-box-shadow-bottom)
      var(--spinner-box-shadow-blur)
      var(--spinner-box-shadow-spread)
      var(--spinner-box-shadow-color);
    width: var(--spinner-width);
    height: var(--spinner-height);
    animation: var(--spinner-animation) var(--spinner-speed);
    animation-iteration-count: infinite;
    animation-timing-function: var(--spinner-animation-timing-function);
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes grow {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(.7);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes spin-and-grow {
  0% {
    transform: scale(1) rotate(0deg);
  }
  50% {
    transform: scale(.7) rotate(180deg);
  }
  100% {
    transform: scale(1) rotate(360deg);
  }
}

.sorry {
  position: fixed;
  bottom: 10px;
  padding: 30px;
  font-family: monospace;
  color: #f25;
}
            
          
!

JS

            
              //make css look pretty
hljs.initHighlightingOnLoad();

const inputs = {};
inputs["background-color"] = "#000000";
inputs["background-color-opacity"] = 0
inputs["border-radius"] = "50%";
inputs["box-shadow-color"] = "#203a57"
inputs["box-shadow-right"] = 1;
inputs["box-shadow-bottom"] = 1;
inputs["box-shadow-blur"] = 0;
inputs["box-shadow-spread"] = 0;
inputs["box-shadow-inset"] = false;
inputs["height"] = 50;
inputs["width"] = 50;
inputs["speed"] = 1;
inputs["animation-timing-function"] = "linear"
inputs["animation"] = "spin"

const gui = new dat.GUI();
bsFolder = gui.addFolder('Box Shadow');

document.documentElement.style.setProperty(
  "--spinner-background-color",
  "#00000000"
);
displayCSSVals();

for (const input in inputs) {  
  const listener = addValToGUI(input)
  
  listener.onChange(function(val) {
    setCSS(input, val);
    displayCSSVals(input);
    hljs.highlightBlock(document.getElementById('spinner-css'));
  }.bind(this))
}

function displayCSSVals(valName) {  
  let msg = ``;
  const exclusions = ['box-shadow', 'speed', 'animation', 'background-color']
  for (const input in inputs) {
    if (exclusions.some(cssName => input.includes(cssName))) continue;
    let suffix = '';
    if (typeof inputs[input] === 'number') suffix = 'px';
    
    if (inputs[input]) msg += `\n    ${input}: ${inputs[input] + suffix};`;
  }
  msg += `\n    background-color: ${document.documentElement.style.getPropertyValue("--spinner-background-color")};`
  //box shadow is split into multiple values, so display here
  msg += `\n    box-shadow: ${inputs['box-shadow-inset'] ? 'inset ' : ''}${inputs['box-shadow-right']}px ${inputs['box-shadow-bottom']}px ${inputs['box-shadow-blur']}px ${inputs['box-shadow-spread']}px ${inputs['box-shadow-color']};`;
  msg += `\n    animation: ${inputs['animation']} ${inputs['speed']}s;`;
  msg += `\n    animation-iteration-count: infinite;`;
  msg += `\n    animation-timing-function: ${inputs['animation-timing-function']};`;

  document.getElementById("spinner-css").textContent =
    "\n.spinner {" + msg + "\n}\n\n" + cssAnim(inputs['animation']) + '\n\n';
}

function addValToGUI(name) {
  const isBoxShadow = name.includes('box-shadow');
  const isSpeed = name.includes('speed');
  const isAnimation = name.includes('animation');
  const isOpacity = name.includes('opacity')
  const valIsNum = typeof inputs[name] === 'number';
  const valIsColor = name.includes('color');
  
  if (isOpacity) return gui.add(inputs, name, 0, 1, .01);
  
  if (isSpeed) return gui.add(inputs, name, 0, 5, .01);
  if (isAnimation) {
    if (name.includes('function')) return gui.add(inputs, name, ['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out', 'steps(5)', 'steps(10)'])
    return  gui.add(inputs, name, ['spin', 'grow', 'spin-and-grow'])
  } 
  
  if (!isBoxShadow) {
    if (valIsNum) return gui.add(inputs, name, 0, 100, 1);
    if (valIsColor) return gui.addColor(inputs, name);
    return gui.add(inputs, name);
  }
  if (valIsNum) return bsFolder.add(inputs, name, 0, 100, 1);
  if (valIsColor) return bsFolder.addColor(inputs, name);
  return bsFolder.add(inputs, name);
}

function setCSS(name, val) {
  const rootStyle = document.documentElement.style;
  let suffix = 'px'
  
  //dat.GUI doesn't support opacity, so I have to use this mess instead
  if (name.includes('background-color')) {
    const currentColor = rootStyle.getPropertyValue("--spinner-background-color");

    if (!name.includes('opacity')) {
      rootStyle.setProperty(
        "--spinner-background-color",
        val + currentColor.slice(7)
      );
      return;
    }
    
    let hexOpac = parseInt(val * 255).toString(16);
    if (hexOpac.length == 1) hexOpac = '0' + hexOpac;
    
    rootStyle.setProperty(
      "--spinner-background-color", 
      currentColor.substring(0, currentColor.length - 2) + hexOpac
    );
    return;
  }
  
  if (name.includes('speed')) suffix = 's';
  if (name.includes('color') || name.includes('animation') || name.includes('radius')) suffix = '';
  if (name.includes('inset')) return rootStyle.setProperty("--spinner-" + name, val ? 'inset' : '');
  
  rootStyle.setProperty("--spinner-" + name, val + suffix);
}

function cssAnim(name) {
  if (name === 'spin') {
    return  `@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}`;
  } else if (name === 'grow') {
    return `@keyframes grow {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(.7);
  }
  100% {
    transform: scale(1);
  }
}`
  }
  return `@keyframes spin-and-grow {
  0% {
    transform: scale(1) rotate(0deg);
  }
  50% {
    transform: scale(.7) rotate(180deg);
  }
  100% {
    transform: scale(1) rotate(360deg);
  }
}`
}
            
          
!
999px

Console