Styles

Customize the look and feel of Elements with Styles. Styles, also referred to as Classes in the Designer, save styling information that can be applied to as many elements as you want across a site.

Working with styles

1

Create a style

To create a style, you need to provide a unique name. The Webflow API prevents creating styles with duplicate names to maintain uniqueness across your project.

1// Create new style
2const newStyle = await webflow.createStyle(styleName);
2

Add style properties

Add CSS properties to a style. Refer to this list of Style Properties for a full index of properties that can added to a style in Webflow.

You can add properties to a style in two ways:

The set property method requires you to pass a single property name and its corresponding value as string parameters. Additionally, you can include an optional options parameter, which we cover below.

1// Create new style
2const newStyle = await webflow.createStyle("My Custom Style");
3
4// Set a single property
5await newStyle.setProperty("background-color", "blue")
3

Apply styles to elements

Once you’ve created and modified a style, you can apply it to one or more elements.

1// Get selected element
2const selectedElement = await webflow.getSelectedElement()
3
4// Get style
5const myStyle = await webflow.getStyleByName("My Custom Style");
6
7// Apply style to element
8await selectedElement.setStyles([newStyle])

Responsive styling with breakpoints and pseudo states

Webflow’s responsive design features enable customization of style properties for different contexts, such as varying screen sizes or specific states like :hover or :active. You can also target a specific component variant.

Pass the options parameter when setting style properties to customize the style for different breakpoints, pseudo-states, and variants.

1{
2 breakpoint?: BreakpointId,
3 pseudo?: PseudoStateKey,
4 variantId?: string
5}
  • BreakpointId: Identifies the responsive breakpoint to get styles for.

    1type BreakpointId = "xxl" | "xl" | "large" | "main" | "medium" | "small" | "tiny"
  • PseudoStateKey: Specifies a CSS pseudo-class to get styles for.

    1type PseudoStateKey = "noPseudo" | "nth-child(odd)" | "nth-child(even)" |
    2 "first-child" | "last-child" | "hover" | "active" | "pressed" |
    3 "visited" | "focus" | "focus-visible" | "focus-within" |
    4 "placeholder" | "empty" | "before" | "after"
  • variantId: The ID of the variant to target. To retrieve variant IDs, use component.getVariants(). The primary variant returns an ID of 'base' from that method, but 'base' is not a valid value for this parameter. To target the primary variant’s styles, omit variantId.

Example

1// Create new style
2const newStyle = await webflow.createStyle("My Custom Style");
3
4// Property Map for XXL Breakpoint
5const propertyMapXXL = {
6 'font-size': "16px",
7 'font-weight': "bold",
8}
9
10// Property Map for Medium Breakpoint
11const propertyMapMedium = {
12 'font-size': "12px",
13 'font-weight': "bold",
14}
15
16// Set style properties for XXL Breakpoint and hover state
17await newStyle.setProperties(propertyMapXXL, {breakpoint: "xxl", pseudo: "hover"})
18
19// Set styles for Medium Breakpoint and hover state
20await newStyle.setProperties(propertyMapMedium, {breakpoint: "medium", pseudo: "hover"})
21
22// Set style properties for a specific variant
23await newStyle.setProperty('color', 'red', { variantId: 'variant-123' })
24
25// Set style properties for a variant at a specific breakpoint and pseudo-state
26await newStyle.setProperty('font-size', '24px', {
27 variantId: 'variant-123',
28 breakpoint: 'medium',
29 pseudo: 'hover',
30})

Breakpoint IDs

Breakpoint IDDescription
xxlVery large screens or high-resolution monitors
xlLarge desktop monitors
largeStandard desktop monitors
mainSuitable for smaller desktops or large tablets
mediumSuitable for tablets and some large phones
smallSuitable for larger mobile devices
tinySuitable for the smallest mobile devices

Pseudo-State Keys

Pseudo-StateDesigner StateDescription
hoverHoverElement is hovered over by the mouse
pressedPressedElement is in pressed state
visitedVisitedLink element has been visited
focusFocusedElement has keyboard/input focus
focus-visibleFocused (Keyboard)Element has keyboard focus with visible indicator
focus-withinElement or its descendant has focus
placeholderPlaceholderPlaceholder text in form block inputs
first-childFirst ItemFirst Collection Item in a collection list
last-childLast ItemLast Collection Item in a collection list
nth-child(odd)Odd ItemsOdd-numbered Collection Item in a collection list
nth-child(even)Even ItemsEven-numbered Collection Item in a collection list

FAQs

No, you can’t style HTML Tags with the Designer API. Currently, the Designer API only supports creating and applying CSS Classes.