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 class="search-box">
<p><label>Search:</label><input id="query" type="text" /> 
  <small>(try searching for "Safari")</small><p>
</section>
<article>
  <p>Web technology is constantly moving forward, with both big new features and small subtle adjustments. Now days, developers expect web browsers to update multiple times a year, instead of the once or twice a year typical of the late 2000s… In response, we changed the way Safari is numbered just over two years ago.</p>
  <p>A new version of Safari shipped 17 times in the last 28 months — version 15.0, 15.1, 15.2, 15.3, 15.4, 15.5, 15.6, 16.0, 16.1, 16.2, 16.3, 16.4, 16.5, 16.6, 17.0, 17.1, and now, today’s Safari 17.2. This makes it possible to spread out the arrival of new web technology more widely across the year, and get it into the hands of your users that much sooner.</p>
  <p>With 39 features and 169 fixes, today’s release is Safari’s biggest December release of web technology ever. </p>
</article>
              
            
!

CSS

              
                ::highlight(search-results) {
  background-color: rgb(53, 177, 53);
  color: yellow;
  padding: 10px;
}

























/*---- General styling ----*/

* { box-sizing: border-box; }
body {
  font-family: Avenir, Helevetica, sans-serif;
  font-size: 1.2rem;
  margin: 1.4rlh;
}
.search-box {
  margin-block: 1rlh;
  p {
    margin: 0;
  }
  input {
    border: 1px solid black;
    height: 1.5rlh;
    margin: 0;
    margin-inline: 0.5rem;
    overflow: visible;
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
  }
  label {
    display: inline-block;
    margin-block-end: .5rem;
  }
  small {
    color: #888;
    font-size: 0.9rem;
    display: block;
    margin-inline-start: 9ch;
/*     font-style: italic; */
  }
}
article {
  font-size: 0.9rem;
  margin-trim: block;
  columns: 30ch;
  gap: 2rlh;
  p {
    margin-block-start: 0;
  }
}
body {
  background: #ebe8e0;
}
@media (prefers-color-scheme: dark) {
  body {
    color: #eee;
    background: #262a39;
  }
}

              
            
!

JS

              
                const query = document.getElementById("query");
const article = document.querySelector("article");

// Find all text nodes in the article. We'll search within
// these text nodes.
const treeWalker = document.createTreeWalker(article, NodeFilter.SHOW_TEXT);
const allTextNodes = [];
let currentNode = treeWalker.nextNode();
while (currentNode) {
  allTextNodes.push(currentNode);
  currentNode = treeWalker.nextNode();
}

// Listen to the input event to run the search.
query.addEventListener("input", () => {
  // If the CSS Custom Highlight API is not supported,
  // display a message and bail-out.
  if (!CSS.highlights) {
    article.textContent = "CSS Custom Highlight API not supported.";
    return;
  }

  // Clear the HighlightRegistry to remove the
  // previous search results.
  CSS.highlights.clear();

  // Clean-up the search query and bail-out if
  // if it's empty.
  const str = query.value.trim().toLowerCase();
  if (!str) {
    return;
  }

  // Iterate over all text nodes and find matches.
  const ranges = allTextNodes
    .map((el) => {
      return { el, text: el.textContent.toLowerCase() };
    })
    .map(({ text, el }) => {
      const indices = [];
      let startPos = 0;
      while (startPos < text.length) {
        const index = text.indexOf(str, startPos);
        if (index === -1) break;
        indices.push(index);
        startPos = index + str.length;
      }

      // Create a range object for each instance of
      // str we found in the text node.
      return indices.map((index) => {
        const range = new Range();
        range.setStart(el, index);
        range.setEnd(el, index + str.length);
        return range;
      });
    });

  // Create a Highlight object for the ranges.
  const searchResultsHighlight = new Highlight(...ranges.flat());

  // Register the Highlight object in the registry.
  CSS.highlights.set("search-results", searchResultsHighlight);
});
              
            
!
999px

Console