Variables & Collections
Variables allow you to name, organize and reference number, percentage,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 }); |
Number & Percentage | Number value between 0 and 100 | collection.createPercentageVariable(“opacity”, 50); collection.createNumberVariable(“scale”, 1.5); |
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.
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.
Setting a Variable value
While the type of 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.
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.
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.
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.