APIsChangelog
Log In

Overview

Variables allow you to name, organize and reference size, color and font values across your Webflow projects. When someone updates a variable value, that change will update everywhere it’s used on the site.

Once you've created a variable, you can set it as the value of a style property to easily dictate design elements like colors, size and typography, and ensure that designs are adaptable, consistent, and easily modifiable.

Collections

Collections serve as an organizational layer for Variables. Currently, Webflow offers a single, default collection. You can access your variables after retrieving your collection using webflow.getDefaultVariableCollection().

Variables

Webflow currently supports 3 types of variables:

  • Size
  • Color
  • Font

Creating a Variable

Each variable type can be created using a specific function. The variable names must be unique across the collection.

General Syntax

collection.createXXXVariable(variable-name, variable-value);

Type Accepted Formats for Value Examples
Color Strings of one of three formats:
- Color name
- Color rgb hex value
- Color rgba hex value
Color Name
collection.createColorVariable("primary", "red");

RGB Hex
collection.createColorVariable("primary", "#ffcc11");

RGBA Hex
collection.createColorVariable("primary", "#fffcc11");
Size Object in the following format:
{ unit: “px” | “rem” | “em” | “vw” | “vh” | “svh” | “svw” | “ch” value: number }
collection.createSizeVariable(“defaultLength”, { unit: “px”, value: 50 });
FontFamily String of the font family name (e.g., "Verdana") collection.createFontFamilyVariable(“defaultFont”, “Verdana”);

Selecting a Variable

Variables can be selected from their Collection by their name or ID.

// Get Collection
const collection = await webflow.getDefaultVariableCollection()

if (collection) {

  // Get variable by ID
  const variableById = await collection.getVariable('id-123')

  // Get Variable by Name
  const variableByName = await collection.getVariableByName('Space Cadet')
  }

Editing a Variable

Renaming and setting new values on a Variable.

Renaming a Variable
Variables in Webflow can be renamed for better clarity or organization. After you've successfully renamed a variable, all instances where this variable is used will automatically reference the new name.

// Get Collection
const collection = await webflow.getDefaultVariableCollection()

if (collection) {

  // Get variable and reset name
  const variable = await collection.getVariableByName("Space Cadet")
  await variable?.setName('Our awesome bg color')
}

Setting a Variable value
While the type of a variable in Webflow remains fixed and cannot be changed, its value can be updated. The format for updating the value is consistent with its initial declaration.

// Get Collection
const collection = await webflow.getDefaultVariableCollection()

// Get Variable
const variable = await collection?.getVariable('id-123')

// Check Variable type and set color
if (variable?.type === "Color") await variable.set("#fffcc11");

Applying Variables to Styles

After defining your variables, you can incorporate them into your styles. To do this, simply use the variable as the property value in the style you're customizing.

// Get collection
const collection = await webflow.getDefaultVariableCollection()

// Get Style and desired variable
const style = await webflow.getStyleByName(styleName)
const variable = await collection?.getVariable(variableName)

// Check variable type and set property
if (variable?.type === 'Size') await style?.setProperties({ "font-size": variable})

Applying a Variable as an Alias
When you assign a variable to reference another, the second variable effectively becomes an alias of the first. This results in the second variable mirroring the value of the first one they are both linked to. Any changes made to the value of the first variable will be automatically reflected in the second, maintaining their synchronization. It's essential that both variables are of the same type for alias references to function correctly.

In the below example, both the variables are of type ColorVariable.

// Get Collection
const collection = await webflow.getDefaultVariableCollection()

// Create first variable
const firstVariable = await collection?.createColorVariable('Default Color', 'red')

if (firstVariable) {

  // Create second variable as an alias of the first
  const secondVariable = await collection?.createColorVariable('Space Cadet', firstVariable)
  }

Removing a variable

Deleting a Variable
Variables in Webflow can also be deleted. When a variable is removed, it undergoes a "soft delete" process, similar to the approach taken in the Webflow user interface.

// Get collection
const collection = await webflow.getDefaultVariableCollection()

// Get variable by name
const variable = await collection?.getVariable('id-123')

// Delete variable
await variable?.remove()