Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

6.7 KB
Raw Blame

description: >- Style elements differently based on user interaction (hover, focus, active) or target virtual parts of an element (::before, ::after) using states and selectors.

🎯 States and selectors

States and selectors let you apply styles conditionally — for example, change a button's color on hover, or add decorative content with ::before.

They live in the Style Sources dropdown alongside your Tokens. Open it by clicking the Style Sources area above the Style Panel or pressing ⌘ + Enter (Mac) / Ctrl + Enter (Windows).

Style Sources dropdown listing hover, focus, and active states

Available states appear at the bottom of the Style Sources dropdown

States (pseudo-classes)

States are CSS pseudo-classes that apply styles when a condition is true, such as the user hovering over an element.

Adding a state

  1. Select an instance on the canvas.
  2. Open the Style Sources dropdown (⌘ + Enter).
  3. Choose a state from the list at the bottom, or type one in.
  4. The state appears as a tag in the style sources area. While it's active, every style you set applies only to that state.
  5. Click the state tag again to deselect it and return to the base (default) styles.
Style sources field with the hover state selected

The :hover state is selected — styles set now only apply on hover

Custom states

You can type any valid CSS pseudo-class into the Style Sources dropdown. If it's not in the predefined list, Webstudio will accept it as a custom selector.

{% hint style="info" %} Custom states can target descendants while staying scoped to the selected instance. For example, :hover .button targets descendants with the custom class button while the selected instance is hovered, and :hover > .icon targets only direct children with the custom class icon.

Prefer CSS variables for parent-child interactions. They keep each property on the child that uses it, can coordinate several children from one parent state, and do not couple the interaction to descendant classes or structure. Use a descendant custom state when the interaction specifically requires selector-based targeting.

Start the selector with a pseudo-class, pseudo-element, or attribute selector for the selected instance. You can then use a descendant ( ) or child (>) combinator. Selector lists, selectors that start with a type, class, or ID, and sibling combinators (+ and ~) are rejected because they can target elements outside the selected instance. {% endhint %}

Pseudo-elements

Pseudo-elements target virtual parts of an element, such as inserting content before or after it, or styling the first letter.

Adding a pseudo-element

  1. Select an instance.
  2. Open the Style Sources dropdown (⌘ + Enter).
  3. Type ::before, ::after, or another pseudo-element — autocomplete will suggest matches.
  4. The pseudo-element appears as a tag. While selected, styles apply to that virtual element.
Pseudo-element suggestions filtered by typing ::b, including ::before

Type :: to see available pseudo-elements

Common pseudo-elements

Pseudo-element What it targets
::before Virtual element inserted before the content
::after Virtual element inserted after the content
::placeholder Placeholder text in inputs and textareas
::selection Text selected/highlighted by the user
::first-letter First letter of a block element
::first-line First line of a block element
::marker Bullet or number of a list item
::backdrop Box behind a fullscreen or modal element

{% hint style="info" %} ::before and ::after require a content value to be visible. Set it in the Advanced section — even an empty string ("") works. {% endhint %}

Example: decorative element with ::before

  1. Select a Box, Link, or Button.
  2. Add ::before via the Style Sources dropdown.
  3. In the Advanced section, set content to "".
  4. Set display to block and give it a width, height, and background.
  5. Deselect the ::before tag to go back to the base styles.
Style sources with the ::before pseudo-element selected

Select ::before to edit its styles

Combining states and tokens

States work together with Tokens. When you have a Token selected, adding a state applies styles for that state within that Token. This keeps hover styles, focus styles, and base styles neatly organized.

You can also use CSS variables to coordinate several parent-child style changes — define variables on the parent, then change their values on the :hover state so all children update at once. See Parent-child interactions for details.

Related