Styles
Learn how to style UI elements using properties and styles in LVGL Pro Editor.
Apply visual styling to your UI components through style sheets, local styles, and dynamic bindings that enable conditional styling and theme switching.
Overview
In Components, Widgets, and Screens, both style sheets (lv_style_t) and local styles can be used.
Style Sheets
Simple Usage
In the <styles> section, styles and their properties can be defined. The styles later can be assigned to Components, Widgets, or Screens.
The <styles> can be defined in the UI elements' XML file, or in globals.xml.
It looks like this in the practice
<component> <!-- Could be <widget> or <screen> too -->
<styles>
<style name="style_red"
help="Red border"
border_width="2px"
border_color="0xff0000"/>
<style name="style_blue_bg"
help="Blue background"
bg_opa="50%"
bg_color="0x00f"/>
<style name="style_large_text"
help="Big blue text"/>
</styles>
</component>Styles can be referenced in the view like this:
<view>
<lv_slider value="20">
<style name="style_red"/>
<style name="style_blue_bg" selector="knob"/>
<style name="style_blue_bg" selector="knob|focused"/>
</lv_slider>
</view>As shown in the example, parts and states can be set using selector.
It's recommended to prefix style names with style_ to avoid name collisions.
Transitions
By default the properties of a style are applied immediately when the widget's state changes. A <transition> child of a <style> animates the listed properties instead:
<styles>
<style name="style_button" bg_color="0x0088ff" transform_scale_x="256">
<transition props="bg_color transform_scale_x" duration="300"/>
</style>
<style name="style_button_pressed" bg_color="0x004488" transform_scale_x="230">
<transition props="bg_color transform_scale_x" duration="80"/>
</style>
</styles>
<view extends="lv_button">
<style name="style_button"/>
<style name="style_button_pressed" selector="pressed"/>
</view><transition> supports these properties:
props- Space separated list of the style properties to animate. Mandatory. Useremoveto delete a previously set transition.duration- Duration of the animation in milliseconds. Mandatory.delay- Delay before the animation starts in milliseconds. Default is0.easing- The animation path, e.g.ease_out. See Easing. Default islinear.
A transition describes how to animate into the state of the style that contains it. In the above example pressing the button uses the 80 ms transition of style_button_pressed, and releasing it uses the 300 ms transition of style_button.
This means that to animate in both directions, a <transition> needs to be added to the default state's style too. If only the pressed style has one, the button animates when it's pressed, but snaps back when it's released.
Only state changes (pressed, checked, disabled, focused, ...) start a transition. Switching a style on and off, with enabled or with <bind_style>, is not a state change, so those styles are applied without animation.
Keep these limitations in mind:
- A style can store only one transition. To use different durations for different properties, split them into two styles.
- Only numeric (including opacity) and color properties can be animated. Properties referencing other data (
text_font,bg_image_src,bg_grad, ...) and most enums and booleans (flex_flow,text_align,clip_corner, ...) are ignored with a warning. - The composite names are expanded, so e.g.
props="pad_all"transitions all the four paddings. - Like in any other style property, constants and
{ }expressions of them can be used, but API<prop>s can't.
Conditional Styles
Instead of adding a style unconditionally, let an expression decide whether it applies. Add the style as usual and give it an enabled attribute:
<styles>
<style name="style_warning" bg_color="0xff8800"/>
</styles>
<view extends="lv_obj">
<style name="style_warning" enabled="{subject_temp GT 10 AND subject_temp LE 30}"/>
</view>enabled takes an expression like any other attribute value. Reading a subject or a variant in it makes the style turn itself on and off as that value moves, as above; an expression built from constants and properties, like enabled="{is_compact}", decides once at creation and stays that way.
A typical use case is light/dark theme switching:
<component>
<styles>
<style name="style_base" bg_color="0xeee" text_color="0x111" radius="20" />
<style name="style_dark" bg_color="0x333" text_color="0xeee" />
</styles>
<view extends="lv_button">
<style name="style_base" />
<style name="style_dark" enabled="{subject_dark_theme_on}"/>
<lv_label text="Apply"/>
</view>
</component>A few details:
- The condition doesn't have to be a comparison. Anything but
false,0, and empty text counts as enabled, soenabled="{subject_error}"is enough for an int subject. A float is the exception:0.00is not the text0, so compare it, as inenabled="{subject_level GT 0}". enabled="false"works too, for a style that starts off.- An expression reading no subject is decided at creation, so the style stays that way for the life of the widget.
- One
enabledbinding per style and selector. Two bindings on the same pair fight over the same switch, and last one of them wins.
enabled doesn't add or remove the style, it only switches it. The style keeps its place among the widget's other styles, no matter how often the condition flips.
Legacy: bind_style
Before enabled existed, a style was switched by comparing one subject to one reference value:
<bind_style name="style_dark" subject="subject_dark_theme_on" ref_value="1"/>It still works and accepts the same selector as <style>, but enabled does the same thing with a full expression, so prefer that in new XML.
Local Styles
Simple Usage
Local styles can be used directly on individual elements:
<lv_label style_bg_opa="200" style_bg_color="0x123456"/>Selectors are also supported for local style properties:
<lv_slider style_bg_opa-indicator-pressed="200"/>Local Style Binding
A local style property takes an expression like any other attribute, selectors included, and reading a subject in it keeps the property up to date. See Data bindings.
<lv_label style_text_color-pressed="{subject_error_mode ? 0xff0000 : 0xaaaaaa}"/>
<lv_slider style_bg_opa-knob="{subject_brightness * 2}"/><bind_style_prop> is the older way. It copies one subject's value into one property as-is, with no expression:
<component>
<view extends="lv_slider">
<bind_style_prop prop="bg_opa" selector="knob|pressed" subject="slider_knob_opa" />
</view>
</component>Relation to Constants
In the values of style properties constants can be used as well.
<component>
<consts>
<int name="thin" value="2" help="Thin border"/>
<color name="dark" value="0x333" help="A dark color for the button's background"/>
</consts>
<styles>
<!-- Global constants can be used as well, e.g. `muted` below -->
<style name="style_main" border_width="{thin}" bg_color="{dark}" bg_opa="{muted}"/>
</styles>
</component>Relation to API <prop>s
Since styles are initialized only once, it's not possible to pass API <prop>s to them when the instances are created. So this is invalid:
<component>
<api>
<prop name="thickness" type="int"/>
</api>
<styles>
<!-- ❌ CAN'T DO THIS. A style is initialized before any instance
exists, so a <prop> has no value -->
<style name="style_main" border_width="{thickness}"/>
</styles>
</component>Constants do have a value there, so a style property can be computed from them with an expression:
<consts>
<int name="thin" value="2"/>
</consts>
<styles>
<!-- ✅ Constants and literals work -->
<style name="style_main" border_width="{thin * 2}"/>
</styles>And you can pass <api> <prop>s to local style properties:
<component>
<api>
<prop name="thickness" type="int"/>
<prop name="bg_color" type="color"/>
</api>
<view style_bg_color="{bg_color}">
<!-- ✅ Passing api properties to local styles work -->
<lv_slider style_border_width-knob="{thickness}"/>
</view>
</component>Gradients
Gradients are not yet supported in the UI Editor preview, but they can be used in exported and runtime code.
Before the styles tag, the gradients tag can be used to define various gradients that can later be referenced in your styles.
When a gradient is created, reference it by name in your styles:
<style bg_grad="grad1"/>Or apply it directly to elements:
<lv_button style_bg_grad="grad1"/>Gradients are not yet supported in LVGL's UI Editor, but can be used when exporting and running your code.
Horizontal or Vertical Gradient
To define a simple horizontal or vertical gradient:
<gradients>
<horizontal name="grad1">
<stop color="0xff0000" offset="20%" opa="40%"/>
<stop color="0x00ff00" offset="128" opa="100%"/>
</horizontal>
</gradients>Linear Gradient
To define a skewed gradient between two points:
<gradients>
<linear name="grad1" start="50 50" end="100 80">
<stop color="0xff0000" offset="20%" opa="100%"/>
<stop color="0x00ff00" offset="240" opa="100%"/>
</linear>
</gradients>Radial Gradient
To define a radial gradient:
<gradients>
<radial name="grad1" center="100 50%" edge="200 50" focal_center="50 80%" focal_edge="55 80%">
<stop color="0xff0000" opa="100%"/>
<stop color="0x00ff00" opa="100%"/>
</radial>
</gradients>Conical Gradient
To define a conical gradient:
<gradients>
<conical name="grad1" center="80 50%" angle="45 270">
<stop color="0xff0000" opa="100%"/>
<stop color="0x00ff00" opa="100%"/>
</conical>
</gradients>Last updated on