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

              
                %ul.item-list
  %li.item
    %div.item__information
      %div.item__image
        %img(src="http://upload.wikimedia.org/wikipedia/pt/6/67/30_STM_-_Love_Lust_Faith_%2B_Dreams.jpeg")
      %div.item__body
        %h2.item__title Love, Lust, Faith and Dreams
        %p.item__description Love, Lust, Faith and Dreams is the fourth studio album from American rock band Thirty Seconds to Mars.
      %div.item__price.js-item-price(data-price="11.99") $11.99
    %div.item__interactions
      %p.item__quantity
        %a.js-item-increase(title="Add another copy") +
        %a.js-item-decrease.decrease--disabled(title="Remove a copy") -
        %span(data-quantity="1")
          %b 1
          copy
        at $11.99
      %a.item__remove.js-item-remove(title="Remove this item") &times;
  %li.item
    %div.item__information
      %div.item__image
        %img(src="http://upload.wikimedia.org/wikipedia/en/a/a7/Random_Access_Memories.jpg")
      %div.item__body
        %h2.item__title Random Access Memories
        %p.item__description Random Access Memories is the fourth studio album by French electronic music duo Daft Punk.
      %div.item__price.js-item-price(data-price="11.88") $11.88
    %div.item__interactions
      %p.item__quantity
        %a.js-item-increase(title="Add another copy") +
        %a.js-item-decrease.decrease--disabled(title="Remove a copy") -
        %span(data-quantity="1")
          %b 1
          copy
        at $11.88
      %a.item__remove.js-item-remove(title="Remove this item") &times;
  %li.item
    %div.item__information
      %div.item__image
        %img(src="https://s3.amazonaws.com/rapgenius/600px-The_Heist_Macklemore.jpeg")
      %div.item__body
        %h2.item__title The Heist
        %p.item__description The Heist is the debut studio album by American rapper Macklemore and producer Ryan Lewis.
      %div.item__price.js-item-price(data-price="8.99") $8.99
    %div.item__interactions
      %p.item__quantity
        %a.js-item-increase(title="Add another copy") +
        %a.js-item-decrease.decrease--disabled(title="Remove a copy") -
        %span(data-quantity="1")
          %b 1
          copy
        at $8.99
      %a.item__remove.js-item-remove(title="Remove this item") &times;
%div.summary.js-summary
  %ul.steps
    %li
      %b Subtotal:
      %span.sum.js-subtotal $32.86
    %li
      %b Taxes (5%):
      %span.sum.js-taxes $1.64
    %li
      %b Shipping:
      %span.sum.js-shipping $5.00
  %ul.checkout
    %li
      %b Total:
      %span.sum.js-total $39.50
    %li
      %a.button.js-checkout-button Checkout
