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

              
                <!-- This is a custom dropdown I made to replace native select elements, as part of my own mini front-end framework.

I used other third party plugins before (and tried many others) but after having a hard time customizing them I decided to make my own.

The special thing here, if you didn’t notice, is that you can specify a display text for each option. This will be displayed on the dropdown when the option is selected. For example, here the "None" option has a display text of "Fruits" (which could be "Select fruits”, to be more clear).

You can also add classes to the select element which will be added to the custom markup. This way you can customize different versions with CSS. In the CSS I included a "wide" class (that makes the dropdown and list adapt to the size of the container). I have other classes that vary the size and color, for example.

BTW, it's not really a plugin right now—just a JS file with a method and a few event listeners—but I might wrap it up as an open source plugin if anyone is interested. -->

<div class="container">
  <select class="">
    <option value="" data-display-text="Fruits">None</option>
    <option value="apples">Apples</option>
    <option value="bananas">Bananas</option>
    <option value="oranges">Oranges</option>
  </select>
</div>

<div class="by">Made by <a target="_blank" href="http://hernansartorio.com/">Hernán Sartorio</a></div>

              
            
!

CSS

              
                // Variables
$input_height: 42px;
$border_radius: 6px;
$gray_light: #eee;
$gray_lighter: #f6f6f6;

// Mixins
@mixin gradient($top_color, $bottom_color) {
  background-image: -webkit-linear-gradient(top, $top_color 0%, $bottom_color 100%);
  background-image: linear-gradient(to bottom, $top_color 0%, $bottom_color 100%);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{ie-hex-str($top_color)}', endColorstr='#{ie-hex-str($bottom_color)}', GradientType=0); // IE9 and down
}

// Hide native dropdown
select {
  display: none;
}

// Styles
.dropdown { 
  @include gradient(rgba(255,255,255,.25), rgba(255,255,255,0));
  background-color: $gray_lighter;
  border-radius: $border_radius;
  border: solid 1px $gray_light; 
  box-shadow: 0 1px 0 rgba(0,0,0,0.0075);
  box-sizing: border-box;
  cursor: pointer;
  display: block;
  float: left;
  font-size: 14px;
  font-weight: normal;
  height: $input_height;
  line-height: $input_height - 2;
  outline: none;
  padding-left: 18px;
  padding-right: 30px;
  position: relative;
  text-align: left !important;
  transition: all 0.2s ease-in-out;
  user-select: none;
  white-space: nowrap;
  width: auto;
  &:focus {
    background-color: darken($gray_lighter, 2%);
  }
  &:hover {
    background-color: darken($gray_lighter, 1%);
  }
  &:active {
    background-color: darken($gray_lighter, 2.5%) !important;
    border-color: darken($gray_light, 4%);
    box-shadow: 0 1px 4px rgba(0,0,0,.05) inset;
  }
  // Arrow
  &:after { 
    height: 0;
    width: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 4px solid #777;
    transform: origin(50% 20%);
    transition: all 0.125s ease-in-out;
    content: '';
    display: block; 
    margin-top: -2px;
    pointer-events: none;
    position: absolute; 
    right: 10px; top: 50%; 
  }
  &.open {
    @extend :active;
    &:after {
      transform: rotate(-180deg);
    }
    .list {
      transform: scale(1);
      opacity: 1;
      pointer-events: auto;
    }
    .option {
      cursor: pointer;
    }
  }
  &.wide {
    width: 100%;
    .list {
      left: 0 !important;
      right: 0 !important;
    }
  }
  .list {
    box-sizing: border-box;
    transition: all .15s cubic-bezier(0.25, 0, 0.25, 1.75), opacity .1s linear;
    transform: scale(.75);
    transform-origin: 50% 0;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, .09);
    background-color: #fff;
    border-radius: $border_radius;
    margin-top: 4px;
    padding: 3px 0;
    opacity: 0;
    overflow: hidden;
    pointer-events: none;
    position: absolute;
    top: 100%; left: 0;
    z-index: 999;
    &:hover .option:not(:hover) {
      background-color: transparent !important;
    }
  }
  .option {
    cursor: default;
    font-weight: 400;
    line-height: $input_height - 2;
    outline: none;
    padding-left: 18px;
    padding-right: 29px;
    text-align: left;
    transition: all 0.2s;
    &:hover, &:focus { 
      background-color: $gray_lighter !important;
    }
    &.selected { 
      font-weight: 600;
    }
    &.selected:focus { 
      background: $gray_lighter;
    }
  }
}


