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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

              
                <div class="container">

  <h1>Highlight Color Change</h1>

  <p>Highlight this text to see the color change! I've set up an array that holds all the possible highlight colors in the JavaScript panel of this pen. When your mouse button is pressed, I grab the first color from that array and remove it by using shift(), apply that color to a CSS variable, and then push that color value to the bottom of the array. This allows me to have that infinite color rotation.
  </p>

  <p>
    The trick here is to use the ::selection selector. In this example, I set the background property equal to the CSS variable I set up and that is being controlled by the JavaScript. Pretty neat, huh?</p>

  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Maxime corrupti itaque tempora illo velit fugiat minus! Ipsum pariatur dolorem voluptatem earum recusandae veniam provident, aut similique? Dolorem quibusdam sit quas. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Labore maiores explicabo, aliquam accusamus voluptatem atque dolor sed magni cum iure consequatur totam ipsam, reiciendis modi voluptatum nihil alias? Amet, unde? Lorem ipsum dolor sit amet consectetur adipisicing elit. Aperiam dolorum consequatur nostrum ipsam, impedit aut, itaque corrupti nulla mollitia optio doloribus? Sit amet suscipit quisquam velit cumque consectetur natus possimus.</p>

  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Maxime corrupti itaque tempora illo velit fugiat minus! Ipsum pariatur dolorem voluptatem earum recusandae veniam provident, aut similique? Dolorem quibusdam sit quas. Lorem ipsum dolor, sit amet consectetur adipisicing elit. Labore maiores explicabo, aliquam accusamus voluptatem atque dolor sed magni cum iure consequatur totam ipsam, reiciendis modi voluptatum nihil alias? Amet, unde? Lorem ipsum dolor sit amet consectetur adipisicing elit. Aperiam dolorum consequatur nostrum ipsam, impedit aut, itaque corrupti nulla mollitia optio doloribus? Sit amet suscipit quisquam velit cumque consectetur natus possimus.</p>
</div>
              
            
!

CSS

              
                :root {
  --highlight-color: null;
}

body {
  background: black;
  font-family: "Helvetica", sans-serif;
}

.container {
  width: 80%;
  min-height: 100vh;
  color: white;
  margin: 0 auto;
}

::selection {
  background: var(--highlight-color);
}

              
            
!

JS

              
                const colors = ["#c1b001", "#a8140e", "#4315aa", "#359d09", "#8f4762"];

window.addEventListener("mousedown", (e) => {
  const color = colors.shift();
  document.documentElement.style.setProperty("--highlight-color", color);
  colors.push(color);
});

              
            
!
999px

Console