%div.info
  %div.info__content
    :markdown
      Created by [Sacha Schmid](https://twitter.com/sachaschmid). This is my entry for this week's [CodePen Pattern Rodeo](https://blog.codepen.io/rodeo/season-two/#week-three). If you liked it, please consider voting for me by hearting this Pen on CodePen.
      
      Note: This Pen is best viewed on the [full page](https://codepen.io/sacha/full/DvmCk) or using the vertical editor layout (check the lower right corner in the editor.)
%div.modal-wrapper.js-modal-wrapper
  %div.modal
    %h2.modal__title &hearts;
    %p Seems like we've run out of stock just this moment. Here, take a cute GIF instead.
    %img(src="http://cutestuff.co/wp-content/uploads/2012/12/suprised_kitten.gif")
    %p.modal__info Click anywhere to dismiss.
              
            
!

CSS

              
                @import "compass/css3";

/* ---------- Variables ---------- */

$width: 35em;
$summary-spacing: 1.5em;
$element-shadow: 0 1px 0 rgba(#000, 0.25);

/* ---------- Web Fonts ---------- */

@font-face {
  font-family: 'Source Sans Pro';
  font-style: normal;
  font-weight: 400;
  src: local('Source Sans Pro'), local('SourceSansPro-Regular'), url(http://themes.googleusercontent.com/static/fonts/sourcesanspro/v5/ODelI1aHBYDBqgeIAH2zlNHq-FFgoDNV3GTKpHwuvtI.woff) format('woff');
}

@font-face {
  font-family: 'Source Sans Pro';
  font-style: normal;
  font-weight: 700;
  src: local('Source Sans Pro Bold'), local('SourceSansPro-Bold'), url(http://themes.googleusercontent.com/static/fonts/sourcesanspro/v5/toadOcfmlt9b38dHJxOBGIqjGYJUyOXcBwUQbRaNH6c.woff) format('woff');
}

/* ---------- Mixins ---------- */

@mixin keyframes ($name) {
  @-webkit-keyframes $name { @content; }
  @-moz-keyframes $name { @content; }
  @-ms-keyframes $name { @content; }
  @keyframes $name { @content; }
}

@mixin animation ($value) {
  -webkit-animation: $value;
  -moz-animation: $value;
  -ms-animation: $value;
  animation: $value;
}

@mixin breakpoint ($breakpoint) {
  @if $breakpoint == medium {
    @media (min-width: 40em) { @content; }
  } @elseif $breakpoint == wide {
    @media (min-width: 50em) { @content; }
  }
}

/* ---------- Placeholder selectors ---------- */

%clearfix::after {
  content: "";
  display: table;
  clear: both;
}

/* ---------- Universal selectors ---------- */

* {
  @include box-sizing(border-box);
}

/* ---------- Elements ---------- */

body {
  margin: 3em 0;
  font: 100%/1.5 Source Sans Pro, sans-serif;
  background-color: #f0f0f0;
  overflow-y: scroll;
  overflow-x: hidden;
  
  @include breakpoint(wide) {
    font-size: 112.5%;
  }
}

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* ---------- Shared ---------- */

.item,
.modal,
.steps,
.checkout {
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 1px 3px rgba(#000, 0.25);
}

.item__interactions a,
.button {
  @include transition(background-color 0.25s ease-in-out);
  text-align: center;
  border-radius: 3px;
  cursor: pointer;
}

.empty-hint,
.info__content {
  a {
    @include transition-property(color, border-bottom-color);
    @include transition-duration(0.25s);
    @include transition-timing-function(ease-in-out);
    color: #e66b1e;
    border-bottom: 1px solid transparent;
    text-decoration: none;
    cursor: pointer;
    
    &:focus,
    &:hover {
      color: lighten(#e66b1e, 5%);
      border-bottom-color: lighten(#e66b1e, 5%);
    }
      
    &:active {
      position: relative;
      top: 1px;
    }
  }
}

/* ---------- Items ---------- */

.item-list {
  @extend %clearfix;
  width: 80%;
  margin: 0 auto 1.5em;
  
  &.delayed {
    @include transition(max-height 0.4s ease-in-out);
  }
  
  @include breakpoint(medium) {
    width: $width;
  }
}

.item {
  @include transition-property(transform, opacity, margin-top);
  @include transition-duration(0.25s);
  @include transition-timing-function(ease-in-out);
  @include transition-delay(0, 0, 0.25s);
  @include transform(translate3d(0, 0, 0));
  width: 100%;
  margin-bottom: 1.5em;
  
  .item-list.appearing & {
    @include transform(translateX($width));
    opacity: 0;
  }
  
  .item-list.delayed & {
    &:nth-child(1) { @include transition-delay(0.25s); }
    &:nth-child(2) { @include transition-delay(0.3s); }
    &:nth-child(3) { @include transition-delay(0.35s); }
  }
}

.item--disappearing {
  @include transform(translateX($width));
  opacity: 0;
}

.empty-hint {
  @include transition-property(max-height, opacity);
  @include transition-duration(0.5s);
  @include transition-timing-function(ease-in-out);
  @include transition-delay(0);
  @include transform(translate3d(0, 0, 0));
  max-height: 0;
  text-align: center;
  opacity: 0;
  overflow: hidden;
  -webkit-backface-visibility: hidden;
  
  &.is-visible {
    max-height: 20em;
    opacity: 1;
  }
  
  p {
    margin: 1.5em;
    
    @include breakpoint(medium) {
      padding: 1em;
    }
  }
  
  a {
    font-weight: bold;
  }
}

.item__information {
  @include breakpoint(medium) {
    display: table;
  }
}

.item__image {
  padding: 1.5em;
  border-bottom: 1px solid rgba(#ddd, 0.35);
  vertical-align: middle;
  
  img {
    display: block;
    width: 60%;
    height: auto;
    margin: 0 auto;
    border-radius: 5px;
    box-shadow: $element-shadow;
    -webkit-backface-visibility: hidden;
  
    &:hover,
    &:focus {
      @include animation(tilt 0.25s ease-in-out);
    }
  }
  
  @include breakpoint(medium) {
    display: table-cell;
    width: 7em;
    padding: 0;
    border-right: 1px solid rgba(#ddd, 0.35);
    border-bottom: 0;
  }
}

.item__body {
  padding: 1.5em 1.5em 0.5em;
  
  @include breakpoint(medium) {
    display: table-cell;
    width: 23em;
    padding: 0.75em 1em;
    border-right: 1px solid rgba(#ddd, 0.35);
  }
}

.item__title {
  margin-top: 0;
  margin-bottom: 0.5rem;
  font-size: 1em;
}

.item__description {
  margin-top: 0;
  margin-bottom: 0;
  font-size: 0.9em;
}

.item__price {
  padding: 0 1.5em 1.5em;
  color: #eb894c;
  font-weight: bold;
  border-radius: 0 3px 3px 0;
  vertical-align: middle;
  
  @include breakpoint(medium) {
    display: table-cell;
    width: 5em;
    padding: 0;
    text-align: center;
    background-color: #fcfcfc;
  }
}

.item__interactions {
  @extend %clearfix;
  display: block;
  width: 100%;
  padding: 1.5em;
  color: #666;
  font-size: 0.8em;
  border-top: 1px solid rgba(#ddd, 0.35);
  
  span {
    margin-left: 0.5em;
  }

  a {
    @include background-image(linear-gradient(top, rgba(#fff, 0.6), rgba(#fff, 0)));
    @include transition-property(opacity, background-color);
    @include transform(translate3d(0, 0, 0));
    display: inline-block;
    width: 3em;
    padding: 0.5em;
    background-color: #eaeaea;
    
    &.decrease--disabled {
      opacity: 0.5;
      cursor: default;
    }
    
    &:not([class*="decrease--disabled"]):focus,
    &:not([class*="decrease--disabled"]):hover {
      background-color: lighten(#eaeaea, 2%);
    }
      
    &:not([class*="decrease--disabled"]):active {
      background-color: darken(#eaeaea, 2%);
    }
  }
  
  @include breakpoint(medium) {
    padding: 1em;
  }
}

.item__quantity {
  margin-top: 0;
  margin-bottom: 0;
  float: left;
}

.item__remove {
  float: right;
}

/* ---------- Summary ---------- */

.summary {
  @extend %clearfix;
  width: 80%;
  margin: 0 auto;
  
  @include breakpoint(medium) {
    width: $width;
  }
}

.steps,
.checkout {
  width: 100%;
  
  li {
    padding: 1em;
    border-bottom: 1px solid rgba(#ddd, 0.35);
    
    &:last-child {
      border-bottom: 0;
    }
  }
  
  @include breakpoint(medium) {
    float: left;
    width: ($width - $summary-spacing) / 2;
  }
}

.steps {
  margin-bottom: $summary-spacing;
  
  @include breakpoint(medium) {
    margin-right: $summary-spacing;
    margin-bottom: 0;
  }
}

.sum {
  float: right;
}

.button {
  @include background-image(linear-gradient(top, rgba(#fff, 0.2), rgba(#fff, 0)));
  display: block;
  padding: 1em;
  color: #fff;
  font-weight: bold;
  text-shadow: 0 -1px 0 rgba(#000, 0.35);
  background-color: #e66b1e;
  box-shadow: $element-shadow;
  
  &:hover,
  &:focus {
    background-color: lighten(#e66b1e, 5%);
  }
    
  &:active {
    background-color: darken(#e66b1e, 5%);
  }
}

/* ---------- Info ---------- */

.info {
  padding-top: 3em;
  margin-top: 3em;
  border-top: 1px solid rgba(#ddd, 0.35);
}

.info__content {
  width: 80%;
  margin: 0 auto;
  text-align: center;
  
  p {
    margin-top: 0;
    margin-bottom: 1.5em;
    
    &:last-child {
      margin-bottom: 0;
      color: #666;
    }
  }
  
  @include breakpoint(medium) {
    width: $width;
  }
}

/* ---------- Modal ---------- */

.modal-wrapper {
  @include transition(visibility 0s linear 0.25s, opacity 0.25s ease-in-out);
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  text-align: center;
  background-color: rgba(#111, 0.7);
  opacity: 0;
  visibility: hidden;
  cursor: pointer;
  
  &.is-visible {
    visibility: visible;
    opacity: 1;
    @include transition-delay(0s);
  }
  
  &::before {
    content: "";
    display: inline-block;
    height: 100%;
    vertical-align: middle;
  }
}

.modal {
  @include transform(translateY(5em) scale(0.95));
  @include transition(transform 0.25s ease-in-out);
  display: inline-block;
  width: 70%;
  padding: 1.5em;
  vertical-align: middle;
  
  .modal-wrapper.is-visible & {
    @include transform(translateY(0) scale(1));
  }
  
  p {
    margin: 0;
  }
  
  img {
    max-width: 100%;
    margin: 1.5em 0;
  }
  
  @include breakpoint(medium) {
    width: 25em;
  }
}

.modal__title {
  margin-top: 0;
  margin-bottom: 0;
  color: darken(red, 10%);
  line-height: 2;
}

.modal__info {
  color: #666;
  font-size: 0.8em;
}

/* ---------- Keyframes ---------- */

@include keyframes(tilt) {
  50% { @include transform(rotate(-5deg)); }
}
              
            
!

JS

              
                var subtotal = document.querySelector('.js-subtotal'),
    itemList = document.querySelector('.item-list'),
    priceFields = document.querySelectorAll('.item .js-item-price'),
    taxes = document.querySelector('.js-taxes'),
    shipping = document.querySelector('.js-shipping'),
    total = document.querySelector('.js-total'),
    checkoutButton = document.querySelector('.js-checkout-button'),
    modalWrapper = document.querySelector('.js-modal-wrapper'),
    initialList = itemList.innerHTML

function loop (which, callback) {
  var len = which.length
      
  while (len--) {
    callback(which[len], len)
  }
}

function handleCalculations () {
  var subTotalPrice = 0,
      taxesPrice = 0
      
  loop(priceFields, function (price) {
    subTotalPrice += +price.textContent.substr(1)
  })
      
  subTotalPrice = subTotalPrice.toFixed(2)
      
  taxesPrice = (subTotalPrice * 0.05).toFixed(2)
      
  subtotal.textContent = '$' + subTotalPrice
  taxes.textContent = '$' + taxesPrice
  shipping.textContent = subTotalPrice !== '0.00' ? '$5.00' : 'Free'
  
  total.textContent = '$' + ((+subTotalPrice) + (+taxesPrice) + (+subTotalPrice > 0 ? 5 : 0)).toFixed(2)
}

function changeQuantity (emitter, action) {
  var action = emitter.classList.contains('js-item-increase') ? 'increase' : 'decrease',
      quantityField = emitter.parentElement.querySelector('span'),
      quantity = +quantityField.getAttribute('data-quantity'),
      price
  
  if (action === 'increase') {
    emitter.nextElementSibling.classList.remove('decrease--disabled')
  } else if (action === 'decrease') {
    if (quantity === 2) {
      emitter.classList.add('decrease--disabled')
    } else if (quantity === 1) {
      return
    }
  }
  
  quantityField.innerHTML = '<b>' + (action === 'increase' ? ++quantity : --quantity) + '</b> ' + (quantity > 1 ? 'copies' : 'copy')
  quantityField.setAttribute('data-quantity', quantity)

  price = emitter.parentElement.parentElement.parentElement.querySelector('.js-item-price')

  price.textContent = '$' + (quantity * price.getAttribute('data-price')).toFixed(2)
      
  handleCalculations()
}

function removeItem (emitter) {
  var item = emitter.parentElement.parentElement,
      len = priceFields.length,
      marginBottom = len > 1 ? parseInt(getComputedStyle(item).marginBottom, 10) : 0
      
  item.classList.add('item--disappearing')
  item.style.marginTop = -(item.offsetHeight + marginBottom) + 'px'
  
  setTimeout(function () {
    itemList.removeChild(item)
    
    priceFields = document.querySelectorAll('.item .js-item-price')
      
    if (!priceFields.length) {
      itemList.innerHTML = '<li class="item empty-hint"><p>Bummer, you removed all items! Wanna <a class="js-restore-list">start over again</a>?</li>'
      itemList.firstElementChild.classList.add('is-visible')
    }
      
    handleCalculations()
  }, 500)
}

function restoreList () {
  itemList.firstElementChild.classList.remove('is-visible')
    
  setTimeout(function () {
    itemList.style.minHeight = itemList.offsetHeight + 'px'
    itemList.classList.add('appearing', 'delayed')
    itemList.innerHTML = initialList
    itemList.style.maxHeight = itemList.offsetHeight + 'px'
    itemList.classList.remove('appearing')
    priceFields = document.querySelectorAll('.item .js-item-price')
    handleCalculations()
  }, 500)
    
  setTimeout(function () {
    itemList.style.minHeight = 0
    itemList.style.maxHeight = 'none'
    itemList.classList.remove('delayed')
  }, 1500)
}

itemList.addEventListener('click', function (e) {
  var target = e.target,
      classList = target.classList
  
  if (classList.contains('js-item-increase') || classList.contains('js-item-decrease')) {
    changeQuantity(target)
  } else if (classList.contains('js-item-remove')) {
    removeItem(target)
  } else if (classList.contains('js-restore-list')) {
    restoreList()
  }
})

checkoutButton.addEventListener('click', function () {
  modalWrapper.classList.add('is-visible')
})

modalWrapper.addEventListener('click', function () {
  modalWrapper.classList.remove('is-visible')
})

setTimeout(function () {
  modalWrapper.style.display = 'block'
}, 250)
              
            
!
999px

Console