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

              
                <section search="container">
    <h1>Search and highlight</h1>

    <form class="input__container">
      <input type="text" search="input" placeholder="Search Miguel or Charles">
      <button type="reset" title="Clear input" search="clear" hidden>
        <svg viewBox="0 0 20 20">
          <path
            d="M11.4142136,10 L14.2426407,7.17157288 L12.8284271,5.75735931 L10,8.58578644 L7.17157288,5.75735931 L5.75735931,7.17157288 L8.58578644,10 L5.75735931,12.8284271 L7.17157288,14.2426407 L10,11.4142136 L12.8284271,14.2426407 L14.2426407,12.8284271 L11.4142136,10 L11.4142136,10 Z M2.92893219,17.0710678 C6.83417511,20.9763107 13.1658249,20.9763107 17.0710678,17.0710678 C20.9763107,13.1658249 20.9763107,6.83417511 17.0710678,2.92893219 C13.1658249,-0.976310729 6.83417511,-0.976310729 2.92893219,2.92893219 C-0.976310729,6.83417511 -0.976310729,13.1658249 2.92893219,17.0710678 L2.92893219,17.0710678 Z M4.34314575,15.6568542 C7.46734008,18.7810486 12.5326599,18.7810486 15.6568542,15.6568542 C18.7810486,12.5326599 18.7810486,7.46734008 15.6568542,4.34314575 C12.5326599,1.21895142 7.46734008,1.21895142 4.34314575,4.34314575 C1.21895142,7.46734008 1.21895142,12.5326599 4.34314575,15.6568542 L4.34314575,15.6568542 Z"
            stroke="none" stroke-width="1" fill="#38383a" fill-rule="evenodd"></path>
        </svg>
      </button>
      <div search="counter"></div>
    </form>

    <details search="item">
      <summary>Don Quixote</summary>
      
      The Ingenious Gentleman Don Quixote of La Mancha, or just Don Quixote, is a Spanish novel by Miguel de Cervantes.
    </details>

    <details search="item">
      <summary>A Tale of Two Cities</summary>
      
      A Tale of Two Cities is an 1859 historical novel by Charles Dickens, set in London and Paris before and during the French Revolution.
    </details>

    <div search="noResults" hidden>No results</div>

  </section>
              
            
!

CSS

              
                $accent: #dfa041
$darkGray: #38383a
$lightGray: #ebeef3
$white: #f8f8f8
$boxShadow: 0px 16px 64px -24px rgba(0,0,0,0.45)
$borderRadius: 32px
$animation: .4s cubic-bezier(.5, 0, .2, 1)

html 
  font-size: 18px
  background-color: $lightGray
  box-sizing: border-box
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
  color: $darkGray

body 
  display: flex
  padding: 0 1rem

html,
body 
  height: 100%
  margin: 0

section 
  width: 375px
  margin: auto
  padding: 2rem
  background: white
  border-radius: $borderRadius
  box-shadow: $boxShadow

.input__container
  position: relative

input
  font-size: 16px
  width: 100%
  padding: .75rem
  border: 2px solid $lightGray
  border-radius: 8px
  box-sizing: border-box

::placeholder
  opacity: .5

details
  margin-top: 2rem
  padding-left: 1.35rem

  &[open]
    animation: opacity $animation

summary
  margin-left: -1.1rem
  padding-bottom: .5rem
  font-weight: 600

[search="clear"]
  position: absolute
  right: .75rem
  top: .75rem
  background: none
  border: none
  padding: 0

  svg
    height: 24px
    width: 24px
    pointer-events: none
    animation: opacity $animation
    

[search="counter"]
  position: absolute
  bottom: -1rem
  font-size: .75rem
  opacity: .5

[search="noResults"]
  margin: 2rem 0 0
  animation: opacity $animation

@keyframes opacity 
  0%    
    opacity: 0 

  100%
    opacity: 1

              
            
!

JS

              
                (function () {
  'use strict';

  var controls = {
    input: document.querySelector('[search="input"]'),
    items: document.querySelectorAll('[search="item"]'),
    noResults: document.querySelector('[search="noResults"]'),
    clear: document.querySelector('[search="clear"]'),
    counter: document.querySelector('[search="counter"]'),
    indexedItems: [],
    hasControls: function() {
      return this.input != undefined && this.items != undefined
    }
  }

  // checks for required search components
  if (!controls.hasControls()) return;

  // shows/hides no results message
  function toggleNoResultsMessage(searchTerm) {
    // checks if any items are open
    var hasResults = Array.prototype.filter.call(controls.items, function (item) {
      return item.hasAttribute('open');
    })

    if (hasResults.length && searchTerm.length > 1) {
      // hide no results message if items are open
      controls.noResults.setAttribute('hidden', '');
    } else if (searchTerm.length > 1) {
      // show no results message
      controls.noResults.removeAttribute('hidden');
      Array.prototype.forEach.call(controls.items, function (item) {
        item.setAttribute('hidden', '')
      })
      return;
    } else {
      // hide no results message
      controls.noResults.setAttribute('hidden', '');
    }
  }

  // searches and highlights
  function searchAndHighlight() {
    Array.prototype.forEach.call(controls.items, function (item) {
      item.innerHTML = item.innerHTML.replace(/<mark>([^<]+)<\/mark>/gi, '$1');
    });

    var searchTerm = event.target.value.trim().toLowerCase();

    if (searchTerm.length > 1) {
      controls.indexedItems.forEach(function (item, i) {
        if (controls.indexedItems[i].indexOf(searchTerm) != -1) {
          controls.items[i].setAttribute('open', true);
          controls.items[i].removeAttribute('hidden'); // removes hidden attribute when deleteing
          controls.items[i].innerHTML = controls.items[i].innerHTML.replace(new RegExp(searchTerm + '(?!([^<]+)?>)', 'gi'), '<mark>$&</mark>');
        } else {
          controls.items[i].removeAttribute('open');
          controls.items[i].setAttribute('hidden', '');
        }
      });
      controls.clear.removeAttribute('hidden');
    } else {
      Array.prototype.forEach.call(controls.items, function (item) {
        item.removeAttribute('open');
        item.removeAttribute('hidden');
      });
      controls.clear.setAttribute('hidden', '');
    }
    countHighlights();
    toggleNoResultsMessage(searchTerm);
  }

  // counts number of matches
  function countHighlights() {
    var count = document.querySelectorAll('mark').length;
    if (count) {
      return controls.counter.innerHTML = count + ' results';
    }
    return controls.counter.innerHTML = '';
  }

  // sanitize search result matches
  Array.prototype.forEach.call(controls.items, function (item) {
    controls.indexedItems.push(item.textContent.replace(/\s{2,}/g, ' ').toLowerCase());
  });
  
  controls.input.addEventListener('keydown', function(event) {
    // prevent submit on enter
    if (event.keyCode === 13) 
    {
      event.preventDefault();
      return false;
    }
  })

  // input keyup
  controls.input.addEventListener('keyup', function(event) {
    searchAndHighlight();
  });

  // clear button click
  controls.clear.addEventListener('click', function() {
    event.target.setAttribute('hidden', '');
    toggleNoResultsMessage('');
    searchAndHighlight();
    controls.input.focus();
  })

})();

              
            
!
999px

Console