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

              
                <html>

<head>
  <!-- HTML HEAD CONTENT -->
</head>

<body>
  <header></header>

  <main>
    <button type="button" class="o-btn js-btn--modal" data-modal=".c-modal--version1">Click me</button>
  </main>

  <footer></footer>
  
  <!-- 
    The Modal needs to be right below the body
    and not somewhere inside a component to assure
    the backdrop overlaps all elements
  -->
  <div class="c-modal c-modal--version1" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="c-modal__backdrop js-modal__backdrop"></div>
    <div class="c-modal__dialog" role="document">
      <div class="c-modal__content">
        <button class="c-modal__close o-button js-modal__close"></button>
        <div class="c-modal__body">
          <h3 class="c-modal__headline">Title</h3>
          <div class="c-modal__text">Text</div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
              
            
!

CSS

              
                .c-modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 2000;
  
  padding: 25px 15px;
  outline: 0;
  
  text-align: center;
  white-space: nowrap;
  
  visibility: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  
  transition: visibility .4s ease .4s;
}

.c-modal.is-visible {
  visibility: visible;
  transition: none;
}

.c-modal:after {
  content: "";
  position: relative;
  height: 100%;
  z-index: -1;
  display: inline-block;
  
  white-space: nowrap;
  vertical-align: middle;
  margin-right: -.25em;

  @media only screen and (max-width: 786px){
    vertical-align: top;
  }
}

.c-modal__backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(#000, .6);
  opacity: 0;
  transition: opacity .2s;
}

.c-modal.is-visible .c-modal__backdrop {
  opacity: 1;
}

.c-modal__dialog {
  display: inline-block;
  width: 100%;
  max-width: 450px;
  margin: 0 auto;
  opacity: 0;
  text-align: left;
  vertical-align: middle;
  white-space: normal;
  transform: translateY(10%);
  transition: all .4s;
}

// Add more modifies to c-modal to adjust modal sizes
.c-modal--large .c-modal__dialog {
  max-width: 700px;
}

.c-modal.is-visible .c-modal__dialog {
  opacity: 1;
  transform: translateY(0);
}

.c-modal__content {
  position: relative;
  padding: 35px 50px;
  background: #fff;
}

.c-modal__headline {
  margin-bottom: 21px;
  text-align: center;
  font-size: 1.4375em;

  &:after {
    content: '';
    display: block;
    width: 100%;
    max-width: 330px;
    height: 1px;
    margin: 26px auto 0;
    background: #d8d8d8;
  }
}

.c-modal__text {
  position: relative;
  font-size: .875em;
  margin-bottom: 23px;
}

.c-modal__text > *:last-child {
  margin-bottom: 0;
}

.c-modal__close {
  position: absolute;
  width: 20px;
  height: 20px;
  padding: 9px 0;
  top: 6px;
  right: 6px;
  background: transparent;
  z-index: 2;

  &:before,
  &:after {
    content: '';
    position: absolute;
    left: 2px;
    top: 9px;
    width: 16px;
    height: 2px;
    background: #eee;
    transition: background .4s;
  }

  &:before {
    transform: rotate(45deg);
  }

  &:after {
    transform: rotate(-45deg);
  }

  &:hover,
  &:focus {
    background: none;
    cursor: pointer;

    &:before,
    &:after {
      background: #111;
    }
  }
}

// ===== ADDITIONAL STYLING FOR CODEPEN =====

.o-btn {
  display: block;
  margin: 25px auto;
  background: green;
  color: #fff;
  padding: 10px 20px;
  cursor: pointer;
}
              
            
!

JS

              
                // jQuery should be present
var MODULE = MODULE || {};

(function($, MODULE){

  var $document = $(document); 
  
  MODULE.init_modals = function(){
    $document.on('click', '.js-btn--modal', function(e){
      var target_modal = $(this).attr('data-modal');
      MODULE._OpenCloseModal($(target_modal), true);
    });
    
    MODULE.ModalsLogic();
  };
  
  MODULE.ModalsLogic = function() {
    $document.on('click', '.js-modal__close', function(e) {
      MODULE._OpenCloseModal($(this).closest('.c-modal'), false);
    });

    $document.on('click', '.js-modal__backdrop', function(e) {
      MODULE._OpenCloseModal($(this).closest('.c-modal'), false);
    });

    $document.keyup(function(e) {
      if (e.keyCode == 27) { // esc keycode
        MODULE._OpenCloseModal($('.c-modal'), false);
      }
    });
  }
  
  // Generic function to open/close a given modal
  MODULE._OpenCloseModal = function(modal, show) {
    // Close all other modals
    $('.c-modal').removeClass('is-visible').attr('aria-hidden', 'true');

    modal.toggleClass('is-visible', show);
    modal.attr('aria-hidden', !show);
    // Also en-/disable Body Scrolling dependent on opened/closed modal
    MODULE._ScrollLock(modal, show);
  }
  
  // Uses https://github.com/willmcpo/body-scroll-lock
  MODULE._ScrollLock = function(targetelem, show) {
    if(show) {
      bodyScrollLock.disableBodyScroll(targetelem[0]);
    } else {
      bodyScrollLock.enableBodyScroll(targetelem[0]);
    }
  }
  
  /* READY FUNCTION
  ====================== */
  
  $(function() {
    MODULE.init_modals();
  });
  
})(jQuery, MODULE)
              
            
!
999px

Console