Design and Styling

States and Context Selectors

22min

Element styles in different states

You can style each element in different states. For example, on hover, when active or in focus. This way you can create micro-animations that make your website more interactive and dynamic.

You can add an element state in the Design tab. To do this, open the drop-down menu in the States section and select the desired option.

Document image


To style different element states, select states from the list one by one. 

Let's change the transparency of the element in the On Hover state.

Document image


If you need to add a state for the element with a custom class, open Class settings and select a state you need.



Context Selectors: Styling child elements 

You can style both the element itself in its different states and its child elements. For example, when you hover over a card, you can change the background color of the button inside it.

Use Context Selectors to do this. By using Context Selectors you can style a child element depending on the state of the parent element.

Context Selectors can currently be used for unique element styles. This option is not available for custom classes.

Styling the child element with a defined state of the parent element

1. Add a state you need to the parent element.

Example: On Hover state.

Document image


2. Select a child element. In the Design tab, next to the States setting, a yellow Context label No1 will appear. This way you apply one Context Selector to the child element.

A yellow label will be displayed on the canvas with the name of the parent element and its state, in the context of which the styles are defined (Div Block:Hover).

Document image


3. Assign styles you need to the child element. For example, change the background color.

Document image


4. Check in preview. When you hover over the card, the button changes color.

Document image


Done! The button has a context selector added that will work when hovering over the card.

Deleting state for the parent element

If you delete the state from the parent element, all styles in the context of this state will also be deleted for child elements.

Nesting levels for Context Selectors

Context Selectors can have unlimited nesting, as they are determined by the level of nesting of elements in each other.

Let's look at the container with cards:

Document image

  • Card is a child element of the container. It can be styled in the context of the container. For example, when you hover over the container, the card will have an outline.
  • Button is a child element of two parent elements at once: the container and the card. Therefore, it can be styled in the context of two elements. For example, when you hover over the container, you can change the background color of the button, and when you hover over the card, you can add an outline.
  • Text on the button is a child element of three parent elements at once: the container, the card, and the button. This means that its styles can be changed when you hover over each of these elements.

By nesting context selectors you can flexibly customize styles in different states and implement multi-level micro-animation effects.

Example 1. Styling multiple child elements of a parent element

For any state of the parent element, you can style multiple child elements.

For example, when hovering over the card, you can change the styles for two child elements: text color for the text block and background color for the button.

Document image


When you hover over the card, the text color and the background color of the button change.

To do this:

1. Select the card and assign the On Hover state. 

Document image


2. Select the text element. I will have a context selector label. Set styles for the element that will apply when hovering over the card — change the text color.

Document image


3. Select the button. It will have a context selector label. Assign the button styles that will apply when hovering over the card — change the background color.

Document image


Done! Now when you hover over the card, the color in the text block and the background color of the button change.

Example 2. Styling the child element in the context of its parent element + its own state

You can change the styles of the element depending on:

  1. Its own state;
  2. Its parent's state;
  3. Parent's state + its own state.

For example, with the third option, you can create the following effects for the button: 

  • When you hover over the card (parent element), the background color of the button changes — (as in Example 1).
Document image


When you hover over the card, the button's background color changes.

  • When you hover over the button itself, outline appears. 
Document image


When you hover over the button, outline appears.

To do this, follow these steps:

1. The first steps are the same as in Example 1. Here, you style the button in the context of the card.

2. Next, you need to add a state to the button. Add the On Hover state to the button.

Document image


3. Assign your button some on hover styles — add an outline.

Document image


Done! The button has effects when hovering over the card and the button itself.

Example 3. Styling the element in the context of multiple parent elements

Here, nesting property of context selectors is used — the styles of the element depend on the state of its parents.

Let's create the following effects:

  • When you hover over the card (parent element), the background color of the button changes — (as in Example 1).
Document image

  • When you hover over the button, outline appears (as in Example 2) and additionally the color of the text inside it.
Document image


To do this, follow these steps:

1. The first steps are the same as in Example 2. Here, you style the button in the context of the card + the button itself.

2. Next, style the text on the button. Select the text element inside the button. It will have label 2 Context (dual context). Also add labels of two parent elements on the canvas, the state of which will determine the styles of the text element (card and button).

Document image


Assign the styles to the text, which will apply when hovering over the button. The text color changes.

Document image


Done! Two context selectors are used with the text inside the button — its styles change when hovering over the card and the button.

Other examples of using context selectors

You can use context selectors to create a variety of micro-animations. You can make elements appear and disappear, change their sizes, move, and so on.

Here are two examples::

1. When hovering over the card hidden elements appear.

For example, when hovering over the product card, you can show additional information and the button, and when hovering over the button, its color changes.

Document image


You can do it by adding a context selector to the additional block, which is triggered when hovering over the card. The opacity and motion properties are used to create the block appearance effect.

2. Creating tooltips

You can make a block with a tooltip that appears when you hover over the icon.

Document image


To do this, you can use a context selector that is triggered when you hover over the icon, and also the resize and opacity settings.

You can watch our stream for more details on the above examples. 

Smooth transitions when changing states

To make styles change smoothly when changing the state, you need to use the Transitions settings.

1. For the selected state, add Transitions in the Appearance section.

Document image


2. Set the transition duration in milliseconds and select the easing type.

Document image


Difference between using context selectors and animations

Both context selectors and animations can be used to create interactive effects. But there are differences:

  • Effects. With context selectors you can change any CSS properties of elements, such as size, background and text color, borders, shadows, and so on. With animations you can create a limited set of effects: changing the size, tilt, scaling, opacity, as well as movement and rotation.
  • Possibility of use. Context selectors are only for nested elements. Animations can be configured for any elements on the page.
  • Animation triggers. Context selectors are used only for element states (on hover, when active, in focus). Animations can be used to create effects on click, when the element appears, and when scrolling the page.
  • Page speed. Context selectors use CSS, and animations use scripts. Therefore, using context selectors, you will get a simpler code, as a result, the page will load faster than when using animations.



Updated 26 Nov 2024
Did this page help you?