// Page styles
body {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  color: #666; 
  font-family: 'Montserrat', sans-serif;
}

.container {
  margin: 40px auto 0;
  max-width: 120px;
}

.by {
  bottom: 12px;
  color: #aaa;
  font-size: 12px;
  left: 0;
  position: absolute;
  right: 0;
  text-align: center;
}

a {
  color: #aaa;
  text-decoration: none;
  transition: all 0.2s ease-in-out;
  &:hover {
    color: #666;
  }
}
              
            
!

JS

              
                function create_custom_dropdowns() {
  $('select').each(function(i, select) {
    if (!$(this).next().hasClass('dropdown')) {
      $(this).after('<div class="dropdown ' + ($(this).attr('class') || '') + '" tabindex="0"><span class="current"></span><div class="list"><ul></ul></div></div>');
      var dropdown = $(this).next();
      var options = $(select).find('option');
      var selected = $(this).find('option:selected');
      dropdown.find('.current').html(selected.data('display-text') || selected.text());
      options.each(function(j, o) {
        var display = $(o).data('display-text') || '';
        dropdown.find('ul').append('<li class="option ' + ($(o).is(':selected') ? 'selected' : '') + '" data-value="' + $(o).val() + '" data-display-text="' + display + '">' + $(o).text() + '</li>');
      });
    }
  });
}

// Event listeners

// Open/close
$(document).on('click', '.dropdown', function(event) {
  $('.dropdown').not($(this)).removeClass('open');
  $(this).toggleClass('open');
  if ($(this).hasClass('open')) {
    $(this).find('.option').attr('tabindex', 0);
    $(this).find('.selected').focus();
  } else {
    $(this).find('.option').removeAttr('tabindex');
    $(this).focus();
  }
});
// Close when clicking outside
$(document).on('click', function(event) {
  if ($(event.target).closest('.dropdown').length === 0) {
    $('.dropdown').removeClass('open');
    $('.dropdown .option').removeAttr('tabindex');
  }
  event.stopPropagation();
});
// Option click
$(document).on('click', '.dropdown .option', function(event) {
  $(this).closest('.list').find('.selected').removeClass('selected');
  $(this).addClass('selected');
  var text = $(this).data('display-text') || $(this).text();
  $(this).closest('.dropdown').find('.current').text(text);
  $(this).closest('.dropdown').prev('select').val($(this).data('value')).trigger('change');
});

// Keyboard events
$(document).on('keydown', '.dropdown', function(event) {
  var focused_option = $($(this).find('.list .option:focus')[0] || $(this).find('.list .option.selected')[0]);
  // Space or Enter
  if (event.keyCode == 32 || event.keyCode == 13) {
    if ($(this).hasClass('open')) {
      focused_option.trigger('click');
    } else {
      $(this).trigger('click');
    }
    return false;
    // Down
  } else if (event.keyCode == 40) {
    if (!$(this).hasClass('open')) {
      $(this).trigger('click');
    } else {
      focused_option.next().focus();
    }
    return false;
    // Up
  } else if (event.keyCode == 38) {
    if (!$(this).hasClass('open')) {
      $(this).trigger('click');
    } else {
      var focused_option = $($(this).find('.list .option:focus')[0] || $(this).find('.list .option.selected')[0]);
      focused_option.prev().focus();
    }
    return false;
  // Esc
  } else if (event.keyCode == 27) {
    if ($(this).hasClass('open')) {
      $(this).trigger('click');
    }
    return false;
  }
});

$(document).ready(function() {
  create_custom_dropdowns();
});
              
            
!
999px

Console