Ever wanted to tweak CSS variables in real-time while presenting a demo or experimenting with styles? While tools like Tweakpane exist, CodePen now offers its own native solution: slideVars. This guide will walk you through its setup, basic usage, and advanced configuration to supercharge your interactive prototypes. You can find the original inspiration in this source material.

Web developer coding on a laptop with CSS code on screen Programming Illustration

Getting Started in Two Lines

The core functionality is incredibly simple. Import the module and initialize it in your CodePen's JavaScript panel. It will automatically detect CSS variables defined in your styles and generate a control panel.

// Add this to your JS panel
import { slideVars } from "@codepen/slidevars";
slideVars.init();

Important Note: In default (auto) mode, your variables must be declared on the :root with default values. Here's the corresponding CSS:

/* Add this to your CSS panel */
:root {
  --primary-hue: 200; /* Adjustable via slider */
  --margin-size: 2rem; /* Adjustable via slider */
  --blur-amount: 5px; /* Adjustable via slider */
}

.element {
  background-color: hsl(var(--primary-hue), 100%, 50%);
  margin: var(--margin-size);
  filter: blur(var(--blur-amount));
}

Once added, a panel with sliders for --primary-hue, --margin-size, and --blur-amount will appear in the top-right corner of your pen.

Interactive UI design with sliders and controls Technical Structure Concept

Advanced: Manual Configuration

For more control, you can manually define each variable's properties, including its scope, range, and unit. Pass a config object to slideVars.init().

import { slideVars } from "@codepen/slidevars";

slideVars.init({
  variables: [
    {
      name: "--width",
      value: 150,
      unit: "px", // CSS unit
      min: 10,
      max: 500,
      scope: "#container" // CSS selector where var is used
    },
    {
      name: "--intensity",
      value: 0.7,
      unit: "", // Unit-less value
      min: 0,
      max: 1
      // step: 0.05 // Note: Increment control isn't fully supported in manual mode yet
    }
  ]
});

Current Limitation: The step property for fine-grained control doesn't seem fully operational in manual mode. Unit-less values increment in integers. Decimal stepping works in auto mode.

Modern developer desk setup with multiple monitors Development Concept Image

Pro Tips and Conclusion

  • Reposition the Panel: Place a <slide-vars> custom element anywhere in your HTML to render the controls there. By default, it's placed at the bottom.
  • Style It: You can apply CSS to the <slide-vars> element to match your demo's look.
  • Use Outside CodePen: The @codepen/slidevars module can be imported into projects beyond CodePen.

slideVars is a powerful tool for prototyping, teaching, or simply exploring the impact of CSS properties in real-time. Its biggest strength is enabling rich, interactive demos with minimal setup. Give it a try on your next CodePen experiment!