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.

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

              
                <main>

  <header>
    <h1>Snowflakes: JS generated, CSS animated</h1>
    <p>This version uses ES6 JavaScript to create and embed SVG snowflakes into the HTML.<br>All animations are controlled via CSS.</p>
    <p>Originally written 15th November 2015 for Tesco.com.<br>The snowflake SVG is the copyright of Tesco PLC, please use your own.</p>
    <p>The original <a target=_blank title="[new window]" href="http://websemantics.uk/articles/snow/">Snowflakes article</a> (not updated).<br>
    Updated <a target=_blank title="[new window]" href="https://codepen.io/2kool2/pen/eYmNjvY">CSS only Snowflakes</a>.</p>
  </header>

  <div class=tall-content></div>
  <div class=tall-content></div>

</main>



<!-- Footer codepen include -->
[[[https://codepen.io/2kool2/pen/mKeeGM]]]
              
            
!

CSS

              
                /* Generic styling */

html{font-family: sans-serif; box-sizing: border-box; line-height: 1.5}
*,:after,:before{box-sizing: inherit}
body,button{text-rendering: optimizeLegibility}
body{background-color: #ddd; color: #3a2c49; margin:0}
h1{color: #4B3D5A; font-weight:100; font-size: 2rem; margin: .618rem 0}
a{background: transparent; color: #236ECE; text-decoration: none}
a:hover,a:focus,a:active{color: #014cac}
p a:hover,a:focus,li a:hover,a:focus{text-decoration: underline; outline: 0}

header{padding: 4.236rem}
.tall-content{height: 50vh; margin: 50vh 0}

/* Content bg is semi-transparent to allow snowflakes to show through */
header,
.tall-content{background-color: rgba(35,110,206,.15)}




/* Snow place to be on a cold day in Tesco */

.field {
  color: rgba(255,255,255,.8);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  text-align: center;
  z-index: -1;

  /* Defined by JS - Shift initial position off-screen and encourage hardware acceleration */
/*   transform: translate3d(0, -8rem, 0); */

  /* Flex used to space the svg flakes evenly horizontally. Plays nicely at inbetween widths */
  display:flex;
  justify-content: space-between;

  /* Allow for multi row snowflakes */
  flex-wrap: wrap;
}



    /* Generic styling of snowflakes, default animation and transform */

.field_flake {
  fill: currentColor;
  pointer-events: none; 
  will-change: transform;
  display: inline-block;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  animation: falling;
  animation-iteration-count: infinite;
}
/* Disable if user prefers */
@media (prefers-reduced-motion: reduce) {
  .field_flake {
    animation: none;
  }
}


/* Filter is completely optional, and it may cause performance issues on low powered GPUs. */

.field_flake:nth-of-type(3n+0) {
  filter: drop-shadow(0 1px 1px rgba(0,0,0,.125));
}
.field_flake:nth-of-type(3n+1) {
  filter: blur(1px);
}


/* Animation durations are calculated to be multiples of each other +/- a few seconds for initial delays */

.field_flake:nth-child(3n+0) {
  animation-duration: 12s;
}
.field_flake:nth-child(3n+1) {
  animation-duration: 18s;
}
.field_flake:nth-child(3n+2) {
  animation-duration: 24s;
}


/* Set individual origins */

.field_flake:nth-child(4n+0) {
  transform-origin: -5% -5%;
}
.field_flake:nth-child(4n+1) {
  transform-origin: 15% 0;
}
.field_flake:nth-child(4n+2) {
  transform-origin: 0 -30%;
}
.field_flake:nth-child(4n+3) {
  transform-origin: -100% -100%;
}

/* Keep leftmost snowflake close to the left edge */

.field_flake:first-child {
  transform-origin: 60% 40%;
}


/* Delay the starting times */

.field_flake:nth-of-type(5n+0) {
  animation-delay: 0s;
}
.field_flake:nth-of-type(5n+1) {
  animation-delay: 2s;
}
.field_flake:nth-of-type(5n+2) {
  animation-delay: 4s;
}
.field_flake:nth-of-type(5n+3) {
  animation-delay: 6s;
}
.field_flake:nth-of-type(5n+4) {
  animation-delay: 8s;
}
/* In case of flex wrap, the last flake starts immediately */
.field_flake:last-child {
  animation-delay: 0s;
}


/* Tweak timing functions - a tad more random */

.field_flake:nth-of-type(6n+0) {
  animation-timing-function: ease-in-out;
}
.field_flake:nth-of-type(6n+1) {
  animation-timing-function: ease-out;
}
.field_flake:nth-of-type(6n+2) {
  animation-timing-function: ease;
}
.field_flake:nth-of-type(6n+3) {
  animation-timing-function: ease-in;
}
.field_flake:nth-of-type(6n+4) {
  animation-timing-function: linear;
}
.field_flake:nth-of-type(6n+5) {
  animation-timing-function: cubic-bezier(0.2, 0.3, 0.8, 0.9);
}


/* Tweak opacity - more randomness */

.field_flake:nth-of-type(7n+0) {opacity: 0.6;}
.field_flake:nth-of-type(7n+1) {opacity: 0.9;}
.field_flake:nth-of-type(7n+2) {opacity: 0.5;}
.field_flake:nth-of-type(7n+4) {opacity: 0.8;}
.field_flake:nth-of-type(7n+6) {opacity: 0.7;}



/* Animation for all snowflakes */

@keyframes falling {
  from {
    transform: translate(0, 0) rotate(0deg) scale(0.8);
  }
  to {
    transform: translate(0, 120vh) rotate(360deg) scale(1.2);
  }
}
              
            
!

JS

              
                var supportsES6 = function() {
  // https://gist.github.com/bendc/d7f3dbc83d0f65ca0433caf90378cd95
  try {
    new Function("(a = 0) => a");
    return true;
  }
  catch (err) {
    return false;
  }
}();



// I wanted to delay placing the snowflakes into the HTML until after the page loaded.

var Let_it_snow = (function (window, document) {

  'use strict';
  if (!supportsES6) return false;

  // The different sizes of the flakes (rem).
  // They appear left to right repeated across the screen in this order:
  const flakeSizes = [8, 6, 4, 2];

  // Try varying the number of flakes and their sizes.
  // const flakeSizes = [3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1];

  // Utility array functions
  const _arrSum = arr => arr.reduce((a,b) => a + b, 0);
  const _arrMax = arr => Math.max(...arr);

  const fieldName = 'field';
  const flakeName = 'flake';
  const flakeSetWidth = _arrSum(flakeSizes);


  const _getScreenWidthInRem = _ => {
 
    // Get the root font size (px)
    const rootFontSize = Number(window.getComputedStyle(document.body).getPropertyValue('font-size').match(/\d+/)[0]);

    // Return screen-width in rem
    return parseInt(window.innerWidth / rootFontSize);
  };


  const _getNoOfFlakes = screenWidthRem => {

    const setsOfFlakes = parseInt(screenWidthRem / flakeSetWidth);

    // Get the number of odd flakes required to fill the remaining screen width:
    let swRemainer = parseInt(screenWidthRem % flakeSetWidth);
    let oddFlakes = 0;

    for (const size of flakeSizes) {
      if (swRemainer - size < 0) break;
      oddFlakes++;
      swRemainer -= size;
    }

    // console.log(`flakeSizes(${flakeSizes}) * sets(${setsOfFlakes}) + odds(${oddFlakes}) = total(${setsOfFlakes * flakeSizes.length + oddFlakes})`);

    return (setsOfFlakes * flakeSizes.length) + oddFlakes;
  };



  const _getFlakesHtml = totalFlakes => {

    let html = '';
    let count = 0;

    while (count < totalFlakes) {

      for (const size of flakeSizes) {

        // The snowflake SVG
        html += `<svg class="${fieldName}_${flakeName}" style="width:${size}rem; height:${size}rem; fill-rule:evenodd" viewBox="0 0 96 96"><path d="M38.2 65l1-9.7 6.8-3.8v7.8L38.2 65zM58 65l-8-5.8v-7.8l6.8 4 1 9.6zm9.8-17l-9 3.8-6.6-3.8 7-4 8.6 4zm-39.4 0l8.7-4 7 4-6.5 3.8-8.8-4zM58 31l-1 9.6-7 4v-8l8-5.7zm-19.8 0l7.8 5.5v8l-6.7-3.8-1-9.8zm12-17L60.5 3.5l2.8 3-13.3 13v12L63 22l-1.7 16 10.2-5.7 5-18.3 4 1-4 14.4 12.2-7 2 3.6-12 7L93 37l-1.3 3.8-18.2-5-10.2 5.8L78 48l-14.7 6.4 10 5.6 18-5 1.2 4-14.3 3.8L90.6 70l-2 3.5L76 66.3l4 14.4-4 1-5-18.2-9.8-5.6L63 73.3 50 64v11.6L63.3 89l-2.8 3L50 81.5V96h-4V81.5L35.4 92l-2.8-3L46 75.8V64.4l-12.8 9.3 1.7-16-10.5 6-5 18-4-1 4-14.3-12.5 7L5 70l13-7.2-14.5-4 1-3.8L23 60l10-5.7L18.3 48l14.4-6.4-10.2-6-18.3 5-1-3.8 14.3-4-12.2-7 2-3.4 12.2 7-4-14.4 4-1 5 18.2 10.4 6-2-16 13 9.2V19.8L32.4 6.3l3-2.8L46 14V0h4v14z"/></svg>`;

        count++;
        if (count >= totalFlakes) break;
      }
    }

    return html;
  };


  const _insertSnowField = totalFlakes => {

    const fieldDiv = document.getElementById(fieldName) || document.createElement('div');
    fieldDiv.id = fieldName;
    fieldDiv.className = fieldName;

    // Shift initial position off-screen, by size of largest flake
    fieldDiv.setAttribute('style', `transform: translate3d(0, -${_arrMax(flakeSizes)}rem, 0)`);

    fieldDiv.innerHTML =  _getFlakesHtml(totalFlakes);
    document.body.insertBefore(fieldDiv, document.body.childNodes[0]);

  };


  const letItSnow = _ => {  
    const screenWidthRem = _getScreenWidthInRem();
    const totalFlakes = _getNoOfFlakes(screenWidthRem);
    _insertSnowField(totalFlakes);
  };


  window.addEventListener('load', letItSnow);
  window.addEventListener('resize', letItSnow);

}(window, document));


              
            
!
999px

Console